Jump to content


Photo

New MarkupCache module

Module

  • Please log in to reply
23 replies to this topic

#1 ryan

ryan

    Reiska

  • Administrators
  • 7,797 posts
  • 6572

  • LocationAtlanta, GA

Posted 16 December 2010 - 02:07 PM

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.

#2 martinluff

martinluff

    Full Member

  • Members
  • PipPipPip
  • 79 posts
  • 2

  • LocationChristchurch NZ

Posted 24 January 2011 - 11:13 PM

Looks like a great addition to the caching/performance side of things - thanks...

#3 ryan

ryan

    Reiska

  • Administrators
  • 7,797 posts
  • 6572

  • LocationAtlanta, GA

Posted 25 January 2011 - 10:00 AM

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

#4 ceberlin

ceberlin

    Distinguished Member

  • Members
  • PipPipPipPip
  • 132 posts
  • 41

  • LocationHamburg & Berlin (Germany)

Posted 16 July 2012 - 04:37 PM

The "more" Link in the modules list is dead for this module. (I am running CW 2.2.2)

#5 ryan

ryan

    Reiska

  • Administrators
  • 7,797 posts
  • 6572

  • LocationAtlanta, GA

Posted 17 July 2012 - 09:37 AM

It was a broken redirect. I've fixed the link-- thanks.

#6 Biorn

Biorn

    Distinguished Member

  • Members
  • PipPip
  • 28 posts
  • 9

  • LocationOdense, Denmark

Posted 26 July 2012 - 01:42 AM

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
Best regards

Biorn Christiansen
ChrisB.dk
Bredstedgade 6 st th
DK 5000 Odense C

#7 ryan

ryan

    Reiska

  • Administrators
  • 7,797 posts
  • 6572

  • LocationAtlanta, GA

Posted 26 July 2012 - 08:07 AM

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

#8 Biorn

Biorn

    Distinguished Member

  • Members
  • PipPip
  • 28 posts
  • 9

  • LocationOdense, Denmark

Posted 31 July 2012 - 11:48 AM

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

Biorn Christiansen
ChrisB.dk
Bredstedgade 6 st th
DK 5000 Odense C

#9 Michael van Laar

Michael van Laar

    Distinguished Member

  • Members
  • PipPip
  • 42 posts
  • 27

  • LocationFürth, Germany

Posted 13 November 2012 - 08:52 AM

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?

#10 Soma

Soma

    Hero Member

  • Moderators
  • 5,055 posts
  • 3821

  • LocationSH, Switzerland

Posted 13 November 2012 - 08:59 AM

/**
* 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?

@somartist | modules created | support me, flattr my work flattr.com


#11 Michael van Laar

Michael van Laar

    Distinguished Member

  • Members
  • PipPip
  • 42 posts
  • 27

  • LocationFürth, Germany

Posted 13 November 2012 - 09:20 AM

Does this answer you question?


OK, RTFM :wacko: My fault.

#12 thomas

thomas

    Distinguished Member

  • Members
  • PipPipPipPip
  • 129 posts
  • 37

  • LocationKöln, Germany

Posted 11 December 2012 - 04:23 AM

Hello,
is there a method to delete (expire) only selected cache files other than manually unlinking them?
Thanks,
thomas

#13 Wanze

Wanze

    Hero Member

  • Members
  • PipPipPipPipPip
  • 637 posts
  • 758

  • LocationBern, Switzerland

Posted 11 December 2012 - 04:29 AM

You can set the cache time to zero:

$cache->get('your_cache_id', 0);


#14 thomas

thomas

    Distinguished Member

  • Members
  • PipPipPipPip
  • 129 posts
  • 37

  • LocationKöln, Germany

Posted 11 December 2012 - 06:42 AM

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 :)

#15 valan

valan

    Distinguished Member

  • Members
  • PipPipPip
  • 94 posts
  • 21

  • LocationRussia, Moscow

Posted 06 April 2014 - 06:59 PM

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.



#16 Martijn Geerts

Martijn Geerts

    Hero Member

  • Members
  • PipPipPipPipPip
  • 1,351 posts
  • 1179

  • LocationThe Netherlands

Posted 07 April 2014 - 08:36 AM

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


#17 valan

valan

    Distinguished Member

  • Members
  • PipPipPip
  • 94 posts
  • 21

  • LocationRussia, Moscow

Posted 07 April 2014 - 05:44 PM

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.



#18 Martijn Geerts

Martijn Geerts

    Hero Member

  • Members
  • PipPipPipPipPip
  • 1,351 posts
  • 1179

  • LocationThe Netherlands

Posted 08 April 2014 - 12:51 AM

 

 

 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, 08 April 2014 - 08:41 AM.


#19 valan

valan

    Distinguished Member

  • Members
  • PipPipPip
  • 94 posts
  • 21

  • LocationRussia, Moscow

Posted 08 April 2014 - 06:21 AM

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.



#20 thomas

thomas

    Distinguished Member

  • Members
  • PipPipPipPip
  • 129 posts
  • 37

  • LocationKöln, Germany

Posted 08 April 2014 - 06:59 AM

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







Also tagged with one or more of these keywords: Module

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users