Last active
February 10, 2016 22:10
-
-
Save arstropica/a7e11ab4701071f5193a to your computer and use it in GitHub Desktop.
ZF2 RedisCache Driver - Restores the deleteByRegex, deleteByPrefix and deleteBySuffix methods in Doctrine 2's Zend Framework 2 RedisCache Driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\ORM\Tools\Cache; | |
use Doctrine\Common\Cache\RedisCache as BaseCache; | |
/** | |
* Redis Cache Class | |
* | |
* Restores the deleteByRegex, deleteByPrefix and deleteBySuffix methods | |
* in Doctrine 2's RedisCache Driver | |
* | |
* @author arstropica | |
* @link http://github.com/arstropica/ | |
* | |
*/ | |
class RedisCache extends BaseCache { | |
/** | |
* Deletes all cache entries matching the given regular expression. | |
* | |
* @param string $regex | |
* @return array An array of deleted cache ids | |
*/ | |
public function deleteByRegex($regex) | |
{ | |
$deleted = array (); | |
$ids = $this->getIds(); | |
foreach ( $ids as $id ) { | |
if (preg_match($regex, $id)) { | |
$deleted [] = $id; | |
} | |
} | |
if ($deleted) { | |
foreach ( $deleted as $id ) { | |
$this->doDelete($id); | |
} | |
} | |
return $deleted; | |
} | |
/** | |
* Deletes all cache entries beginning with the given string. | |
* | |
* @param string $prefix | |
* @return array An array of deleted cache ids | |
*/ | |
public function deleteByPrefix($prefix) | |
{ | |
$deleted = array (); | |
$deleted = $this->getIds($prefix); | |
if ($deleted) { | |
foreach ( $deleted as $id ) { | |
$this->doDelete($id); | |
} | |
} | |
return $deleted; | |
} | |
/** | |
* Deletes all cache entries ending with the given string. | |
* | |
* @param string $suffix | |
* @return array An array of deleted cache ids | |
*/ | |
public function deleteBySuffix($suffix) | |
{ | |
$deleted = array (); | |
$ids = $this->getIds(); | |
foreach ( $ids as $id ) { | |
if ($suffix === substr($id, -1 * strlen($suffix))) { | |
$deleted [] = $id; | |
} | |
} | |
$this->doDelete($deleted); | |
return $deleted; | |
} | |
/** | |
* Returns an array of cache ids. | |
* | |
* @param string $prefix | |
* Optional id prefix | |
* @return array An array of cache ids | |
*/ | |
public function getIds($prefix = null) | |
{ | |
if ($prefix) { | |
return $this->getRedis() | |
->keys($this->getNamespace() . '\[' . $prefix . '*'); | |
} else { | |
return $this->getRedis() | |
->keys($this->getNamespace() . '*'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment