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 File Backend queryKeys and directories

Hello. I have the following code as my cache

    $frontCache = new \Phalcon\Cache\Frontend\Data(array(
        "lifetime" => 86400
    ));
    $cache = new \Phalcon\Cache\Backend\File($frontCache, array(
        "cacheDir" => ROOT_PATH . '/var/cache/'
    ));

    $cacheKey = 'json/my_json.' . $key;
    $json = $cache->get($cacheKey);
    if (is_null($json)) {
        $json = make_json($key);

        $cache->save($cacheKey, $json);
    }

It works perfectly and creates files inside the json dir /home/user/www/var/cache/json/json_1 for example but when i try the following code, it always return an empty array

    (...)
    var_dump($cache->queryKeys('json/my_json'));

So, as far as it seems, the $cache->get() and $cache->save() functions can work with subdirectories but the $cache->queryKeys() function cannot. So, is this a bug or i am using the cache service incorrectly?



98.9k

It checks if any file in the cacheDir starts by the given pattern:

https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/cache/backend/file.zep#L233



11.2k
edited Oct '14

Right. And as far as it looks, the only way to access a certain subdirectory is to create a new \Phalcon\Cache\Backend\File object, right?

If so, should I go to GitHub to propose the inclusion of the setOptions() function to \Phalcon\Cache\Backend?



98.9k

What kind of options you have in mind?



11.2k

To change the cacheDir on the run, so i can have an abstract $cache on my DI and use it to DELETE files from var/cache/foo and from var/cache/bar