Jump to content
Adam Kiss

How to take advantage of Markup Cache module

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

Share this post


Link to post
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.

Share this post


Link to post
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 :)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Thanks Ryan :)

Is there any API call to purge (/delete/invalidate) cache? If I'll use this as a part of module, I can't rely on site administrator setting up things properly :)

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

You can do this:

$cache->removeAll(); 

If you want to remove an individual cache, you'd want to do what Pete suggested.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites

@jeroen,

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

Good to see you here.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

I use MarkupCache to cache JSON data all the time so I'll definitely take a look at your code, Jeroen. Thanks

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...