ZF-3326: It is impossible to cache FALSE values with code snippets in Zend_Cache docs


Code snippets in Zend_Cache docs use Zend_Cache_Backend::load() method to test if the cache exists. It is bad practice, because it is impossible to get result when we cache FALSE value. Caching FALSE values may occurs when we cache results of database queries.

This won't work:

$result = $cache->get($cacheId);

if(false === $result) {
    // cache doesn't exist

Nor this:

if(false === ($cache->load($cacheId)) {
    // cache doesn't exist

This code will work as expected:

if(false === $cache->test($cacheId)) {
    // cache doesn't exist


duplicate with #ZF-2511 (but thanks)

Thanks for answer, but I think it is not duplicated issue - I've checked ZF-2511 before I reported.

Check this out, we have cached value:


The only way to check if it is cached is use of Zend_Cache_Backend::test() method. It will return true if $data is cached, and false if is not.

If you are using Zend_Cache_Backend::load() method, it will always return false, beside the $data is cached or not. Using strict operator comparision will not help.

You are right

I think it is a good idea to change the "not cached"-return value of $cache->load() from false to null. If a null value will save it can equal to remove the cache id.

example to my last comment:

if ( ($cache->load($id) === null) ) {
    // ... create / load data
    // if the user dosn't test his created data and he will save a NULL value 
    // then the cache internal calls $cache->remove($id)
    $cache->save($data, $cache);

because the null value is not a cacheable data for practise

I dont' want to break the API (even it would be a minor change)

I think we don't need to break the API. Instead of this we can mention in documentation that the only way to test cache is Zend_Cache_Backend::test() method, and change sample code.

ok for the documentation and sample code

fixed in SVN trunk (thanks)

Updating for the 1.6.0 release.