优化缓存类

This commit is contained in:
TOP糯米 2019-09-02 12:47:15 +08:00
parent 6a92e60a77
commit b9579677a6
2 changed files with 70 additions and 53 deletions

View File

@ -21,6 +21,12 @@ class File implements CacheIfs
*/ */
private static $instance; private static $instance;
/**
* 已读取的文件
* @var array
*/
private $files = [];
/** /**
* 默认缓存位置 * 默认缓存位置
* @var null|string * @var null|string
@ -64,11 +70,14 @@ class File implements CacheIfs
{ {
$this->createCacheDir(); $this->createCacheDir();
$filename = $this->getFileName($key); $filename = $this->getFileName($key);
if (is_array($value) || is_object($value)) { $cacheArray = [
$value = json_encode($value); 'create' => time(),
} 'time' => $timeout,
$content = '<?php if (!defined(\'APP_PATH\')) { exit; } $timeout = ' . $timeout . '; ?>' . PHP_EOL . $value; 'value' => $value
];
$content = serialize($cacheArray);
if (file_put_contents($filename, $content)) { if (file_put_contents($filename, $content)) {
$this->files[$key] = $cacheArray;
return true; return true;
} }
return false; return false;
@ -82,17 +91,12 @@ class File implements CacheIfs
*/ */
public function get($key = null, $callable = null) public function get($key = null, $callable = null)
{ {
$filename = $this->getFileName($key); // 判断缓存是否存在
// 如果缓存文件存在 if ($this->exists($key)) {
if (file_exists($filename)) { // 返回缓存数据
// 判断文件是否有效 return $this->getCacheContent($key);
if ($this->isTimeOut($key)) { } elseif (is_callable($callable)) {
// 返回缓存数据 // 如果缓存不存在但是存在callable则调用
return $this->getCacheContent($key);
}
}
// 如果缓存不存在或缓存无效并且存在callable
if (is_callable($callable)) {
return $callable($this); return $callable($this);
} }
return false; return false;
@ -106,7 +110,7 @@ class File implements CacheIfs
public function remove($key = null) public function remove($key = null)
{ {
$filename = $this->getFileName($key); $filename = $this->getFileName($key);
if (file_exists($filename)) { if (is_file($filename)) {
@unlink($filename); @unlink($filename);
} }
return true; return true;
@ -119,7 +123,11 @@ class File implements CacheIfs
*/ */
public function exists($key) public function exists($key)
{ {
return $this->isTimeOut($key); $filename = $this->getFileName($key);
if (is_file($filename) && !$this->timeOut($key)) {
return true;
}
return false;
} }
/** /**
@ -129,16 +137,8 @@ class File implements CacheIfs
*/ */
private function getCacheContent($key) private function getCacheContent($key)
{ {
$filename = $this->getFileName($key); $content = $this->readCacheFile($key);
ob_start(); return (!$content) ? false : $content['value'];
require $filename;
$content = ob_get_contents();
ob_clean();
$jsonDecode = json_decode($content, true);
if (is_null($jsonDecode)) {
return $content;
}
return $jsonDecode;
} }
/** /**
@ -146,25 +146,47 @@ class File implements CacheIfs
* @param $key * @param $key
* @return bool * @return bool
*/ */
private function isTimeOut($key) private function timeOut($key)
{ {
$filename = $this->getFileName($key); $content = $this->readCacheFile($key);
if (file_exists($filename)) { // 缓存文件存在,已读取到内容
ob_start(); if (!empty($content)) {
require $filename; $mtime = $content['create'];
ob_clean(); $timeout = $content['time'];
$mtime = filemtime($filename);
if ($timeout == 0) { if ($timeout == 0) {
return true; return false;
} elseif ((time() - $mtime >= $timeout)) { } elseif ((time() - $mtime >= $timeout)) {
// 已超时,删除缓存 // 已超时,删除缓存
$this->remove($key); $this->remove($key);
return false;
} else {
return true; return true;
} else {
return false;
} }
} }
return false; // 否则直接返回超时
return true;
}
/**
* 读取缓存文件
* @param $key
* @return mixed
*/
private function readCacheFile($key)
{
if (!isset($this->files[$key])) {
// 获取文件名
$filename = $this->getFileName($key);
if (is_file($filename)) {
$content = file_get_contents($filename);
$this->files[$key] = unserialize($content);
unset($content);
} else {
// 文件不存在
$this->files[$key] = false;
}
}
return $this->files[$key];
} }
/** /**
@ -174,7 +196,7 @@ class File implements CacheIfs
*/ */
public function getFileName($key) public function getFileName($key)
{ {
return $this->dir . $key . '.php'; return $this->dir . $key . '.txt';
} }
/** /**

View File

@ -2,7 +2,6 @@
namespace top\library\cache\driver; namespace top\library\cache\driver;
use top\library\cache\ifs\CacheIfs; use top\library\cache\ifs\CacheIfs;
use top\library\Config; use top\library\Config;
use top\traits\Instance; use top\traits\Instance;
@ -14,6 +13,7 @@ use top\traits\Instance;
*/ */
class Redis implements CacheIfs class Redis implements CacheIfs
{ {
use Instance; use Instance;
/** /**
@ -55,9 +55,8 @@ class Redis implements CacheIfs
*/ */
public function set($key, $value, $timeout = 10) public function set($key, $value, $timeout = 10)
{ {
if (is_array($value) || is_object($value)) { $data = ['value' => $value];
$value = json_encode($value); $value = serialize($data);
}
$timeout = $timeout == 0 ? null : $timeout; $timeout = $timeout == 0 ? null : $timeout;
return $this->redis->set($key, $value, $timeout); return $this->redis->set($key, $value, $timeout);
} }
@ -71,22 +70,18 @@ class Redis implements CacheIfs
public function get($key = null, $callable = null) public function get($key = null, $callable = null)
{ {
$status = $this->exists($key); $status = $this->exists($key);
$value = $status == 0 ? false : $this->redis->get($key); $value = $status ? $this->redis->get($key) : false;
// 如果获取不到结果但是callable存在 // 如果获取不到结果但是callable存在
if ($value === false) { if ($value === false) {
if (is_callable($callable)) { if (is_callable($callable)) {
return $callable($this); return $callable($this);
} }
return false; return false;
} else {
$data = unserialize($value);
// 返回转换后的数据
return $data['value'];
} }
// 判断值是否是json字符串
$jsonDecode = json_decode($value, true);
if (is_null($jsonDecode)) {
// 原始数据
return $value;
}
// 返回转换后的数据
return $jsonDecode;
} }
/** /**
@ -106,6 +101,6 @@ class Redis implements CacheIfs
*/ */
public function exists($key) public function exists($key)
{ {
return $this->redis->exists($key); return $this->redis->exists($key) ? true : false;
} }
} }