ryan Posted December 16, 2010 Share Posted December 16, 2010 MarkupCache is a simple module that enables you to cache any individual parts in a template. I'm working on a site now that has 500+ cities in a select pulldown generated from ProcessWire pages. Loading 500+ pages and creating the select options on every pageview isn't terribly efficient, but I didn't want to cache the whole template because it needed to support dynamic parameters in the URL. The solution was to cache just the code that generated the select options. Here's an example: $cache = $modules->get("MarkupCache"); if(!$data = $cache->get("something")) { // ... generate your markup in $data ... $cache->save($data); } echo $data; I left the markup generation code (the part that gets cached) out of the example above to keep it simple. Below is the same example as above, but filled out with code that finds the pages and generates the markup (the part that gets cached): $cache = $modules->get("MarkupCache"); if(!$data = $cache->get("city_options")) { foreach($pages->find("template=city, sort=name") as $city) { $data .= "<option value='{$city->id}'>{$city->title}</option>"; } $cache->save($data); } echo $data; That was an example of a place where this module might be useful, but of course this module can used to cache any snippets of code. By default, it caches the markup for an hour. If you wanted to cache it for a longer or shorter amount of time, you would just specify the number of seconds as a second parameter in the get() call. For example, this would keep a 60-second cache of the data: $cache->get("city_options", 60) I hope to have it posted to GitHub soon and it will be included in the ProcessWire distribution. If anyone wants it now, just let me know and I'll post it here or email it to you. 9 Link to comment Share on other sites More sharing options...
martinluff Posted January 25, 2011 Share Posted January 25, 2011 Looks like a great addition to the caching/performance side of things - thanks... Link to comment Share on other sites More sharing options...
ryan Posted January 25, 2011 Author Share Posted January 25, 2011 Looks like I forgot to update this before, but wanted to mention that this module is now included in the current ProcessWire distribution (and has been for a little while). Link to comment Share on other sites More sharing options...
ceberlin Posted July 16, 2012 Share Posted July 16, 2012 The "more" Link in the modules list is dead for this module. (I am running CW 2.2.2) Link to comment Share on other sites More sharing options...
ryan Posted July 17, 2012 Author Share Posted July 17, 2012 It was a broken redirect. I've fixed the link-- thanks. Link to comment Share on other sites More sharing options...
Biorn Posted July 26, 2012 Share Posted July 26, 2012 I was wondering... Now that the man man from the grim north has extended the image field, can we cache the images?? I could come hande now with all those big image sliders.. Keep it up, Im soon gonna release a lot of stuff.. Bjørn aka ChrisB @ChrisB_dk 1 Link to comment Share on other sites More sharing options...
ryan Posted July 26, 2012 Author Share Posted July 26, 2012 Now that the man man from the grim north has extended the image field, can we cache the images?? I love the way this question is worded, but I don't understand it? Can you expand on the question? Just in case it's related, want to mention that ProcessWire already does cache any image sizes you create with $image->size(), $image->width() or $image->height(). Link to comment Share on other sites More sharing options...
Biorn Posted July 31, 2012 Share Posted July 31, 2012 Sorry for that, I shouldn't post when I havent sleept in 48 hours.. But you answered the right there.. I wanted to cache the images.. And btw, Im loving PW more and more for everyday Im working with it.. Thanks for you hard work.. 1 Link to comment Share on other sites More sharing options...
Michael van Laar Posted November 13, 2012 Share Posted November 13, 2012 A stupid question, but I want to get it right: The module chooses the name, with which the cached markup result snippet can be identified later on, automatically by looking at the if-statement? Link to comment Share on other sites More sharing options...
Soma Posted November 13, 2012 Share Posted November 13, 2012 /** * Save the data to the cache * * Must be preceded by a call to get() so that you have set the cache unique name * * @param string $data Data to cache * @return int Number of bytes written to cache, or FALSE on failure. * */ public function save($data) { if(!$this->cache) throw new WireException("You must attempt to retrieve a cache first, before you can save it."); $result = $this->cache->save($data); $this->cache = null; return $result; } Does this answer you question? 1 Link to comment Share on other sites More sharing options...
Michael van Laar Posted November 13, 2012 Share Posted November 13, 2012 Does this answer you question? OK, RTFM My fault. Link to comment Share on other sites More sharing options...
thomas Posted December 11, 2012 Share Posted December 11, 2012 Hello, is there a method to delete (expire) only selected cache files other than manually unlinking them? Thanks, thomas Link to comment Share on other sites More sharing options...
Wanze Posted December 11, 2012 Share Posted December 11, 2012 You can set the cache time to zero: $cache->get('your_cache_id', 0); 1 Link to comment Share on other sites More sharing options...
thomas Posted December 11, 2012 Share Posted December 11, 2012 Thanks Wanze, unfortunately I needed wildcards so I had to build my own method. I used your tip in another class ten minutes later though Link to comment Share on other sites More sharing options...
valan Posted April 6, 2014 Share Posted April 6, 2014 Is it possible to disable markup cache expiration at API level when I save certain pages? Hook? Could you please share an example? Module settings allow only yes/no setup for the whole system but I'd like not to inform module on several page save events while keeping "expire" for all other pages. Thanks! P.S. I've checked ProCache docs but it looks too big and UI-oriented for this task. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted April 7, 2014 Share Posted April 7, 2014 Look at the code provided by Wanze. Or you could trigger a delete of the cache after Pages::save class DeleteMarkupCache extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'Delete cache module', 'singular' => true, 'autoload' => true ); } public function init() { $this->pages->addHookAfter('save', $this, 'deleteCache'); } public function deleteCache($event) { $page = $event->arguments[0]; if($page->template != 'wanted-template') return; // now do your logic. } } 2 Link to comment Share on other sites More sharing options...
valan Posted April 7, 2014 Share Posted April 7, 2014 May be my request is not clear enough or I don't understand the answer... I don't need to trigger delete of the cache - by default it triggers after any page save. This is a default setting in Markup Cache module. Instead, I need to stop trigger for certain pages. I need to do it at API level in my code, e.g. not in PW core. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted April 8, 2014 Share Posted April 8, 2014 (edited) by default it triggers after any page save. This is a default setting in Markup Cache module. MarkupCache, doesn't get triggered upon page save, it has it's own time schedule to expire. (second parameter) Are you sure you mean MarkupCache and not the Build-in cache (Template Level) ? Thanks Teppo: <advertisement>ProcessWire caching explained</advertisement> Edited April 8, 2014 by Martijn Geerts Link to comment Share on other sites More sharing options...
valan Posted April 8, 2014 Share Posted April 8, 2014 In module settings there is a "yes/no" question: "Expire Markup Chaches when pages are saved?" As far as I understand, if "yes" is enabled then MarkupCache get triggered on page save, and it deletes all cache. I want to keep "yes" but want to stop trigger MarkupCache (=cache deletes) when some specific pages are saved. Link to comment Share on other sites More sharing options...
thomas Posted April 8, 2014 Share Posted April 8, 2014 Yes, normally every MarkupCache expires on any page save. You can disable this and then build your own caching logic with a hook on save() like Martijn suggested. So you tell it when to expire instead of when not to expire ... 1 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted April 8, 2014 Share Posted April 8, 2014 Thanks for pointing that one out. Didn't knew there was that option... Has it been there from the start ? Link to comment Share on other sites More sharing options...
Martijn Geerts Posted April 8, 2014 Share Posted April 8, 2014 I want to keep "yes" but want to stop trigger MarkupCache (=cache deletes) when some specific pages are saved. You can turn that around, should be easier. Set it to "no" and delete the ones you want to expire. Link to comment Share on other sites More sharing options...
valan Posted April 8, 2014 Share Posted April 8, 2014 @thomas, @martin - thanks! Looks like the only option is to disable expire Markup Chaches when pages are saved and install module below. And the way to tell it when not to expire instead of when to expire is as below. class DeleteMarkupCache extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'Delete cache module', 'singular' => true, 'autoload' => true ); } public function init() { $this->pages->addHookAfter('save', $this, 'deleteCache'); } public function deleteCache($event) { $page = $event->arguments[0]; $notExpire = ['template1', 'template2']; if(in_array($page->template, $notExpire)) return; $cache = $modules->get("MarkupCache"); $cache->expire(); } } Link to comment Share on other sites More sharing options...
Martijn Geerts Posted April 8, 2014 Share Posted April 8, 2014 Be aware of variable scoping $modules would be $this->modules For expiring the cache you should look for the right syntax, as MarkupCache files are separate folders, delete able by its name. 1 Link to comment Share on other sites More sharing options...
Hari KT Posted May 14, 2014 Share Posted May 14, 2014 Hi guys, I am experiencing a bit of trouble with the MarkupCache not cached properly. This is the code, $cache = wire()->modules->get("MarkupCache"); if(! $someoperation = $cache->get("someoperation", 60 * 15)) { $someoperation = " I am some operation "; if(! $expensive_operation = $cache->get("expensive_operation", 60 * 60 * 24)) { $expensive_operation = " Some expensive operation "; $cache->save($expensive_operation); } $someoperation .= $expensive_operation; // I am forced to use the get operation again for I have seen if you are not calling the get the value saved is something else $cache->get("someoperation", 0); $cache->save($someoperation); } Do you guys know what may be the reason the cache could not load ? I have tried setting the permission from 766 , 777 etc to the folder site/assets/cache/MarkupCache/ . Not saying this doesn't works always but some times espeically before the time expires or when I do a deploy via Jenkins it could not load the data. I can see the file being created, but it is not loading the data. What is the use of lastgood file ? 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