Files
nl-admin-api/system/cache/file.php

63 lines
1.2 KiB
PHP
Raw Normal View History

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
*/
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))
{
return null;
}
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-08-08 10:21:35 -05:00
if (time() >= substr($cache, 0, 10)) return $this->forget($key);
2011-06-08 23:45:08 -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
}
}