0

I'm trying to cache an object returned after a 3rd party API call via HTTP. The API charges based on calls, so I'm trying to cache the information returned for some period of time.

However I don't seem to be able to get cache_set() to ever store anything. I've enabled "Cache pages for anonymous users" and "Cache blocks" in the performance configuration, as I read somewhere that the cache API may depend on those being set.

I'm using code inside a function called in hook_block_view():

$cache_key = 'custom_module'.$index;

$conditions = cache_get($cache_key);

if (!$conditions) { $conditions = API_CALL($index); cache_set($cache_key, $conditions, 'cache', REQUEST_TIME + (60 * 10)); //stores in cache table and expires after 10 minutes }

However this still results in an API call per page load with testing from a logged out browser. I added some minimal test code:

cache_set('testing','foo','cache',CACHE_PERMANENT);
var_dump(cache_get('testing'));

And the output is "bool(false)".

Looking in the database shows the 'cache' table is empty.

I'm about to start digging through the function calls being cache_set() to see what is happening, and intend to report back what I find. However if this is a common problem perhaps someone already has an answer?

  • Just copy/pasted your code into /devel/php and cache_get('testing')->data == 'foo' as expected. Your cache table shouldn't be empty (unless you're not using db cache) so it might suggest you have a custom/contrib module invalidating the cache on every page load – Clive Nov 26 '14 at 0:25
  • Yep - just had a forehead slap moment. Earlier in the piece I had problems with excess caching, and had configured up "DrupalFakeCache". Had to debug down into the cache_set function to realise it wasn't calling the default cache implementation before I remembered! – Mark Nov 26 '14 at 3:28
0

I'm an idiot, and I had configured up the DrupalFakeCache earlier in development to prevent caching. Clearly this results in no caching.

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.