Forums / Developer / custom caching from within an extension
Matthew Carroll
Thursday 11 March 2010 10:28:26 am
I'm building an ez extension which uses some template operators to look up info on an external website. I don't have control over that site or direct access to the database, so it uses curl to scrape the info and xpaths to analyse.
There are three curl page fetches each time my extension's php is hit, one of which changes every time, the second and third of which change much less often, so could be cached. Is there any way to use ez's caching mechanisms from within my extension to cache the result from that second lookup somehow?
Any pointers to appropriate documentation or similar example to work from would be really appreciated.
ThanksMatthew
http://carroll.org.uk
Gaetano Giunta
Thursday 11 March 2010 1:04:43 pm
- create a template fetch function that does the ws calls
- put cache blocks around those calls in the template
Principal Consultant International Business Member of the Community Project Board
Thursday 11 March 2010 2:09:52 pm
Thanks! That was the nudge I needed to see the problem from a new angle.
Currently it does all three external fetches within the one operator function, but if I extract some of the logic out to the ez template instead, and put the second and third lookups in a separate function, it will be more efficient overall.
I was trying to imagine taking the caching into the php of the extension, when what I need to do is bring a small portion of the extension logic out to the ez template. I just wasn't thinking about the problem the right way (isn't that always the challenge!)
Thursday 11 March 2010 7:48:41 pm
Unfortunately the approach I thought would work doesn't seem to.
I took out the second two external website lookups and put them into a template fetch function, but I need to be able to access the result of that fetch as a variable later on. If I do this:
{cache-block expiry=86400} {def $mylist=fetch( 'mplookup', 'list', hash())} {/cache-block}
...it doesn't work the second time, as the variable $mylist doesn't exist once that block is cached. Does that make sense? I want to cache the result of the fetch, but still be able to access the variable next time that template is used (with a different view parameter). Am I missing something obvious here?
Friday 12 March 2010 12:29:13 am
No, it is the way cache-blocks work: the actual html is stored in the file on disk, no template-level variable.
Workarounds:
- move the display of the results within the cache-block (nb: cache-blocks can be nested)
- let the cache-block save data into a js variable and use it later down the page for display