samsoir / kohana-cache Goto Github PK
View Code? Open in Web Editor NEWThis project forked from kohana/cache
Cache library for Kohana 3
This project forked from kohana/cache
Cache library for Kohana 3
// If file does not exist
if ( ! $file->isFile())
{
// Return default value
return $default;
}
else
{
// Open the file and extract the json
$json = $file->openFile()->current();
// Decode the json into PHP object
$data = json_decode($json);
// Test the expiry
if ($data->expiry < time())
{
// Delete the file
$this->_delete_file($file, NULL, TRUE);
// Return default value
return $default;
}
else
{
return ($data->type === 'string') ? $data->payload : unserialize($data->payload);
}
}
There is a chance to read the file contents while another thread is writing to a file.
I've caught that just once for 2 years, and this is how the exception looked like:
Kohana_Cache_Exception [ 0 ]: Kohana_Cache_File::get failed to unserialize cached object with message : Trying to get property of non-object ~ MODPATH/cache/classes/kohana/cache/file.php [ 170 ]
So I think that the file was written only partially and $json contained malformed json, what leaded to a NULL
in $data
.
Proposed solution - add if (!$data) return $default;
after decoding
$lifetime
is always set to 0 if not provided in the set() method when using the memcached driver. By changing $lifetime = 0
to $lifetime = Arr::get($this->_config, 'default_expire', Cache::DEFAULT_EXPIRE);
in classes/kohana/cache/memcache.php:103 the problem is solved.
What about tags emulation (or native, if possible) support?
I think, this constant is missing on cache.php
const DEFAULT_EXPIRE = 3600;
too many cache driver use this constant, sample:
Arr::get($this->_config, 'default_expire', Cache::DEFAULT_EXPIRE)
I use kohana 3.0.4.
Declaration of Kohana_Cache_Sqlite::set() should be compatible with that of Kohana_Cache::set()
MODPATH/cache-samsoir\classes\kohana\cache\sqlite.php [ 13 ]
8 * @category Cache
9 * @author Kohana Team
10 * @copyright (c) 2009-2010 Kohana Team
11 * @license http://kohanaphp.com/license
12 */
13 class Kohana_Cache_Sqlite extends Cache implements Kohana_Cache_Tagging {
14
15 /**
16 * Database resource
17 *
18 * @var PDO
Line 209:
$liftime = Cache_Memcache::CACHE_CEILING + time();
Should be:
$lifetime = Cache_Memcache::CACHE_CEILING + time();
default config and file config is have missing APPPATH.
'cache_dir' => 'cache/.kohana_cache',
to
'cache_dir' => 'APPPATH.cache/kohana_cache',
set() method in example sets time()+n for lifetime, while in the implementation function set_with_tags() it adds the lifetime to time() - this doubles up the time()*2
http://github.com/samsoir/kohana-cache/blob/master/classes/cache/sqlite.php#L233
I compare with sqlite driver and find some missing lines and small errors on file driver.
protected function __construct(array $config)
{
parent::__construct($config); // missing, I added
try
{
//$directory = Arr::get($config, 'cache_dir', APPPATH.Cache_File::CACHE_DIR); // wrong, I comment
$directory = Arr::get($this->_config, 'cache_dir', APPPATH.Cache_File::CACHE_DIR); // true, I added
$this->_cache_dir = new RecursiveDirectoryIterator($directory);
}
Hello!
Your plugin is amazing, but there are several problems with file cache.
1)public function set($id, $data, $lifetime = NULL)
lifetime is not used in code, and that would be good..
maybe i missed something, but setting lifetime doesn`t work
2)public function get($id, $default = NULL)
expiration check looks strange
if ($file->getMTime() < (time() - Arr::get($this->_config, 'default_expire', Cache::DEFAULT_EXPIRE)))
as i understand it approves problem#1
3)problem with serialize/unserialize functions in php directly.. it doesn`t work well with strings that contains \r and \n.. maybe use base64 or json instead?
File cache driver:
/**
* Constructs the file cache driver
*
* @param array config
* @throws Kohana_Cache_Exception
*/
protected function __construct(array $config)
{
parent::__construct($config);
try
{
$directory = Arr::get($this->_config, 'cache_dir', APPPATH.Cache_File::CACHE_DIR);
$this->_cache_dir = new RecursiveDirectoryIterator($directory);
}
// You are catching wrong exception here. At least php 5.3 reports so.
catch (ErrorException $e)
{
if ( ! mkdir($directory, 0777, TRUE))
{
throw new Kohana_Cache_Exception('Failed to create the defined cache directory : :directory', array(':directory' => $directory));
}
chmod($directory, 0777);
$this->_cache_dir = new RecursiveDirectoryIterator($directory);
}
Details: http://www.php.net/~helly/php/ext/spl/classUnexpectedValueException.html
Hey,
I've probable made a super headache for you with File Cache driver.
So, http://github.com/samsoir/kohana-cache/blob/master/classes/kohana/cache/file.php#L187
If $data is an array or an object the ErrorException is risen. base64_encode accepts only strings.
I didn't find any solution about that yet, if so we probable need encode and decode methods to handle that correctly. Though if I want to store the object or an array the data shouldn't be encoded.
Changing default driver to sqlite in config file returned me an error in using cache library.
Cache::$instances[$group] passes driver name in $group param, not Kohana_Config configuration. So __construct($config) don't gets needed Kohana_Config, but an array of cache default group.
To initialize $_config property in right way in __construct() method I made this:
protected function __construct($config)
{
$_config = Kohana::config('cache');
$this->_config = $_config->get($config['driver']);
}
What about tags emulation (or native, if possible) support?
I think it would be useful to simply type Cache::instance() instead of knowing which caching class to use specifically
I feel like it would be something that might have to be switched around as traffic builds on your site
maybe allow for specifying which driver to use with a parameter to instance() (default to a config item)... I'm not sure if an app would ever need to use 2 different caching systems
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.