ZF-3326: It is impossible to cache FALSE values with code snippets in Zend_Cache docs
Description
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
}
Comments
Posted by Fabien MARTY (fab) on 2008-05-26T09:47:15.000+0000
duplicate with #ZF-2511 (but thanks)
Posted by Wojciech Naruniec (wojciech) on 2008-05-26T11:12:03.000+0000
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.
Posted by Fabien MARTY (fab) on 2008-05-28T09:47:58.000+0000
You are right
Posted by Marc Bennewitz (GIATA mbH) (mben) on 2008-05-29T14:59:16.000+0000
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.
Posted by Marc Bennewitz (GIATA mbH) (mben) on 2008-05-29T15:09:40.000+0000
example to my last comment:
because the null value is not a cacheable data for practise
Posted by Fabien MARTY (fab) on 2008-05-30T10:57:58.000+0000
I dont' want to break the API (even it would be a minor change)
Posted by Wojciech Naruniec (wojciech) on 2008-05-30T11:48:29.000+0000
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.
Posted by Fabien MARTY (fab) on 2008-05-30T13:18:36.000+0000
ok for the documentation and sample code
Posted by Fabien MARTY (fab) on 2008-06-20T12:53:28.000+0000
fixed in SVN trunk (thanks)
Posted by Wil Sinclair (wil) on 2008-09-02T10:39:01.000+0000
Updating for the 1.6.0 release.