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

Issue Type: Docs: Problem Created: 2008-05-26T07:22:08.000+0000 Last Updated: 2008-09-02T10:39:01.000+0000 Status: Resolved Fix version(s): - 1.6.0 (02/Sep/08)

Reporter: Wojciech Naruniec (wojciech) Assignee: Fabien MARTY (fab) Tags: - Zend_Cache

Related issues: Attachments:


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:

<pre class="highlight">$result = $cache->get($cacheId);

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

Nor this:

<pre class="highlight">

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

This code will work as expected:

<pre class="highlight">if(false === $cache->test($cacheId)) {
    // cache doesn't exist


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:

    <pre class="highlight">
    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



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.



Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.