diff --git a/framework/library/cache/driver/File.php b/framework/library/cache/driver/File.php index 4352a5a..476b384 100644 --- a/framework/library/cache/driver/File.php +++ b/framework/library/cache/driver/File.php @@ -60,7 +60,7 @@ class File implements CacheIfs * @param int $timeout * @return bool */ - public function set($key = '', $value = '', $timeout = 10) + public function set($key, $value, $timeout = 10) { $this->createCacheDir(); $filename = $this->getFileName($key); @@ -77,16 +77,23 @@ class File implements CacheIfs /** * 获取缓存 * @param string $key + * @param null $callable * @return bool|false|string */ - public function get($key = '') + public function get($key = null, $callable = null) { $filename = $this->getFileName($key); + // 如果缓存文件存在 if (file_exists($filename)) { + // 判断文件是否有效 if ($this->isTimeOut($key)) { + // 返回缓存数据 return $this->getCacheContent($key); } - return false; + } + // 如果缓存不存在或缓存无效并且存在callable + if (is_callable($callable)) { + return $callable($this); } return false; } @@ -96,7 +103,7 @@ class File implements CacheIfs * @param string $key * @return bool */ - public function remove($key = '') + public function remove($key = null) { $filename = $this->getFileName($key); if (file_exists($filename)) { @@ -149,7 +156,7 @@ class File implements CacheIfs $mtime = filemtime($filename); if ($timeout == 0) { return true; - } elseif ((time() - $mtime > $timeout)) { + } elseif ((time() - $mtime >= $timeout)) { // 已超时,删除缓存 $this->remove($key); return false; diff --git a/framework/library/cache/driver/Redis.php b/framework/library/cache/driver/Redis.php index e4094ee..2990ac4 100644 --- a/framework/library/cache/driver/Redis.php +++ b/framework/library/cache/driver/Redis.php @@ -53,7 +53,7 @@ class Redis implements CacheIfs * @param int $timeout * @return bool */ - public function set($key = '', $value = '', $timeout = 0) + public function set($key, $value, $timeout = 10) { if (is_array($value) || is_object($value)) { $value = json_encode($value); @@ -64,16 +64,24 @@ class Redis implements CacheIfs /** * 获取缓存的值 - * @param string $key + * @param null $key + * @param null $callable * @return bool|mixed|string */ - public function get($key = '') + public function get($key = null, $callable = null) { $value = $this->redis->get($key); + // 如果获取不到结果但是callable存在 + if ($value === false && is_callable($callable)) { + return $callable($this); + } + // 判断值是否是json字符串 $jsonDecode = json_decode($value, true); if (is_null($jsonDecode)) { + // 原始数据 return $value; } + // 返回转换后的数据 return $jsonDecode; } @@ -82,7 +90,7 @@ class Redis implements CacheIfs * @param string $key * @return int */ - public function remove($key = '') + public function remove($key = null) { return $this->redis->del($key); } diff --git a/framework/library/cache/ifs/CacheIfs.php b/framework/library/cache/ifs/CacheIfs.php index f4f0269..520a484 100644 --- a/framework/library/cache/ifs/CacheIfs.php +++ b/framework/library/cache/ifs/CacheIfs.php @@ -5,9 +5,9 @@ namespace top\library\cache\ifs; interface CacheIfs { - public function set($name = '', $value = '', $timeout = null); + public function set($name, $value, $timeout = null); - public function get($name = ''); + public function get($name = null, $callable = null); - public function remove($name = ''); + public function remove($name = null); }