Jump to content

New MarkupCache module


ryan

Recommended Posts

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.

  • Like 9
Link to comment
Share on other sites

  • 1 month later...
  • 1 year later...
  • 2 weeks later...

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

  • Like 1
Link to comment
Share on other sites

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

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..

  • Like 1
Link to comment
Share on other sites

  • 3 months later...
/**
* 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?

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...
  • 1 year later...

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

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.
        }
}
  • Like 2
Link to comment
Share on other sites

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

 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 by Martijn Geerts
Link to comment
Share on other sites

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

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 ...

  • Like 1
Link to comment
Share on other sites

@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

  • 1 month later...

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

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
×
×
  • Create New...