Jump to content

Wire cache and pagination


Zeka
 Share

Recommended Posts

Hi.

I'm doing the first steps in using wire cache, but I ran into a problem.

Here the code that I use to save and get cache:

$categories = getAncestors($page, 2);
$cacheNamespace = "products";
$cacheTitle = "prefix__$page->id-$page->template-{$user->language->name}";
if ($input->pageNum > 1) $cacheTitle .= "-page$input->pageNum";

$allProducts = $cache->getFor($cacheNamespace, $cacheTitle, "5", function($pages) use($categories) {
  return $pages->find("template=product, product_category|product_subcategory={$categories}, sort=product_stock, sort=title, limit=5");
});

This code generates "products__prefix__1047-product-category-default" table in DB that contain paginated page array:

{"PageArray":[1230,1095,1233,1142,1061],"template":48}

Then I output pagination:

echo $all_products->renderPager();

But it works only when cache expires and $all_products contain uncached array. 

So the question how to use wire cache in this situation?

Zeka

Link to comment
Share on other sites

Looks like my question was not clear enough, so I'll try to make it more clear.

I have a page with products listing. The structure of this page looks like this: 

exp.jpg

Because there is a lot of dynamic parts on a page I can't use template cache, so I stick to MarkupCache and WireCache. 

Every product's card cached by MarkupCache. Also, there are different sorting options which I want to cache by WireCache:

$sort = ($input->get->sort) ? "sort=" . $input->get->sort. "," : "sort=title,";

$products = $cache->get("products-{$sort}", "+10 minutes", function($pages) use($categories, $sort) {
  return $pages->find("template=product, product_category|product_subcategory={$categories}, sort=product_stock, {$sort} limit=100");
}); 

It works as expected and I get cached arrays limited for 100 products in DB and outputted list of 100 products. But I want to list only 20 products per page, so I have to limit find method for 20 items:

return $pages->find("template=product, product_category|product_subcategory={$categories}, sort=product_stock, {$sort} limit=20");
echo $products->renderPager();

It generates cached arrays for 20 products and renderPages method for the first page load, when there is no cache, will see all products and will output pagination markup, but for the second load, when $products variable loads from cache it will see only 20 items in cached array and will not output pagination markup.

How can I cache array and the get pagination markup from it? 

Link to comment
Share on other sites

  • 2 weeks later...

For caching children or paginated pages you can use a solution like this.

<?php
$children = $page->children("limit=10");
$cacheName = 'MyCache_children' . (string) $page . (string) $children;
// If you have multilanguage website you can add (string) $user->language to your cache name
// $cacheName = 'MyCache_children' . (string) $page . (string) $user->language . (string) $children;
$cacheExpire = "parend_id|id=" . (string) $children . "|" . (string) $page;
cache($cacheName, $cacheExpire, function() use($children) {
	$output = "";
	foreach($children as $item) $output .= "<h2>$item->title</h2>";
	return $output;
});

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...