diff --git a/library/think/Cache.php b/library/think/Cache.php index f13f511c..10ff01c8 100644 --- a/library/think/Cache.php +++ b/library/think/Cache.php @@ -110,6 +110,36 @@ class Cache return self::$handler->set($name, $value, $expire); } + /** + * 自增缓存(针对数值缓存) + * @access public + * @param string $name 缓存变量名 + * @param int $step 步长 + * @param int $expire 有效时间 0为永久 + * @return false|int + */ + public function inc($name, $step = 1, $expire = null) + { + self::init(); + self::$writeTimes++; + return self::$handler->inc($name, $step, $expire); + } + + /** + * 自减缓存(针对数值缓存) + * @access public + * @param string $name 缓存变量名 + * @param int $step 步长 + * @param int $expire 有效时间 0为永久 + * @return false|int + */ + public function dec($name, $step = 1, $expire = null) + { + self::init(); + self::$writeTimes++; + return self::$handler->dec($name, $step, $expire); + } + /** * 删除缓存 * @access public @@ -119,6 +149,7 @@ class Cache public static function rm($name) { self::init(); + self::$writeTimes++; return self::$handler->rm($name); } @@ -130,6 +161,7 @@ class Cache public static function clear() { self::init(); + self::$writeTimes++; return self::$handler->clear(); } diff --git a/library/think/cache/driver/File.php b/library/think/cache/driver/File.php index 19d42005..c356f21c 100644 --- a/library/think/cache/driver/File.php +++ b/library/think/cache/driver/File.php @@ -158,6 +158,42 @@ class File } } + /** + * 自增缓存(针对数值缓存) + * @access public + * @param string $name 缓存变量名 + * @param int $step 步长 + * @param int $expire 有效时间 0为永久 + * @return false|int + */ + public function inc($name, $step = 1, $expire = null) + { + if ($this->has($name)) { + $value = $this->get($name) + $step; + } else { + $value = $step; + } + return $this->set($name, $value, $expire) ? $value : false; + } + + /** + * 自减缓存(针对数值缓存) + * @access public + * @param string $name 缓存变量名 + * @param int $step 步长 + * @param int $expire 有效时间 0为永久 + * @return false|int + */ + public function dec($name, $step = 1, $expire = null) + { + if ($this->has($name)) { + $value = $this->get($name) - $step; + } else { + $value = $step; + } + return $this->set($name, $value, $expire) ? $value : false; + } + /** * 删除缓存 * @access public diff --git a/library/think/cache/driver/Lite.php b/library/think/cache/driver/Lite.php index ab295672..e83f73e4 100644 --- a/library/think/cache/driver/Lite.php +++ b/library/think/cache/driver/Lite.php @@ -113,6 +113,42 @@ class Lite return $ret; } + /** + * 自增缓存(针对数值缓存) + * @access public + * @param string $name 缓存变量名 + * @param int $step 步长 + * @param int $expire 有效时间 0为永久 + * @return false|int + */ + public function inc($name, $step = 1, $expire = null) + { + if ($this->has($name)) { + $value = $this->get($name) + $step; + } else { + $value = $step; + } + return $this->set($name, $value, $expire) ? $value : false; + } + + /** + * 自减缓存(针对数值缓存) + * @access public + * @param string $name 缓存变量名 + * @param int $step 步长 + * @param int $expire 有效时间 0为永久 + * @return false|int + */ + public function dec($name, $step = 1, $expire = null) + { + if ($this->has($name)) { + $value = $this->get($name) - $step; + } else { + $value = $step; + } + return $this->set($name, $value, $expire) ? $value : false; + } + /** * 删除缓存 * @access public diff --git a/library/think/cache/driver/Memcache.php b/library/think/cache/driver/Memcache.php index 73b6b008..49b67454 100644 --- a/library/think/cache/driver/Memcache.php +++ b/library/think/cache/driver/Memcache.php @@ -100,6 +100,32 @@ class Memcache return false; } + /** + * 自增缓存(针对数值缓存) + * @access public + * @param string $name 缓存变量名 + * @param int $step 步长 + * @param int $expire 有效时间 0为永久 + * @return false|int + */ + public function inc($name, $step = 1, $expire = null) + { + return $this->handler->increment($name, $step, $expire); + } + + /** + * 自减缓存(针对数值缓存) + * @access public + * @param string $name 缓存变量名 + * @param int $step 步长 + * @param int $expire 有效时间 0为永久 + * @return false|int + */ + public function dec($name, $step = 1, $expire = null) + { + return $this->handler->decrement($name, $step, $expire); + } + /** * 删除缓存 * @param string $name 缓存变量名 diff --git a/library/think/cache/driver/Memcached.php b/library/think/cache/driver/Memcached.php index 4140a842..6b800c1e 100644 --- a/library/think/cache/driver/Memcached.php +++ b/library/think/cache/driver/Memcached.php @@ -106,6 +106,32 @@ class Memcached return false; } + /** + * 自增缓存(针对数值缓存) + * @access public + * @param string $name 缓存变量名 + * @param int $step 步长 + * @param int $expire 有效时间 0为永久 + * @return false|int + */ + public function inc($name, $step = 1, $expire = null) + { + return $this->handler->increment($name, $step, $expire); + } + + /** + * 自减缓存(针对数值缓存) + * @access public + * @param string $name 缓存变量名 + * @param int $step 步长 + * @param int $expire 有效时间 0为永久 + * @return false|int + */ + public function dec($name, $step = 1, $expire = null) + { + return $this->handler->decrement($name, $step, $expire); + } + /** * 删除缓存 * @param string $name 缓存变量名 diff --git a/library/think/cache/driver/Sqlite.php b/library/think/cache/driver/Sqlite.php index 561acdfc..6814773a 100644 --- a/library/think/cache/driver/Sqlite.php +++ b/library/think/cache/driver/Sqlite.php @@ -110,6 +110,42 @@ class Sqlite return false; } + /** + * 自增缓存(针对数值缓存) + * @access public + * @param string $name 缓存变量名 + * @param int $step 步长 + * @param int $expire 有效时间 0为永久 + * @return false|int + */ + public function inc($name, $step = 1, $expire = null) + { + if ($this->has($name)) { + $value = $this->get($name) + $step; + } else { + $value = $step; + } + return $this->set($name, $value, $expire) ? $value : false; + } + + /** + * 自减缓存(针对数值缓存) + * @access public + * @param string $name 缓存变量名 + * @param int $step 步长 + * @param int $expire 有效时间 0为永久 + * @return false|int + */ + public function dec($name, $step = 1, $expire = null) + { + if ($this->has($name)) { + $value = $this->get($name) - $step; + } else { + $value = $step; + } + return $this->set($name, $value, $expire) ? $value : false; + } + /** * 删除缓存 * @access public diff --git a/library/think/cache/driver/Wincache.php b/library/think/cache/driver/Wincache.php index 5556162c..940ecc4e 100644 --- a/library/think/cache/driver/Wincache.php +++ b/library/think/cache/driver/Wincache.php @@ -85,6 +85,32 @@ class Wincache return false; } + /** + * 自增缓存(针对数值缓存) + * @access public + * @param string $name 缓存变量名 + * @param int $step 步长 + * @param int $expire 有效时间 0为永久 + * @return false|int + */ + public function inc($name, $step = 1, $expire = null) + { + return wincache_ucache_inc($name, $step, $expire); + } + + /** + * 自减缓存(针对数值缓存) + * @access public + * @param string $name 缓存变量名 + * @param int $step 步长 + * @param int $expire 有效时间 0为永久 + * @return false|int + */ + public function dec($name, $step = 1, $expire = null) + { + return wincache_ucache_dec($name, $step, $expire); + } + /** * 删除缓存 * @access public diff --git a/library/think/cache/driver/Xcache.php b/library/think/cache/driver/Xcache.php index 6c7b9c95..99b01f6b 100644 --- a/library/think/cache/driver/Xcache.php +++ b/library/think/cache/driver/Xcache.php @@ -85,6 +85,32 @@ class Xcache return false; } + /** + * 自增缓存(针对数值缓存) + * @access public + * @param string $name 缓存变量名 + * @param int $step 步长 + * @param int $expire 有效时间 0为永久 + * @return false|int + */ + public function inc($name, $step = 1, $expire = null) + { + return xcache_inc($name, $step, $expire); + } + + /** + * 自减缓存(针对数值缓存) + * @access public + * @param string $name 缓存变量名 + * @param int $step 步长 + * @param int $expire 有效时间 0为永久 + * @return false|int + */ + public function dec($name, $step = 1, $expire = null) + { + return xcache_dec($name, $step, $expire); + } + /** * 删除缓存 * @access public