* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
+ * @author Benoit Burnichon <bburnichon@gmail.com>
*/
-abstract class CacheProvider implements Cache, FlushableCache, ClearableCache, MultiGetCache, MultiPutCache
+abstract class CacheProvider implements Cache, FlushableCache, ClearableCache, MultiOperationCache
{
const DOCTRINE_NAMESPACE_CACHEKEY = 'DoctrineNamespaceCacheKey[%s]';
public function fetchMultiple(array $keys)
{
if (empty($keys)) {
- return array();
+ return [];
}
-
+
// note: the array_combine() is in place to keep an association between our $keys and the $namespacedKeys
- $namespacedKeys = array_combine($keys, array_map(array($this, 'getNamespacedId'), $keys));
+ $namespacedKeys = array_combine($keys, array_map([$this, 'getNamespacedId'], $keys));
$items = $this->doFetchMultiple($namespacedKeys);
- $foundItems = array();
+ $foundItems = [];
// no internal array function supports this sort of mapping: needs to be iterative
// this filters and combines keys in one pass
*/
public function saveMultiple(array $keysAndValues, $lifetime = 0)
{
- $namespacedKeysAndValues = array();
+ $namespacedKeysAndValues = [];
foreach ($keysAndValues as $key => $value) {
$namespacedKeysAndValues[$this->getNamespacedId($key)] = $value;
}
return $this->doSave($this->getNamespacedId($id), $data, $lifeTime);
}
+ /**
+ * {@inheritdoc}
+ */
+ public function deleteMultiple(array $keys)
+ {
+ return $this->doDeleteMultiple(array_map(array($this, 'getNamespacedId'), $keys));
+ }
+
/**
* {@inheritdoc}
*/
*
* @return string The namespaced id.
*/
- private function getNamespacedId($id)
+ private function getNamespacedId(string $id) : string
{
$namespaceVersion = $this->getNamespaceVersion();
*
* @return string
*/
- private function getNamespaceCacheKey()
+ private function getNamespaceCacheKey() : string
{
return sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace);
}
*
* @return integer
*/
- private function getNamespaceVersion()
+ private function getNamespaceVersion() : int
{
if (null !== $this->namespaceVersion) {
return $this->namespaceVersion;
}
- $namespaceCacheKey = $this->getNamespaceCacheKey();
- $this->namespaceVersion = $this->doFetch($namespaceCacheKey) ?: 1;
+ $namespaceCacheKey = $this->getNamespaceCacheKey();
+ $this->namespaceVersion = (int) $this->doFetch($namespaceCacheKey) ?: 1;
return $this->namespaceVersion;
}
*/
protected function doFetchMultiple(array $keys)
{
- $returnValues = array();
+ $returnValues = [];
foreach ($keys as $key) {
if (false !== ($item = $this->doFetch($key)) || $this->doContains($key)) {
*/
abstract protected function doSave($id, $data, $lifeTime = 0);
+ /**
+ * Default implementation of doDeleteMultiple. Each driver that supports multi-delete should override it.
+ *
+ * @param array $keys Array of keys to delete from cache
+ *
+ * @return bool TRUE if the operation was successful, FALSE if it wasn't
+ */
+ protected function doDeleteMultiple(array $keys)
+ {
+ $success = true;
+
+ foreach ($keys as $key) {
+ if (! $this->doDelete($key)) {
+ $success = false;
+ }
+ }
+
+ return $success;
+ }
+
/**
* Deletes a cache entry.
*