cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

setting cache lifetime while saving is not working

Open emrecdr opened this issue 13 years ago • 2 comments

http://docs.phalconphp.com/en/latest/api/Phalcon_Cache_BackendInterface.html According to api doc, I should be able to set lifetime via 3rd param while saving cache. But when I tried to store cache for 10 seconds with following code:

$di->set('dataCache', function() use ($config) { $frontend = new Phalcon\Cache\Frontend\Data(array( //'lifetime' => 10 )); $cache = new Phalcon\Cache\Backend\File($frontend, array( "cacheDir" => $config['cacheDir'] )); return $cache; });

public function indexAction() {

  $dataCache = $this->di->get('dataCache');
  $cacheKey = 'robots_order_id.cache';
  $robots    = $dataCache->get($cacheKey);
  if ($robots === null)
  {
     $robots = new stdClass();
     $this->di->get('logger')->notice('robots cache data not exists: '.date("H:i:s"));
     // Store it in the cache
     $dataCache->save($cacheKey, $robots, 10);
  }

}

I saw that script is logging "robots cache data not exists" notice for every second. I am using ver 0.7.0

emrecdr avatar Dec 20 '12 22:12 emrecdr

Setting the lifetime when saving the cache using the Files adapter will not work, that option is intended for adapters like Apc/Memcached that allow this.

In this case you can move the lifetime to the method get($cacheKey, 10)

I will a note to the documentation explaining this special case

ghost avatar Dec 21 '12 01:12 ghost

I am be able to set lifetime while getting cache instead of saving as you advised. Thanks for your answer.

emrecdr avatar Dec 21 '12 21:12 emrecdr