改进自增缓存

原来的方式在step为负数时会报错,并且在没有这个键值对时不会自动创建。现在的方式,避免了这个两个问题。
This commit is contained in:
zzpuser
2016-08-03 23:21:27 +08:00
committed by GitHub
parent dd1f1d49ab
commit 3c0e97767d

View File

@@ -115,7 +115,14 @@ class Memcached
*/
public function inc($name, $step = 1)
{
return $this->handler->increment($this->options['prefix'] . $name, $step);
$oldValue = $this->handler->get($this->options['prefix'] . $name);
$value = $oldValue + $step;
$res = $this->handler->set($this->options['prefix'] . $name, $value);
if (!$res) {
return false;
} else {
return $value;
}
}
/**
@@ -129,7 +136,7 @@ class Memcached
{
$oldValue = $this->handler->get($this->options['prefix'] . $name);
$value = $oldValue - $step;
$res = $this->handler->set($this->options['prefix'] . $name, $oldValue - $step);
$res = $this->handler->set($this->options['prefix'] . $name, $value);
if (!$res) {
return false;
} else {