From 6eefcc061ad185a6d1281b9ef6940754c0f564f3 Mon Sep 17 00:00:00 2001 From: byends Date: Wed, 10 Jul 2013 23:09:33 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=BC=8F=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cache/File.php | 122 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 Cache/File.php diff --git a/Cache/File.php b/Cache/File.php new file mode 100644 index 0000000..f68e48a --- /dev/null +++ b/Cache/File.php @@ -0,0 +1,122 @@ + + * @license GNU General Public License 2.0 + */ +class File implements CacheInterface +{ + /** + * 缓存路径 + */ + private $_cacheDir; + + /** + * 有效时间 + */ + private $_lifeTime; + + /** + * @param $cacheDir + * @param int $lifeTime (0 || null 表示永不过期,默认900秒,15分钟) + */ + public function __construct($cacheDir, $lifeTime = 900) + { + $this->_cacheDir = rtrim($cacheDir, '/').'/'; + $this->_lifeTime = $lifeTime; + } + + /** + * 获取原始对象 + * + * @return mixed + */ + public function getCache() + {} + + /** + * 设置缓存 + * + * @param string $key + * @param string $data + */ + public function set($key, $data) + { + $data = array( + /** 记录时效 */ + 'II' => pack('II', $this->_lifeTime, time()), + 'data' => $data + ); + + $content = ''; + + /** 写入缓存 */ + file_put_contents($this->_cacheDir . $key . '.php', $content, LOCK_EX); + + /** 释放内存 */ + unset($data, $content); + } + + /** + * 获取缓存 + * + * @param string $key + * @return string + */ + public function get($key) + { + $path = $this->_cacheDir . $key . '.php'; + $cache = array(); + + if (file_exists($path)) { + $cache = include $path; + } + + if ($cache) { + $tmp = isset($cache['II']) && $cache['II'] ? unpack('Il/IL', $cache['II']) : ''; + + /** 检测时效性 */ + if ($tmp && (!$tmp['l'] || (time() - $tmp['L'] <= $tmp['l']))) { + return $cache['data']; + } + } + + /** 清除已经过时的缓存 */ + @unlink($path); + return ''; + } + + /** + * 获取多个缓存 + * + * @param array $keys + * @return array + */ + public function getMultiple(array $keys) + { + $cache = array(); + foreach ($keys as $v) { + $cache[$v] = $this->get($v); + } + + return $cache; + } + + /** + * 删除缓存 + * + * @param string $key + */ + public function remove($key) + { + $path = $this->_cacheDir . $key . '.php'; + @unlink($path); + } +} From 84f9098d11a811467c61b6ecbb636a443f9659fd Mon Sep 17 00:00:00 2001 From: byends Date: Mon, 15 Jul 2013 12:02:12 +0800 Subject: [PATCH 2/3] Update File.php Code formatting --- Cache/File.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cache/File.php b/Cache/File.php index f68e48a..28f0b7f 100644 --- a/Cache/File.php +++ b/Cache/File.php @@ -11,7 +11,7 @@ */ class File implements CacheInterface { - /** + /** * 缓存路径 */ private $_cacheDir; From f12bb8d07e7ca8d600a5ff212cc2423a27fb254f Mon Sep 17 00:00:00 2001 From: byends Date: Mon, 15 Jul 2013 13:48:05 +0800 Subject: [PATCH 3/3] Update File.php code formatting ag --- Cache/File.php | 208 ++++++++++++++++++++++++------------------------- 1 file changed, 104 insertions(+), 104 deletions(-) diff --git a/Cache/File.php b/Cache/File.php index 28f0b7f..59b4bf2 100644 --- a/Cache/File.php +++ b/Cache/File.php @@ -3,120 +3,120 @@ namespace TE\Cache; /** - * 文件式缓存 - * + * 文件式缓存 + * * @uses CacheInterface - * @author Byends + * @author Byends * @license GNU General Public License 2.0 */ class File implements CacheInterface { - /** - * 缓存路径 - */ - private $_cacheDir; - - /** - * 有效时间 - */ - private $_lifeTime; + /** + * 缓存路径 + */ + private $_cacheDir; + + /** + * 有效时间 + */ + private $_lifeTime; /** * @param $cacheDir * @param int $lifeTime (0 || null 表示永不过期,默认900秒,15分钟) */ public function __construct($cacheDir, $lifeTime = 900) - { - $this->_cacheDir = rtrim($cacheDir, '/').'/'; - $this->_lifeTime = $lifeTime; - } - - /** - * 获取原始对象 - * - * @return mixed - */ - public function getCache() - {} - - /** - * 设置缓存 - * - * @param string $key - * @param string $data - */ - public function set($key, $data) - { - $data = array( + { + $this->_cacheDir = rtrim($cacheDir, '/').'/'; + $this->_lifeTime = $lifeTime; + } + + /** + * 获取原始对象 + * + * @return mixed + */ + public function getCache() + {} + + /** + * 设置缓存 + * + * @param string $key + * @param string $data + */ + public function set($key, $data) + { + $data = array( /** 记录时效 */ - 'II' => pack('II', $this->_lifeTime, time()), - 'data' => $data - ); - - $content = ''; - - /** 写入缓存 */ - file_put_contents($this->_cacheDir . $key . '.php', $content, LOCK_EX); - - /** 释放内存 */ - unset($data, $content); - } - - /** - * 获取缓存 - * - * @param string $key - * @return string - */ - public function get($key) - { - $path = $this->_cacheDir . $key . '.php'; - $cache = array(); - - if (file_exists($path)) { - $cache = include $path; - } - - if ($cache) { - $tmp = isset($cache['II']) && $cache['II'] ? unpack('Il/IL', $cache['II']) : ''; - - /** 检测时效性 */ - if ($tmp && (!$tmp['l'] || (time() - $tmp['L'] <= $tmp['l']))) { - return $cache['data']; - } - } - - /** 清除已经过时的缓存 */ - @unlink($path); - return ''; - } - - /** - * 获取多个缓存 - * - * @param array $keys - * @return array - */ - public function getMultiple(array $keys) - { - $cache = array(); - foreach ($keys as $v) { - $cache[$v] = $this->get($v); - } - - return $cache; - } - - /** - * 删除缓存 - * - * @param string $key - */ - public function remove($key) - { - $path = $this->_cacheDir . $key . '.php'; - @unlink($path); - } + 'II' => pack('II', $this->_lifeTime, time()), + 'data' => $data + ); + + $content = ''; + + /** 写入缓存 */ + file_put_contents($this->_cacheDir . $key . '.php', $content, LOCK_EX); + + /** 释放内存 */ + unset($data, $content); + } + + /** + * 获取缓存 + * + * @param string $key + * @return string + */ + public function get($key) + { + $path = $this->_cacheDir . $key . '.php'; + $cache = array(); + + if (file_exists($path)) { + $cache = include $path; + } + + if ($cache) { + $tmp = isset($cache['II']) && $cache['II'] ? unpack('Il/IL', $cache['II']) : ''; + + /** 检测时效性 */ + if ($tmp && (!$tmp['l'] || (time() - $tmp['L'] <= $tmp['l']))) { + return $cache['data']; + } + } + + /** 清除已经过时的缓存 */ + @unlink($path); + return ''; + } + + /** + * 获取多个缓存 + * + * @param array $keys + * @return array + */ + public function getMultiple(array $keys) + { + $cache = array(); + foreach ($keys as $v) { + $cache[$v] = $this->get($v); + } + + return $cache; + } + + /** + * 删除缓存 + * + * @param string $key + */ + public function remove($key) + { + $path = $this->_cacheDir . $key . '.php'; + @unlink($path); + } }