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