Jump to content

How to take advantage of Markup Cache module


Adam Kiss
 Share

Recommended Posts

Hey Ryan, could you write a little on how to start with MarkupCache please?

How it is used, what are the situations you want to use it and such;

I for instance would like this to use with textformatter: I am running quite heavy processing on parts of page content, and would like to cache the result, so I don't have to rerun it again, but I am not sure, where goes what with MarkupCache.

Thank you

  • Like 1
Link to comment
Share on other sites

Adam did that page Soma linked to solve your question? There isn't much to the MarkupCache module: just $cache->get('your-unique-name', $num_seconds); and $cache->save($your_markup). Let me know if you have any other questions on how to use it.

Link to comment
Share on other sites

Yeah, it did... partly.

Mainly:

  • Is the cache tied to page, or do I have to supply the differentiation
  • Does it work only for string (HTML), or can I supply also objects (and it'll take care of serialisation?)
  • Is the cache autodeleted? Meaning: do I only check for presence (if I want to save CPU time)
  • How do I invalidate cache (upon Page save, for instance)
  • Is this available for modules?

Thanks :)

Link to comment
Share on other sites

The cache is not connected with any page. The only thing that identifies it is the name that you provide for it in the $cache->get('name-of-cache', $seconds); call.

It only works for strings. It's intended to cache markup (HTML) per the name MarkupCache. :) But I don't see any reason why you couldn't serialize an object to a string and cache it with MarkupCache, but you'd have to do the serialization yourself.

When the cache is cleared is determined by the duration you specify (in seconds) as the second param to the $cache->get() call. You can also configure it to clear on every page save in the module settings (Admin > Modules > Markup > Cache). There is also a manual cache clear toggle there as well.

Modules, templates and core in ProcessWire all use the same stuff, so there's no limits there. You could certainly use it from a module if you wanted to. But get() your cache only when you intend to output it or save it right there, because cache calls can't be nested.

  • Like 1
Link to comment
Share on other sites

I think you could just invalidate it by setting the expiry to zero:

$cache->get('your-unique-name', 0);

but I'm not sure whether that would remove any cache files if that's what you're looking for?

Link to comment
Share on other sites

  • 6 months later...

I know this is an old thread, but it was one of the first results when I searched for 'ProcessWire Object Cache'.

I wanted to store a lot of objects in the cache, so I just converted the MarkupCache module to an ObjectCache module. Nothing fancy; it just adds (un)serialize to the get/save methods. But that alone saves me quite a but of time in this project and results in cleaner (less logic) templates.

The code is available for free (and at your own risk :-)) at https://gist.github.com/jkenters/5019089

  • Like 5
Link to comment
Share on other sites

Very cool Jeroen. Can you tell us more about how you are using it? Would love to hear more about the use cases and benefits. Maybe it's something that belongs as an option in MarkupCache. 

The markup depends on a few factors (page the user is on, current time, etc) so caching the data as a PHP array saves quite a bit of database lookups and even some processing (like price calculations etc), while still allowing me to do the needed markup changes per request. Regular markup cache would not work in this case or would require many markup caches per page.

@jeroen,

You've a familiar name. Think I recognize your name from some dutch MODX meeting or something.

Good to see you here.

Still using MODX (I'm even a "MODX Ambassador") so maybe we indeed met at a meetup or MODXpo in Utrecht last year. Having both MODX and ProcessWire really makes web development fun. Both are great systems to work with. :-)

  • Like 1
Link to comment
Share on other sites

The markup depends on a few factors (page the user is on, current time, etc) so caching the data as a PHP array saves quite a bit of database lookups and even some processing (like price calculations etc), while still allowing me to do the needed markup changes per request. Regular markup cache would not work in this case or would require many markup caches per page.

Sounds good, makes sense to me. I will put in a todo in MarkupCache to support the serialize/unserialize where appropriate. 

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...