diff --git a/library/think/cache/Driver.php b/library/think/cache/Driver.php index 19808c05..b975ba0d 100644 --- a/library/think/cache/Driver.php +++ b/library/think/cache/Driver.php @@ -80,6 +80,17 @@ abstract class Driver */ abstract public function clear($tag = null); + /** + * 获取实际的缓存标识 + * @access public + * @param string $name 缓存名 + * @return string + */ + protected function getCacheKey($name) + { + return $this->options['prefix'] . $name; + } + /** * 缓存标签 * @access public @@ -96,6 +107,7 @@ abstract class Driver if (is_string($keys)) { $keys = explode(',', $keys); } + $keys = array_map([$this, 'getCacheKey'], $keys); $value = array_unique(array_merge($this->getTagItem($name), $keys)); $this->set($key, implode(',', $value)); } diff --git a/library/think/cache/driver/File.php b/library/think/cache/driver/File.php index b3059e59..da3b3f6f 100644 --- a/library/think/cache/driver/File.php +++ b/library/think/cache/driver/File.php @@ -60,11 +60,11 @@ class File extends Driver /** * 取得变量的存储文件名 - * @access private + * @access protected * @param string $name 缓存变量名 * @return string */ - private function filename($name) + protected function getCacheKey($name) { $name = md5($name); if ($this->options['cache_subdir']) { @@ -90,7 +90,7 @@ class File extends Driver */ public function has($name) { - $filename = $this->filename($name); + $filename = $this->getCacheKey($name); return is_file($filename); } @@ -103,7 +103,7 @@ class File extends Driver */ public function get($name, $default = false) { - $filename = $this->filename($name); + $filename = $this->getCacheKey($name); if (!is_file($filename)) { return $default; } @@ -140,7 +140,7 @@ class File extends Driver if (is_null($expire)) { $expire = $this->options['expire']; } - $filename = $this->filename($name); + $filename = $this->getCacheKey($name); if ($this->tag && !is_file($filename)) { $first = true; } @@ -202,7 +202,7 @@ class File extends Driver */ public function rm($name) { - return $this->unlink($this->filename($name)); + return $this->unlink($this->getCacheKey($name)); } /** @@ -219,6 +219,7 @@ class File extends Driver foreach ($keys as $key) { $this->unlink($key); } + $this->rm('tag_' . md5($tag)); return true; } $fileLsit = (array) glob($this->options['path'] . '*'); diff --git a/library/think/cache/driver/Lite.php b/library/think/cache/driver/Lite.php index 4ae82e6e..a527a012 100644 --- a/library/think/cache/driver/Lite.php +++ b/library/think/cache/driver/Lite.php @@ -44,11 +44,11 @@ class Lite extends Driver /** * 取得变量的存储文件名 - * @access private + * @access protected * @param string $name 缓存变量名 * @return string */ - private function filename($name) + protected function getCacheKey($name) { return $this->options['path'] . $this->options['prefix'] . md5($name) . '.php'; } @@ -61,7 +61,7 @@ class Lite extends Driver */ public function has($name) { - $filename = $this->filename($name); + $filename = $this->getCacheKey($name); return is_file($filename); } @@ -74,7 +74,7 @@ class Lite extends Driver */ public function get($name, $default = false) { - $filename = $this->filename($name); + $filename = $this->getCacheKey($name); if (is_file($filename)) { // 判断是否过期 $mtime = filemtime($filename); @@ -106,7 +106,7 @@ class Lite extends Driver if (0 === $expire) { $expire = 10 * 365 * 24 * 3600; } - $filename = $this->filename($name); + $filename = $this->getCacheKey($name); if ($this->tag && !is_file($filename)) { $first = true; } @@ -161,7 +161,7 @@ class Lite extends Driver */ public function rm($name) { - return unlink($this->filename($name)); + return unlink($this->getCacheKey($name)); } /** @@ -178,6 +178,7 @@ class Lite extends Driver foreach ($keys as $key) { unlink($key); } + $this->rm('tag_' . md5($tag)); return true; } array_map("unlink", glob($this->options['path'] . $this->options['prefix'] . '*.php')); diff --git a/library/think/cache/driver/Memcache.php b/library/think/cache/driver/Memcache.php index 2beb5837..8e3df294 100644 --- a/library/think/cache/driver/Memcache.php +++ b/library/think/cache/driver/Memcache.php @@ -64,8 +64,8 @@ class Memcache extends Driver */ public function has($name) { - $name = $this->options['prefix'] . $name; - return $this->handler->get($name) ? true : false; + $key = $this->getCacheKey($name); + return $this->handler->get($key) ? true : false; } /** @@ -77,7 +77,7 @@ class Memcache extends Driver */ public function get($name, $default = false) { - $result = $this->handler->get($this->options['prefix'] . $name); + $result = $this->handler->get($this->getCacheKey($name)); return false !== $result ? $result : $default; } @@ -97,9 +97,9 @@ class Memcache extends Driver if ($this->tag && !$this->has($name)) { $first = true; } - $name = $this->options['prefix'] . $name; - if ($this->handler->set($name, $value, 0, $expire)) { - isset($first) && $this->setTagItem($name); + $key = $this->getCacheKey($name); + if ($this->handler->set($key, $value, 0, $expire)) { + isset($first) && $this->setTagItem($key); return true; } return false; @@ -114,8 +114,8 @@ class Memcache extends Driver */ public function inc($name, $step = 1) { - $name = $this->options['prefix'] . $name; - return $this->handler->increment($name, $step); + $key = $this->getCacheKey($name); + return $this->handler->increment($key, $step); } /** @@ -127,9 +127,9 @@ class Memcache extends Driver */ public function dec($name, $step = 1) { - $name = $this->options['prefix'] . $name; - $value = $this->handler->get($name) - $step; - $res = $this->handler->set($name, $value); + $key = $this->getCacheKey($name); + $value = $this->handler->get($key) - $step; + $res = $this->handler->set($key, $value); if (!$res) { return false; } else { @@ -145,10 +145,10 @@ class Memcache extends Driver */ public function rm($name, $ttl = false) { - $name = $this->options['prefix'] . $name; + $key = $this->getCacheKey($name); return false === $ttl ? - $this->handler->delete($name) : - $this->handler->delete($name, $ttl); + $this->handler->delete($key) : + $this->handler->delete($key, $ttl); } /** @@ -165,6 +165,7 @@ class Memcache extends Driver foreach ($keys as $key) { $this->handler->delete($key); } + $this->rm('tag_' . md5($tag)); return true; } return $this->handler->flush(); diff --git a/library/think/cache/driver/Memcached.php b/library/think/cache/driver/Memcached.php index 78be1069..9c0a2597 100644 --- a/library/think/cache/driver/Memcached.php +++ b/library/think/cache/driver/Memcached.php @@ -70,8 +70,8 @@ class Memcached extends Driver */ public function has($name) { - $name = $this->options['prefix'] . $name; - return $this->handler->get($name) ? true : false; + $key = $this->getCacheKey($name); + return $this->handler->get($key) ? true : false; } /** @@ -83,7 +83,7 @@ class Memcached extends Driver */ public function get($name, $default = false) { - $result = $this->handler->get($this->options['prefix'] . $name); + $result = $this->handler->get($this->getCacheKey($name)); return false !== $result ? $result : $default; } @@ -103,10 +103,10 @@ class Memcached extends Driver if ($this->tag && !$this->has($name)) { $first = true; } - $name = $this->options['prefix'] . $name; + $key = $this->getCacheKey($name); $expire = 0 == $expire ? 0 : $_SERVER['REQUEST_TIME'] + $expire; - if ($this->handler->set($name, $value, $expire)) { - isset($first) && $this->setTagItem($name); + if ($this->handler->set($key, $value, $expire)) { + isset($first) && $this->setTagItem($key); return true; } return false; @@ -121,8 +121,8 @@ class Memcached extends Driver */ public function inc($name, $step = 1) { - $name = $this->options['prefix'] . $name; - return $this->handler->increment($name, $step); + $key = $this->getCacheKey($name); + return $this->handler->increment($key, $step); } /** @@ -134,9 +134,9 @@ class Memcached extends Driver */ public function dec($name, $step = 1) { - $name = $this->options['prefix'] . $name; - $value = $this->handler->get($name) - $step; - $res = $this->handler->set($name, $value); + $key = $this->getCacheKey($name); + $value = $this->handler->get($key) - $step; + $res = $this->handler->set($key, $value); if (!$res) { return false; } else { @@ -152,10 +152,10 @@ class Memcached extends Driver */ public function rm($name, $ttl = false) { - $name = $this->options['prefix'] . $name; + $key = $this->getCacheKey($name); return false === $ttl ? - $this->handler->delete($name) : - $this->handler->delete($name, $ttl); + $this->handler->delete($key) : + $this->handler->delete($key, $ttl); } /** @@ -170,6 +170,7 @@ class Memcached extends Driver // 指定标签清除 $keys = $this->getTagItem($tag); $this->handler->deleteMulti($keys); + $this->rm('tag_' . md5($tag)); return true; } return $this->handler->flush(); diff --git a/library/think/cache/driver/Redis.php b/library/think/cache/driver/Redis.php index 6dbc4844..cd33523f 100644 --- a/library/think/cache/driver/Redis.php +++ b/library/think/cache/driver/Redis.php @@ -63,7 +63,7 @@ class Redis extends Driver */ public function has($name) { - return $this->handler->get($this->options['prefix'] . $name) ? true : false; + return $this->handler->get($this->getCacheKey($name)) ? true : false; } /** @@ -75,7 +75,7 @@ class Redis extends Driver */ public function get($name, $default = false) { - $value = $this->handler->get($this->options['prefix'] . $name); + $value = $this->handler->get($this->getCacheKey($name)); if (is_null($value)) { return $default; } @@ -97,14 +97,18 @@ class Redis extends Driver if (is_null($expire)) { $expire = $this->options['expire']; } - $name = $this->options['prefix'] . $name; + if ($this->tag && !$this->has($name)) { + $first = true; + } + $key = $this->getCacheKey($name); //对数组/对象数据进行缓存处理,保证数据完整性 byron sampson $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value; if (is_int($expire) && $expire) { - $result = $this->handler->setex($name, $expire, $value); + $result = $this->handler->setex($key, $expire, $value); } else { - $result = $this->handler->set($name, $value); + $result = $this->handler->set($key, $value); } + isset($first) && $this->setTagItem($key); return $result; } @@ -117,8 +121,8 @@ class Redis extends Driver */ public function inc($name, $step = 1) { - $name = $this->options['prefix'] . $name; - return $this->handler->incrby($name, $step); + $key = $this->getCacheKey($name); + return $this->handler->incrby($key, $step); } /** @@ -130,8 +134,8 @@ class Redis extends Driver */ public function dec($name, $step = 1) { - $name = $this->options['prefix'] . $name; - return $this->handler->decrby($name, $step); + $key = $this->getCacheKey($name); + return $this->handler->decrby($key, $step); } /** @@ -142,7 +146,7 @@ class Redis extends Driver */ public function rm($name) { - return $this->handler->delete($this->options['prefix'] . $name); + return $this->handler->delete($this->getCacheKey($name)); } /** @@ -159,6 +163,7 @@ class Redis extends Driver foreach ($keys as $key) { $this->handler->delete($key); } + $this->rm('tag_' . md5($tag)); return true; } return $this->handler->flushDB(); diff --git a/library/think/cache/driver/Sqlite.php b/library/think/cache/driver/Sqlite.php index 392cd1fb..0b774a1a 100644 --- a/library/think/cache/driver/Sqlite.php +++ b/library/think/cache/driver/Sqlite.php @@ -47,6 +47,17 @@ class Sqlite extends Driver $this->handler = $func($this->options['db']); } + /** + * 获取实际的缓存标识 + * @access public + * @param string $name 缓存名 + * @return string + */ + protected function getCacheKey($name) + { + return $this->options['prefix'] . sqlite_escape_string($name); + } + /** * 判断缓存 * @access public @@ -55,7 +66,7 @@ class Sqlite extends Driver */ public function has($name) { - $name = $this->options['prefix'] . sqlite_escape_string($name); + $name = $this->getCacheKey($name); $sql = 'SELECT value FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\' AND (expire=0 OR expire >' . $_SERVER['REQUEST_TIME'] . ') LIMIT 1'; $result = sqlite_query($this->handler, $sql); return sqlite_num_rows($result); @@ -70,7 +81,7 @@ class Sqlite extends Driver */ public function get($name, $default = false) { - $name = $this->options['prefix'] . sqlite_escape_string($name); + $name = $this->getCacheKey($name); $sql = 'SELECT value FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\' AND (expire=0 OR expire >' . $_SERVER['REQUEST_TIME'] . ') LIMIT 1'; $result = sqlite_query($this->handler, $sql); if (sqlite_num_rows($result)) { @@ -94,7 +105,7 @@ class Sqlite extends Driver */ public function set($name, $value, $expire = null) { - $name = $this->options['prefix'] . sqlite_escape_string($name); + $name = $this->getCacheKey($name); $value = sqlite_escape_string(serialize($value)); if (is_null($expire)) { $expire = $this->options['expire']; @@ -159,7 +170,7 @@ class Sqlite extends Driver */ public function rm($name) { - $name = $this->options['prefix'] . sqlite_escape_string($name); + $name = $this->getCacheKey($name); $sql = 'DELETE FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\''; sqlite_query($this->handler, $sql); return true; diff --git a/library/think/cache/driver/Wincache.php b/library/think/cache/driver/Wincache.php index e529fe28..36fe3bea 100644 --- a/library/think/cache/driver/Wincache.php +++ b/library/think/cache/driver/Wincache.php @@ -51,8 +51,8 @@ class Wincache extends Driver */ public function has($name) { - $name = $this->options['prefix'] . $name; - return wincache_ucache_exists($name); + $key = $this->getCacheKey($name); + return wincache_ucache_exists($key); } /** @@ -64,8 +64,8 @@ class Wincache extends Driver */ public function get($name, $default = false) { - $name = $this->options['prefix'] . $name; - return wincache_ucache_exists($name) ? wincache_ucache_get($name) : $default; + $key = $this->getCacheKey($name); + return wincache_ucache_exists($key) ? wincache_ucache_get($key) : $default; } /** @@ -81,12 +81,12 @@ class Wincache extends Driver if (is_null($expire)) { $expire = $this->options['expire']; } - $name = $this->options['prefix'] . $name; + $key = $this->getCacheKey($name); if ($this->tag && !$this->has($name)) { $first = true; } - if (wincache_ucache_set($name, $value, $expire)) { - isset($first) && $this->setTagItem($name); + if (wincache_ucache_set($key, $value, $expire)) { + isset($first) && $this->setTagItem($key); return true; } return false; @@ -101,8 +101,8 @@ class Wincache extends Driver */ public function inc($name, $step = 1) { - $name = $this->options['prefix'] . $name; - return wincache_ucache_inc($name, $step); + $key = $this->getCacheKey($name); + return wincache_ucache_inc($key, $step); } /** @@ -114,8 +114,8 @@ class Wincache extends Driver */ public function dec($name, $step = 1) { - $name = $this->options['prefix'] . $name; - return wincache_ucache_dec($name, $step); + $key = $this->getCacheKey($name); + return wincache_ucache_dec($key, $step); } /** @@ -126,7 +126,7 @@ class Wincache extends Driver */ public function rm($name) { - return wincache_ucache_delete($this->options['prefix'] . $name); + return wincache_ucache_delete($this->getCacheKey($name)); } /** @@ -142,6 +142,7 @@ class Wincache extends Driver foreach ($keys as $key) { wincache_ucache_delete($key); } + $this->rm('tag_' . md5($tag)); return true; } else { return wincache_ucache_clear(); diff --git a/library/think/cache/driver/Xcache.php b/library/think/cache/driver/Xcache.php index 18a76d2f..6b18b3fc 100644 --- a/library/think/cache/driver/Xcache.php +++ b/library/think/cache/driver/Xcache.php @@ -49,8 +49,8 @@ class Xcache extends Driver */ public function has($name) { - $name = $this->options['prefix'] . $name; - return xcache_isset($name); + $key = $this->getCacheKey($name); + return xcache_isset($key); } /** @@ -62,8 +62,8 @@ class Xcache extends Driver */ public function get($name, $default = false) { - $name = $this->options['prefix'] . $name; - return xcache_isset($name) ? xcache_get($name) : $default; + $key = $this->getCacheKey($name); + return xcache_isset($key) ? xcache_get($key) : $default; } /** @@ -82,9 +82,9 @@ class Xcache extends Driver if ($this->tag && !$this->has($name)) { $first = true; } - $name = $this->options['prefix'] . $name; - if (xcache_set($name, $value, $expire)) { - isset($first) && $this->setTagItem($name); + $key = $this->getCacheKey($name); + if (xcache_set($key, $value, $expire)) { + isset($first) && $this->setTagItem($key); return true; } return false; @@ -99,8 +99,8 @@ class Xcache extends Driver */ public function inc($name, $step = 1) { - $name = $this->options['prefix'] . $name; - return xcache_inc($name, $step); + $key = $this->getCacheKey($name); + return xcache_inc($key, $step); } /** @@ -112,8 +112,8 @@ class Xcache extends Driver */ public function dec($name, $step = 1) { - $name = $this->options['prefix'] . $name; - return xcache_dec($name, $step); + $key = $this->getCacheKey($name); + return xcache_dec($key, $step); } /** @@ -124,7 +124,7 @@ class Xcache extends Driver */ public function rm($name) { - return xcache_unset($this->options['prefix'] . $name); + return xcache_unset($this->getCacheKey($name)); } /** @@ -141,6 +141,7 @@ class Xcache extends Driver foreach ($keys as $key) { xcache_unset($key); } + $this->rm('tag_' . md5($tag)); return true; } if (function_exists('xcache_unset_by_prefix')) {