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\Data lifetime options being ignored?

Using modelsCache I want to cache common queries in Production, but disable this cache in Development mode. Tried to set Lifetime to 1 second, but queries are still NOT run and delivered from cache. Any ideas why this parameter is totaly ignored?

// Service definition
$di->set('modelsCache', function() use ($config) {
    $cacheLifetime = $config->debug ? 1 : 7200;  // 2h in Prod, 1 sec in Dev
    $frontCache = new \Phalcon\Cache\Frontend\Data(['lifetime' => $cacheLifetime]);

    // Dumping $frontCache->getLifetime() gives correctly 1

    return new \Phalcon\Cache\Backend\File($frontCache, [
        'cacheDir' => $config->site->path->cache . 'queries/'
    ]);
});

// Example QueryBuilder usage
$this->modelsManager->createBuilder()
    ...
    ...
    ->getQuery()->cache(['key' => $cacheFile])->execute();

Hey @dschissler, I'm not sure that I understand your question. As I mentioned above:

// Dumping $frontCache->getLifetime() gives correctly 1

yes. lifetime ignore.

  • File backend
  • None Frontend, always ttl = 1


93.7k
Accepted
answer

Thanks to @Izo I found a workaround for the problem. In development mode we can just use the flush() method to clear the cache on every request.

// Models cache
$di->setShared('modelsCache', function() use ($config) {
    $frontCache = new \Phalcon\Cache\Frontend\Data(['lifetime' => 7200]); // 2h
    $cache = new \Phalcon\Cache\Backend\File($frontCache, [
        'cacheDir' => $config->site->path->cache . 'queries/'
    ]);

    // IMPORTANT PART HERE
    if ($config->debug) {
        $cache->flush();
    }

    return $cache;
});


1.9k

thx, its best proposition at the moment ))

Thanks to @Izo I found a workaround for the problem. In development mode we can just use the flush() method to clear the cache on every request.

// Models cache
$di->setShared('modelsCache', function() use ($config) {
   $frontCache = new \Phalcon\Cache\Frontend\Data(['lifetime' => 7200]); // 2h
   $cache = new \Phalcon\Cache\Backend\File($frontCache, [
       'cacheDir' => $config->site->path->cache . 'queries/'
   ]);

   // IMPORTANT PART HERE
   if ($config->debug) {
       $cache->flush();
   }

   return $cache;
});