Zeka Posted January 29, 2017 Share Posted January 29, 2017 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 More sharing options...
Zeka Posted February 3, 2017 Author Share Posted February 3, 2017 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: 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 More sharing options...
LostKobrakai Posted February 3, 2017 Share Posted February 3, 2017 Do also cache the total/start number aside of the returned page array and use $pa->setStart() and $pa->setTotal() if you hit the cache. Link to comment Share on other sites More sharing options...
ukyo Posted February 17, 2017 Share Posted February 17, 2017 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now