Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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();


44.6k

Is the lifetime the default value or the maximum value?

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


62.6k
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;
});