Pregunta

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... :)

¿Fue útil?

Solución 2

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. :)

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top