缓存get方法支持匿名函数设置缓存值

This commit is contained in:
TOP糯米 2019-08-31 19:51:43 +08:00
parent bcb2ab2a61
commit 139e845cc0
3 changed files with 27 additions and 12 deletions

View File

@ -60,7 +60,7 @@ class File implements CacheIfs
* @param int $timeout * @param int $timeout
* @return bool * @return bool
*/ */
public function set($key = '', $value = '', $timeout = 10) public function set($key, $value, $timeout = 10)
{ {
$this->createCacheDir(); $this->createCacheDir();
$filename = $this->getFileName($key); $filename = $this->getFileName($key);
@ -77,16 +77,23 @@ class File implements CacheIfs
/** /**
* 获取缓存 * 获取缓存
* @param string $key * @param string $key
* @param null $callable
* @return bool|false|string * @return bool|false|string
*/ */
public function get($key = '') public function get($key = null, $callable = null)
{ {
$filename = $this->getFileName($key); $filename = $this->getFileName($key);
// 如果缓存文件存在
if (file_exists($filename)) { if (file_exists($filename)) {
// 判断文件是否有效
if ($this->isTimeOut($key)) { if ($this->isTimeOut($key)) {
// 返回缓存数据
return $this->getCacheContent($key); return $this->getCacheContent($key);
} }
return false; }
// 如果缓存不存在或缓存无效并且存在callable
if (is_callable($callable)) {
return $callable($this);
} }
return false; return false;
} }
@ -96,7 +103,7 @@ class File implements CacheIfs
* @param string $key * @param string $key
* @return bool * @return bool
*/ */
public function remove($key = '') public function remove($key = null)
{ {
$filename = $this->getFileName($key); $filename = $this->getFileName($key);
if (file_exists($filename)) { if (file_exists($filename)) {
@ -149,7 +156,7 @@ class File implements CacheIfs
$mtime = filemtime($filename); $mtime = filemtime($filename);
if ($timeout == 0) { if ($timeout == 0) {
return true; return true;
} elseif ((time() - $mtime > $timeout)) { } elseif ((time() - $mtime >= $timeout)) {
// 已超时,删除缓存 // 已超时,删除缓存
$this->remove($key); $this->remove($key);
return false; return false;

View File

@ -53,7 +53,7 @@ class Redis implements CacheIfs
* @param int $timeout * @param int $timeout
* @return bool * @return bool
*/ */
public function set($key = '', $value = '', $timeout = 0) public function set($key, $value, $timeout = 10)
{ {
if (is_array($value) || is_object($value)) { if (is_array($value) || is_object($value)) {
$value = json_encode($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 * @return bool|mixed|string
*/ */
public function get($key = '') public function get($key = null, $callable = null)
{ {
$value = $this->redis->get($key); $value = $this->redis->get($key);
// 如果获取不到结果但是callable存在
if ($value === false && is_callable($callable)) {
return $callable($this);
}
// 判断值是否是json字符串
$jsonDecode = json_decode($value, true); $jsonDecode = json_decode($value, true);
if (is_null($jsonDecode)) { if (is_null($jsonDecode)) {
// 原始数据
return $value; return $value;
} }
// 返回转换后的数据
return $jsonDecode; return $jsonDecode;
} }
@ -82,7 +90,7 @@ class Redis implements CacheIfs
* @param string $key * @param string $key
* @return int * @return int
*/ */
public function remove($key = '') public function remove($key = null)
{ {
return $this->redis->del($key); return $this->redis->del($key);
} }

View File

@ -5,9 +5,9 @@ namespace top\library\cache\ifs;
interface CacheIfs 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);
} }