We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

cache frontend lifetime not working when using Cache

I am using phalcon 2.0.10 instead of 1.3.4. And I've found cache frontend lifetime not working when I used Cache. The cache would expire 3600 sec all the same. And it did not appear in 1.3.4.

Please post a code, which causes your problem.



643
edited Mar '16
    $frontCache = new \Phalcon\Cache\Frontend\Data([
        'lifetime' => 300
    ]);

    //backend
    $cache = new \Phalcon\Cache\Backend\Libmemcached($frontCache, [
        'servers' => [[
            'host' => '127.0.0.1',
            'port' => '11211'
        ]],
        'statsKey' => null
    ]);

    $di->set('modelsCache', $cache, true);

    //use cache when query
    $ret = Model::find([
        'cache' => [
            'key' => 'foo',
            'service' => 'modelsCache'
        ]
    ]);

Normally, the it will cache 300 sec. But actually, it's 3600 sec.

edited Mar '16

BackendCache in Phalcon internaly uses _lastLifetime attibute where it stores last lifetime set in start method

https://github.com/phalcon/cphalcon/blob/master/phalcon/cache/backend/libmemcached.zep#L205 https://github.com/phalcon/cphalcon/blob/master/phalcon/cache/backend.zep#L97

Your best way to go is to set lifetime internally in your find



643

I just want to set a public lifetime by this way. And I think the problem is caused by https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/model/query.zep#L3268

And compare with this https://github.com/phalcon/cphalcon/blob/1.3.4/ext/mvc/model/query.c#L4775

You will know what I want.

Yeah, you are right, problem is in file

https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/model/query.zep#L3268

You have to submit an issue on github to fix it.



643
edited May '16

I hit the wall with this issue as well today.

The thing is, \Phalcon\Cache\Frontend\Data just does not respect your TTL or Lifetime flags.

If you dive into Zep code here: https://github.com/phalcon/cphalcon/blob/master/phalcon/cache/frontend/data.zep#L63 It's is clear that this class is half implemented.

Update - it seems that it does respect lifetime afterall! If I set it to 10 sec, it will expire and whoo-hoo re-read data again. But if I put lifetime = 86400 (1 day) it will not refresh it. I'm still digging into it to be really sure whenever this is a bug or just some gremlin from my code base / use case scenario.

edited Oct '16

Is zero for never expire?? or -1 ???