TomPich Posted April 7 Share Posted April 7 Hello guys, I’d like to dig a little bit in the $cache API. In my homepage template, I call data from on other page, in repeater field. Although it has only 16 items, I noticed that it slowed down the loading process. When I use the cache a template level, the page is definitely faster. In the homepage template, I do the following : $consultants = $pages->get(1027)->repeaterConsultants; foreach ($consultants as $consultant) { // do stuff with $consultant->consultantPortrait, $consultant->consultantName, etc... } I tried to put in cache the $consultants variable. I read a tutorial on processwire.dev and I read the docs but I can’t get it working... The test below work perfectly though. $test = $cache->get("test", 3600, function(){return "this is a test";}); var_dump($test); So how could I cache the $consultants variable? Thanks. Link to comment Share on other sites More sharing options...
zoeck Posted April 8 Share Posted April 8 Did you use the "$cache->save()" method? So something like this: $cache->save("test", $consultants, 3600); // get single cache value $str = $cache->get('test'); 1 Link to comment Share on other sites More sharing options...
ryan Posted April 8 Share Posted April 8 @TomPich I wouldn't recommend caching a field value like that, as I don't think you'll get any performance benefit since the amount of work for PW to do is the same either way there. What I'd recommend doing instead is caching the resulting output, which also means only loading the repeaterConsultants when it will be used to generate that cached markup. Whatever markup you generate, use that as your cache value, for example: echo $cache->get('consultants', 'hourly', function() use($pages) { $out = ''; $consultants = $pages->get(1027)->repeaterConsultants; foreach ($consultants as $consultant) { $out .= "<li>$consultant->consultantName</li>"; } return "<ul>$out</ul>" ; }); 2 1 Link to comment Share on other sites More sharing options...
TomPich Posted April 8 Author Share Posted April 8 Thanks a lot! I’ll try your solution, Ryan. I didn’t think about caching a prerendered html string, but it seems perfectly logic ? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now