diff --git a/application/Config/cache.php b/application/Config/cache.php index 102d6ff4c7d0f2d7e5476fa5aae7107670d91e35..e0e7f2390a7fe981cbbb10d46f1c1fc5f428d49c 100644 --- a/application/Config/cache.php +++ b/application/Config/cache.php @@ -13,6 +13,6 @@ return [ "redis" => [ "host" => '127.0.0.1', "port" => 6379, - 'database' => 0 + "database" => 0 ] ]; diff --git a/system/Exception/Cache/FileCacheException.php b/system/Exception/Cache/FileCacheException.php new file mode 100644 index 0000000000000000000000000000000000000000..52fc038f44f3bbeaceda5490f7625861c209f399 --- /dev/null +++ b/system/Exception/Cache/FileCacheException.php @@ -0,0 +1,20 @@ +code}]: {$this->message}\n"; + } + +} \ No newline at end of file diff --git a/system/Exception/CacheException.php b/system/Exception/CacheException.php new file mode 100644 index 0000000000000000000000000000000000000000..7bc0ec50e369e2465205742565be28de87372860 --- /dev/null +++ b/system/Exception/CacheException.php @@ -0,0 +1,18 @@ +code}]: {$this->message}\n"; + } + +} \ No newline at end of file diff --git a/system/Support/Cache/Cache.php b/system/Support/Cache/Cache.php index 593a4926dc1ab21cca26d02ce86f7c0adbc89567..15add51c161a05668aa66c0595a816af485b5ee0 100644 --- a/system/Support/Cache/Cache.php +++ b/system/Support/Cache/Cache.php @@ -3,8 +3,7 @@ namespace LyApi\Support\Cache; -use Exception; -use LyApi\Core\Response; +use LyApi\Exception\CacheException; use LyApi\Support\Config; use Predis\Client; use Predis\Connection\ConnectionException; @@ -23,22 +22,23 @@ class Cache { $system = strtoupper(Config::dotConfig('cache.cache_system')); if ($system != null) self::$system = $system; - - if (self::$system == "FILE") { - self::$group = Config::dotConfig("cache.file.default_group"); - self::$object = new FileCache(self::$group); - } else if (self::$system == "REDIS") { - - $server = Config::dotConfig('cache.redis'); - self::$group = $server['database']; - - self::$object = new Client($server); - - try { - self::$object->ping(); - } catch (ConnectionException $e) { - throw new Exception("Redis 服务器连接失败!"); - } + switch (self::$system) { + case 'REDIS': + $server = Config::dotConfig('cache.redis'); + self::$group = $server['database']; + self::$object = new Client($server); + try { + self::$object->ping(); + } catch (ConnectionException $e) { + throw new CacheException("Redis 服务器连接失败!$e"); + } + break; + case 'FILE': + self::$group = Config::dotConfig("cache.file.default_group"); + self::$object = new FileCache(self::$group); + break; + default: + throw new CacheException("dbtype error.Please check the cache profile!"); } } @@ -60,68 +60,49 @@ class Cache // 设置缓存数据 public static function set($key, $value, $expire = 0) { - if (self::$system == 'FILE') { - return self::$object->set($key, $value, $expire); - } else if (self::$system == 'REDIS') { - if ($expire == 0) { - return self::$object->set($key, $value); - } else { - return self::$object->setex($key, $expire, $value); - } + if ($expire == 0) { + return self::$object->set($key, $value); + } else { + return self::$object->setex($key, $expire, $value); } } // 数据自增 public static function inc($key, $add = 1) { - if (self::$system == 'FILE') { - if (self::has($key)) { - $data = self::get($key); - if (is_numeric($data)) { - $exp = self::$object->surexp($key); - if ($exp >= 0) { - return self::set($key, $data + $add, $exp); - } else { - return false; - } - } else { - return false; - } - } else { - return false; - } - } else if (self::$system == 'REDIS') { - return self::$object->incrby($key, $add); - } + return self::$object->incrby($key, $add); } // 检查值是否存在 public static function has($key) { - if (self::$system == 'FILE') { - return self::$object->has($key); - } else if (self::$system == "REDIS") { - return self::$object->exists($key); + switch (self::$system) { + case 'REDIS': + return self::$object->exists($key); + default: + return self::$object->has($key); } } // 删除缓存数据 public static function delete($key) { - if (self::$system == 'FILE') { - return self::$object->delete($key); - } else if (self::$system == 'REDIS') { - return self::$object->del($key); + switch (self::$system) { + case 'REDIS': + return self::$object->del($key); + default: + return self::$object->delete($key); } } // 清空所有数据信息 public static function clean() { - if (self::$system == 'FILE') { - return self::$object->clean(); - } else if (self::$system == 'REDIS') { - return self::$object->flushdb(); + switch (self::$system) { + case 'REDIS': + return self::$object->flushdb(); + default: + return self::$object->clean(); } } @@ -129,10 +110,15 @@ class Cache public static function system($to) { $to = strtoupper($to); - if ($to == "FILE") { - self::$system = "FILE"; - } else if ($to == 'REDIS') { - self::$system = "REDIS"; + switch ($to) { + case 'REDIS': + self::$system = "REDIS"; + break; + case 'FILE': + self::$system = "FILE"; + break; + default: + throw new CacheException('dbtype error.'); } } } diff --git a/system/Support/Cache/FileCache.php b/system/Support/Cache/FileCache.php index da34f2159a3665f3f349ce3ddb45356c7f01ef12..85da829bb4b2fe34aca7a2e68ce5aa6a043bf5e0 100644 --- a/system/Support/Cache/FileCache.php +++ b/system/Support/Cache/FileCache.php @@ -2,6 +2,8 @@ namespace LyApi\Support\Cache; +use LyApi\Exception\Cache\FileCacheException; + class FileCache { private $dir; @@ -9,7 +11,7 @@ class FileCache /** * 初始化缓存设置. */ - public function __construct($group = null) + public function __construct($group = null) { if (is_null($group)) { $group = 'defualt'; @@ -17,7 +19,7 @@ class FileCache $dir = ROOT_PATH . '/runtime/cache/' . $group; if (!is_dir($dir)) { if (!mkdir($dir, 0777, true)) { - return false; + throw new FileCacheException("Can't read or write the cache directory, please check the cache directory permission!"); } } $this->dir = $dir; @@ -27,23 +29,66 @@ class FileCache * 设置一个缓存值. * * @param string $key 键名 + * @param int $add 自增值 + * + * @return boolean + */ + public function incrby($key, $add) + { + if (self::has($key)) { + $data = self::get($key); + if (is_numeric($data)) { + $exp = self::surexp($key); + if ($exp >= 0) { + return self::set($key, $data + $add, $exp); + } else { + return false; + } + } else { + return false; + } + } else { + return false; + } + } + + /** + * 设置一个缓存值. + * + * @param string $key 键名 + * @param int $expire 过期时间 * @param string $data 数据 - * @param string $expire 过期时间 * * @return boolean */ - public function set($key, $data, $expire = 0) + public function setex($key, $expire, $value) { - $filename = $this->dir . '/' . md5($key) . '.lyc'; - if ($expire != 0) { - $duetime = time() + $expire; + return self::set($key, $value, $expire); + } + + /** + * 设置一个缓存值. + * + * @param string $key 键名 + * @param string $data 数据 + * @param int $expire 过期时间 + * + * @return boolean + */ + public function set() + { + $args = func_get_args(); + $filename = $this->dir . '/' . md5($args[0]) . '.lyc'; + + if (count($args) == 3) { + $duetime = time() + $args[2]; $datas = array( - 'data' => $data, + 'data' => $args[1], 'duetime' => $duetime ); } else { $datas = array( - 'data' => $data + 'data' => $args[1] ); } @@ -80,6 +125,8 @@ class FileCache } else { return $datas['data']; } + }else{ + throw new FileCacheException($filename." is not a valid cache file."); } } @@ -107,6 +154,8 @@ class FileCache } else { return 0; } + }else{ + throw new FileCacheException($filename." is not a valid cache file."); } }