-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Cache] Implement PSR-16 SimpleCache #20636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,14 @@ | |
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerAwareTrait; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\SimpleCache\CounterInterface; | ||
use Symfony\Component\Cache\CacheItem; | ||
use Symfony\Component\Cache\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface | ||
abstract class AbstractAdapter implements AdapterInterface, CounterInterface, LoggerAwareInterface | ||
{ | ||
use LoggerAwareTrait; | ||
|
||
|
@@ -372,6 +373,40 @@ public function commit() | |
return $ok; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* This implementation is not atomic unless the "doIncrement()" method provided by the concrete adapter is. | ||
*/ | ||
public function increment($key, $step = 1) | ||
{ | ||
if (!is_numeric($step)) { | ||
return false; | ||
} | ||
$id = $this->getId($key); | ||
|
||
try { | ||
if (is_numeric($result = $this->doIncrement($id, (int) $step))) { | ||
return $result; | ||
} | ||
CacheItem::log($this->logger, 'Failed to increment key "{key}"', array('key' => $key)); | ||
} catch (\Exception $e) { | ||
CacheItem::log($this->logger, 'Failed to increment key "{key}"', array('key' => $key, 'exception' => $e)); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* This implementation is not atomic unless the "doIncrement()" method provided by the concrete adapter is. | ||
*/ | ||
public function decrement($key, $step = 1) | ||
{ | ||
return is_numeric($step) ? $this->increment($key, -$step) : false; | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
if ($this->deferred) { | ||
|
@@ -406,6 +441,29 @@ protected static function unserialize($value) | |
} | ||
} | ||
|
||
/** | ||
* Increments a value in the cache by the given step value and returns the new value. | ||
* | ||
* If the key does not exist, it is initialized to the value of $step. | ||
* | ||
* @param string $key The cache item key | ||
* @param int $step The value to increment by | ||
* | ||
* @return int|bool The new value on success and false on failure | ||
*/ | ||
protected function doIncrement($id, $step) | ||
{ | ||
foreach ($this->doFetch(array($id)) as $value) { | ||
if (is_numeric($value)) { | ||
$step += $value; | ||
} | ||
} | ||
|
||
$e = $this->doSave(array($id => $step), 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this implementation in the base adapter is suspicious. The spec asks for atomicity, but atomicity requires to overwrite this method in each child adapter. An adapter not overwriting it would not provide atomicity, thus breaking the spec. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see #20636 (comment) |
||
|
||
return true === $e || array() === $e ? $step : false; | ||
} | ||
|
||
private function getId($key) | ||
{ | ||
CacheItem::validateKey($key); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you call this code also from decrement, this message may be misleading in some cases