I'm still not sure exactly how the comment caching is intended to work (see my other thread and respond if you will). But I'm looking into ways to do pager caching since I have a custom module that pages content and while I can cache each page of content, I'll also need to cache the pager since the pager doesn't work without a page_query having taken place.

So, I noticed in the comment caching that we're caching the build pager for a particular comment key. I was thinking it might be nice to add some generic pager caching to the actual pager.inc file, so that we can cache the pager information as opposed to the built pager, and adding in some caching here would allow pager caching to be used for comments, or anything else (such as my custom module).

I'm going to hack something together and see how it goes. If you're interested in seeing the results, let me know.

jonathan

Comments

ilmaestro’s picture

Ok, so it looks like all that the pager data consists of is the total number of pages. It makes no sense to cache the entire contents of a built pager just for this. It also makes good sense to cache the total number of pages along with the data you're caching itself... that way you only have to hit the database once to get your data and pager info. So, I wrote two functions to use to cache and retrieve paged data. The "set" function will also cache the page number with your data and the "get" function will restore the page number and GET['page'] into the appropriate global variables to make the pager work.

// Caches paged content along with pager data
function setPagedContentCache($key, $table, $content, $cachePager = true, $pagerElement = 0) {
	if ($cachePager) {
		global $pager_total;
		$content = array(
			"data" => $content,
			"pages" => $pager_total[$pagerElement]
		);
	}
	cache_set($key, $table, $content);
}

// Gets paged content and restores pager data
function getPagedContentCache($key, $table, $cachePager = true, $pagerElement = 0) {
	if ($cache = cache_get($key, $table)) {
		if ($cachePager) {
			global $pager_page_array, $pager_total;
			$page = isset($_GET['page']) ? $_GET['page'] : '';
			$pager_page_array[$pagerElement] = $page;
			$pager_total[$pagerElement] = $cache->data["pages"];
			return $cache->data["data"];
		}
		return $cache->data;
	}
}

This can be used for the comments as well as whatever other custom paged content you might have.

firebus’s picture

one issue here is that if user comment settings are active, then the page numbers can be different for different users.

so you need to cache the pages per user setting (or per user, but that's more to cache)

if you cache multiple keys per pager, you have to expire multiple keys which makes things difficult for memcache (and i'd like to keep things as compatible with memcache as possible)

you could also cache a single object that holds data for various user settings combos...

robertDouglass’s picture

Very nice topic of exploration. Please continue!

ilmaestro’s picture

I don't enable comment controls myself, but I think the way to get around this problem would be to cache the total items as well as the total number of pages (based on the default comments per page setting).

If comment controls are enabled and some user has selected to have a different number of comments per page, we could just calculate the total number of pages using the total number of items, which we'd have in our cache. So, I don't think this changes too much.

I'll have a look when I get a chance.