2011-07-29 23:17:57 -05:00
|
|
|
<?php namespace System\Cache;
|
2011-06-08 23:45:08 -05:00
|
|
|
|
2011-07-29 23:17:57 -05:00
|
|
|
class File implements Driver {
|
2011-06-08 23:45:08 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if an item exists in the cache.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function has($key)
|
|
|
|
|
{
|
|
|
|
|
return ( ! is_null($this->get($key)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an item from the cache.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param mixed $default
|
|
|
|
|
* @return mixed
|
2011-06-18 16:44:34 -05:00
|
|
|
*/
|
2011-07-07 07:40:42 -07:00
|
|
|
public function get($key)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-07-26 22:11:16 -05:00
|
|
|
if ( ! file_exists(CACHE_PATH.$key))
|
2011-06-18 16:44:34 -05:00
|
|
|
{
|
2011-07-07 07:40:42 -07:00
|
|
|
return null;
|
2011-06-18 16:44:34 -05:00
|
|
|
}
|
|
|
|
|
|
2011-07-26 22:11:16 -05:00
|
|
|
$cache = file_get_contents(CACHE_PATH.$key);
|
2011-06-08 23:45:08 -05:00
|
|
|
|
2011-08-02 11:57:01 -05:00
|
|
|
// The cache expiration date is stored as a UNIX timestamp at the beginning
|
|
|
|
|
// of the cache file. We'll extract it out and check it here.
|
2011-06-18 16:44:34 -05:00
|
|
|
if (time() >= substr($cache, 0, 10))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-07-07 07:41:39 -07:00
|
|
|
$this->forget($key);
|
|
|
|
|
|
|
|
|
|
return null;
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2011-07-09 22:15:42 -05:00
|
|
|
return unserialize(substr($cache, 10));
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write an item to the cache.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @param int $minutes
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function put($key, $value, $minutes)
|
|
|
|
|
{
|
2011-07-26 22:11:16 -05:00
|
|
|
file_put_contents(CACHE_PATH.$key, (time() + ($minutes * 60)).serialize($value), LOCK_EX);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete an item from the cache.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function forget($key)
|
|
|
|
|
{
|
2011-07-26 22:11:16 -05:00
|
|
|
@unlink(CACHE_PATH.$key);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|