4

I have a plugin that makes several and potentially slow database calls that eventually return 8 Entries. Is there a way to make use of Craft's built in caching on the plugin side? Using the {% cache %} tag doesn't seem to be doing the trick.

3
  • Are you referring to caching from one of your plugin's templates or caching from PHP code? – Brad Bell Jan 14 '15 at 16:55
  • Caching from PHP code. The plugin does not have a template, just returns a set of Entries from a variable. – Aaron Berkowitz Jan 14 '15 at 20:52
  • Then Justin's answer is spot on. – Brad Bell Jan 14 '15 at 20:53
8

You want to take a look at the CacheService class which is the service that allows you to programmatically interact with the internal cache.

This is an example of how to set some cache that expires in 3600 seconds (one hour). Set the expiration to 0 if you never want it to expire.

craft()->cache->set('some_key', 'some_value', 3600);

Just checkout CacheService for more details, as there are more methods and parameters to make use of, but this should be the basic use case.

CacheService Class Reference

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.