1

I am using Zend Cache in my current project and its working fine .
But I want it to work more fast so I want to check if cache exists before I use load function of zend cache.
Following is my code :

$cache = Zend_Registry::get('cache');

if(!$result = $cache->load('firstfile')) {
    $newArray = 'firstfile';
    $cache->save($newArray, 'firstfile');
} else {

    echo 'retrieving cache data';
    Zend_Debug::dump($result);
}

I read documents of zend cache. It says we only use load this way to check cache exists or not. I want to know is there any other zend cache function available like hasCached or something like so we can use it to check cache exists or not before we use load function.

Thanks in Advance... :)

Yuda Prawira
  • 12,075
  • 10
  • 46
  • 54
Ravi Shah
  • 35
  • 6
  • Is this a bottleneck in your application? If you want to optimise this for the sake of optimising, don't. Xhprof your app first and see whether it's worth optimising at all. – mingos Apr 15 '14 at 12:10
  • hey mingos, thanks for the reply. I want to learn about zend cache, more than the optimizing the project it is about learning and exploring zend, so is there any zend function available where we can check if cache exists or not... – Ravi Shah Apr 15 '14 at 12:13

2 Answers2

1

There is no such function, I'm afraid.

Zend Cache is a wrapper around several other cache mechanisms. I'm not 100% familiar with all of them, e.g. I don't know if Redis or XCache offer a way of checking whether a cache key exists.

APC does not - the key does not exist if attempting to load the cached entry fails.

Memcache and Memcached don't - same as above.

In case of a file cache, checking the existence of a key involves a file lookup, which is slow by itself.

Same applies for the database cache - you'd need to launch a SQL query, which by itself is relatively slow.

If the underlying cache backends don't have a way of checking the existence of a cache key, Zend Cache has no way to offer it other than simply by running the load function under the hood, which simply defeats the purpose of having a hasKey function at all. In case of "slow" cache mechanisms, the lookup would actually slow you down even more.

mingos
  • 23,778
  • 12
  • 70
  • 107
0

The getIds() function return array of stored cache ids.

So I think you can try something like this:

$id_list =  $cache->getIds();
if (in_array('firstfile', $id_list)) {
    $result = $cache->load('firstfile');
}

I have not test it, just an idea. :)

doydoy44
  • 5,720
  • 4
  • 29
  • 45
  • hello doydoy, Thanks for the reply. I tested your code its working perfectly, But I came to know another function of file adapter 'test' which provides us whether that cache exists or not before we save or load it. Thanks..:) – Ravi Shah Apr 15 '14 at 13:19
  • You can optimize my idea with just one call (unless new cache) and put the result in registry. And this method works even if you change the sytem of cache (file, apc, ...) If you have an oher solution, please write it. :) – doydoy44 Apr 15 '14 at 13:24
  • It's not a good idea to check for cache existing and after it start loading because between the check and the load the cache could be expired or otherwise removed by another process. Btw. ``getIds`` returns an array of all cached ids which could be very very slow and it's not supported by all backends. – mabe.berlin Aug 14 '14 at 19:43