fuel / core

Fuel PHP Framework - The core of the Fuel v1 framework
http://fuelphp.com
813 stars 345 forks source link

Cache and expiration problem (file driver) #906

Closed mikebranderhorst closed 12 years ago

mikebranderhorst commented 12 years ago

The docs say if you use [expiration = false] param the default expiration is used.

I have the default expiration (config.cache_lifetime) set to 3600 (default FuelPHP setting).

Now when I use [expiration = false], the cache file is still made with inside "expiration":null which is never expire.

\Cache::set('test', '123', false);

Docs: 123 = seconds to expire null = never expire false = default expire

Code: 123 = seconds to expire null = never expire false = never exipre true = default expire empty = true = default expire

I would say the code is correct but the docs is wrong. i think false = no expiration thus same als null.

WanWizard commented 12 years ago

empty === false, not true. and false is default. But the default in the driver is null.

You make the mistake that you assume that the setting in config/config.php is used by the cache class.

It isn't, the cache class has it's own config file in which the default expiration is set to null. So the docs are correct,

mikebranderhorst commented 12 years ago

Thx for clarification. I searched for cache.php in fuel/classes/config/, my bad. And I misread get as set public static function get($identifier, $use_expiration = true) Everything works fine now for my prune script:

\Event::register('shutdown', 'Cache::prune');

class Cache extends \Fuel\Core\Cache
{
    public static function prune($cache_dir = null)
    {
        if (\Config::get('cache.driver') != 'file') return false;

        is_dir($cache_dir) or $cache_dir = null;
        $cache_dir or $cache_dir = \Config::get('cache_dir');
        if ( ! $cache_dir) return false;

        foreach (glob(rtrim($cache_dir, '/').'/*.cache') as $file)
        {
            if (preg_match('/"expiration":([0-9]+)/', 
                    file_get_contents($file, false, null, 0, 150), 
                    $match) and $match[1] < time())
            {
                unlink($file);
            }
        }
    }
}