4

When developing an extension, I'd like to cache some data to avoid the expense of re-calculating it. Is there a civicrm version of the Drupal's caching interface for developers?

4

I found the corresponding reference page here:

https://docs.civicrm.org/dev/en/latest/framework/cache/

Looks straightforward:

  1. Set a cache value with Civi::cache()->set('mykey', 'myvalue');
  2. Get a cached value with Civi::cache()->get('mykey');
  3. Delete a cached value with Civi::cache()->delete('mykey');
  4. Flush the entire cache with Civi::cache()->flush();

As far as I know, the civicrm caching schema is pretty simple, there's just one key/value table.

As an aside - I just discovered that Herb @ freeform has contributed a recent patch for using Redis as the cache backend. I'm a Redis fan also, so thanks Herb!

  • So Civi::cache is an in-memory cache? It's a bit confusing since I saw the civicrm_cache table and would expect the cache to use that. I was hoping to use this to persist data between requests instead of implementing it myself. – mickadoo Dec 11 '17 at 15:57
  • No, the caching mechanism is abstracted and persistent - it might be using Redis for performance, but the canonical implementation is indeed that civicrm_cache table. But it's not designed for user session data, it's for configuration type data. – Alan Dixon Dec 12 '17 at 15:34

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.