Jump to content


Photo

New MarkupCache module

Module

  • Please log in to reply
13 replies to this topic

#1 ryan

ryan

    Hero Member

  • Administrators
  • 5,773 posts
  • 3122

  • 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

    Hero Member

  • Administrators
  • 5,773 posts
  • 3122

  • 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
  • PipPip
  • 29 posts
  • 7

  • 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

    Hero Member

  • Administrators
  • 5,773 posts
  • 3122

  • 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

    Hero Member

  • Administrators
  • 5,773 posts
  • 3122

  • 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
  • 3,194 posts
  • 1750

  • 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
  • 115 posts
  • 33

  • 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

    Sr. Member

  • Members
  • PipPipPipPip
  • 357 posts
  • 372

  • 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
  • 115 posts
  • 33

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





Also tagged with one or more of these keywords: Module

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users