diff --git a/lyapi/cache/Cache.php b/lyapi/cache/Cache.php new file mode 100644 index 0000000000000000000000000000000000000000..3f933adbd665b26cfaa6757697512c85c512426f --- /dev/null +++ b/lyapi/cache/Cache.php @@ -0,0 +1,56 @@ +dir = $dir; } + /** + * 设置一个缓存值. + * + * @param string $key 键名 + * @param string $data 数据 + * @param string $expire 过期时间 + * + * @return boolean + */ public function set($key, $data, $expire = 0) { $filename = $this->dir . '/' . md5($key) . '.lyc'; @@ -43,6 +56,13 @@ class FileCache } } + /** + * 获取一个缓存值. + * + * @param string $key 键名 + * + * @return string + */ public function get($key) { $filename = $this->dir . '/' . md5($key) . '.lyc'; @@ -63,6 +83,13 @@ class FileCache } } + /** + * 判断一个缓存键是否存在. + * + * @param string $key 键名 + * + * @return boolean + */ public function has($key) { if ($this->get($key) == '') { @@ -72,12 +99,24 @@ class FileCache } } + + /** + * 删除一个缓存键. + * + * @param string $key 键名 + * + * @return boolean + */ public function delete($key) { $filename = $this->dir . '/' . md5($key) . '.lyc'; return @unlink($filename); } + + /** + * 清空所有缓存. + */ public function clean() { $dirs = scandir($this->dir); diff --git a/lyapi/cache/RedisCache.php b/lyapi/cache/RedisCache.php index 3d1c4661fbb8c3cc1533c05528aa67c353afddc4..76b28c95d48fd147cae0ac2664702e002b09f31a 100644 --- a/lyapi/cache/RedisCache.php +++ b/lyapi/cache/RedisCache.php @@ -2,9 +2,69 @@ namespace LyApi\cache; -use Predis\Client; +use Predis; -class RedisCache extends Client +class RedisCache implements Cache { - // 待编写 .... + private $client; + + private $group; + + public function __construct($group = null) + { + Predis\Autoloader::register(); + + $this->client = new Predis\Client('tcp://127.0.0.1:6379'); + + $this->group = 'cache'.$group; + } + + /** + * @inheritDoc + */ + public function set($key, $data, $expire = 0) + { + if($expire == 0) + return $this->client->set($this->group.$key,$data); + else + return $this->client->setex($this->group.$key,$expire,$data); + } + + /** + * @inheritDoc + */ + public function get($key) + { + return $this->client->get($this->group.$key); + } + + /** + * @inheritDoc + */ + public function has($key) + { + return $this->client->exists($this->group.$key); + } + + /** + * @inheritDoc + */ + public function delete($key) + { + return $this->client->del($this->group.$key); + } + + /** + * @inheritDoc + */ + public function clean() + { + $keys = $this->client->keys($this->group . '*'); + + foreach ($keys as $key) { + $this->delete($key); + } + + return true; + } }