Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added deleteByPregix, deleteBySuffix, deleteByRegex in CacheProvider #203

Closed
wants to merge 2 commits into from

7 participants

@masarliev

I found that this feature is missing.

@doctrinebot
Collaborator

Hello,

thank you for positing this Pull Request. I have automatically opened an issue on our Jira Bug Tracker for you with the details of this Pull-Request. See the Link:

http://doctrine-project.org/jira/browse/DCOM-124

lib/Doctrine/Common/Cache/CacheProvider.php
((10 lines not shown))
}
-
- /**
+
+ /**
@stof Collaborator
stof added a note

Please don't add trailing whitespaces and don't break the indentation of the phpdoc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/Common/Cache/CacheProvider.php
((15 lines not shown))
* {@inheritdoc}
*/
public function delete($id)
{
- return $this->doDelete($this->getNamespacedId($id));
+ $deleted = $this->doDelete($this->getNamespacedId($id));
+ if($deleted){
@Baachi
Baachi added a note

Missing spaces here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Baachi Baachi commented on the diff
lib/Doctrine/Common/Cache/CacheProvider.php
((42 lines not shown))
+ }
+ return $deleted;
+ }
+
+ /**
+ * Delete cache entries where the id has the passed prefix
+ *
+ * @param string $prefix
+ * @return array $deleted Array of the deleted cache ids
+ */
+ public function deleteByPrefix($prefix)
+ {
+ $deleted = array();
+ $ids = $this->getIds();
+ foreach ($ids as $id) {
+ if (preg_match('/^'.$prefix.'/', $id)) {
@Baachi
Baachi added a note

You should escape all slashes in $prefix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Baachi Baachi commented on the diff
lib/Doctrine/Common/Cache/CacheProvider.php
((77 lines not shown))
+ $this->delete($id);
+ $deleted[] = $id;
+ }
+ }
+ return $deleted;
+ }
+
+ /**
+ * Get an array of all the cache ids stored
+ *
+ * @return array $ids
+ */
+ public function getIds()
+ {
+ $ids = $this->fetch($this->cacheKey);
+ return $ids ? $ids : array();
@Baachi
Baachi added a note

This could be simplified.

return (array) $ids;

(array) false == array(false) so that wont work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/Common/Cache/CacheProvider.php
((61 lines not shown))
+ }
+ return $deleted;
+ }
+
+ /**
+ * Delete cache entries where the id has the passed suffix
+ *
+ * @param string $suffix
+ * @return array $deleted Array of the deleted cache ids
+ */
+ public function deleteBySuffix($suffix)
+ {
+ $deleted = array();
+ $ids = $this->getIds();
+ foreach ($ids as $id) {
+ if (substr($id, -1 * strlen($suffix)) == $suffix) {
@Baachi
Baachi added a note

Please use === instead of ==

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/Common/Cache/CacheProvider.php
((24 lines not shown))
+ return $deleted;
+ }
+
+ /**
+ * Delete cache entries where the id matches a PHP regular expressions
+ *
+ * @param string $regex
+ * @return array $deleted Array of the deleted cache ids
+ */
+ public function deleteByRegex($regex)
+ {
+ $deleted = array();
+ $ids = $this->getIds();
+ foreach ($ids as $id) {
+ if (preg_match($regex, $id)) {
+ $this->delete($id);
@Baachi
Baachi added a note

You should be ensure that the deletion of the id was successful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius
Owner

@masarliev should all these also land in #211?

@theunraveler

Any plans to merge this in? It's extremely useful.

@beberlei
Owner

We removed this feature earlier, because it produces cache slams and other evil problems depending on the cache driver.

@beberlei beberlei closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 16, 2012
  1. @masarliev
Commits on Nov 1, 2012
  1. @masarliev

    Small fixes

    masarliev authored
This page is out of date. Refresh to see the latest.
View
123 lib/Doctrine/Common/Cache/CacheProvider.php
@@ -29,11 +29,17 @@
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
+ * @author Mitko Masarliev <mitko@masarliev.net>
*/
abstract class CacheProvider implements Cache
{
const DOCTRINE_NAMESPACE_CACHEKEY = 'DoctrineNamespaceCacheKey[%s]';
+ /**
+ * @var string The cache id to store the index of cache ids under
+ */
+ private $cacheKey = 'doctrine_cache_ids';
+
/**
* @var string The namespace to prefix all cache ids with
*/
@@ -86,15 +92,126 @@ public function contains($id)
*/
public function save($id, $data, $lifeTime = 0)
{
- return $this->doSave($this->getNamespacedId($id), $data, $lifeTime);
+ $saved = $this->doSave($this->getNamespacedId($id), $data, $lifeTime);
+ if($saved){
+ $this->saveCacheKey($id);
+ }
+ return $saved;
}
-
+
/**
* {@inheritdoc}
*/
public function delete($id)
{
- return $this->doDelete($this->getNamespacedId($id));
+ $deleted = $this->doDelete($this->getNamespacedId($id));
+ if ($deleted){
+ $this->deleteCacheKey($id);
+ }
+ return $deleted;
+ }
+
+ /**
+ * Delete cache entries where the id matches a PHP regular expressions
+ *
+ * @param string $regex
+ * @return array $deleted Array of the deleted cache ids
+ */
+ public function deleteByRegex($regex)
+ {
+ $deleted = array();
+ $ids = $this->getIds();
+ foreach ($ids as $id) {
+ if (preg_match($regex, $id)) {
+ if ($this->delete($id)){
+ $deleted[] = $id;
+ }
+ }
+ }
+ return $deleted;
+ }
+
+ /**
+ * Delete cache entries where the id has the passed prefix
+ *
+ * @param string $prefix
+ * @return array $deleted Array of the deleted cache ids
+ */
+ public function deleteByPrefix($prefix)
+ {
+ $deleted = array();
+ $ids = $this->getIds();
+ foreach ($ids as $id) {
+ if (preg_match('/^'.$prefix.'/', $id)) {
@Baachi
Baachi added a note

You should escape all slashes in $prefix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ if ($this->delete($id)){
+ $deleted[] = $id;
+ }
+ }
+ }
+ return $deleted;
+ }
+
+ /**
+ * Delete cache entries where the id has the passed suffix
+ *
+ * @param string $suffix
+ * @return array $deleted Array of the deleted cache ids
+ */
+ public function deleteBySuffix($suffix)
+ {
+ $deleted = array();
+ $ids = $this->getIds();
+ foreach ($ids as $id) {
+ if (substr($id, -1 * strlen($suffix)) === $suffix) {
+ if ($this->delete($id)){
+ $deleted[] = $id;
+ }
+ }
+ }
+ return $deleted;
+ }
+
+ /**
+ * Get an array of all the cache ids stored
+ *
+ * @return array $ids
+ */
+ public function getIds()
+ {
+ $ids = $this->fetch($this->cacheKey);
+ return $ids ? $ids : array();
@Baachi
Baachi added a note

This could be simplified.

return (array) $ids;

(array) false == array(false) so that wont work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+
+ /**
+ * Save a cache id to the index of cache ids
+ *
+ * @param string $id
+ * @return boolean TRUE if the id was successfully stored in the cache, FALSE otherwise.
+ */
+ private function saveCacheKey($id, $lifeTime = 0){
+ $ids = $this->getIds();
+ if (in_array($id, $ids)){
+ return false;
+ }
+ $ids[] = $id;
+ return $this->save($this->cacheKey, $ids);
+ }
+
+ /**
+ * Delete a cache id from the index of cache ids
+ *
+ * @param string $id
+ * @return boolean TRUE if the entry was successfully removed from the cache, FALSE otherwise.
+ */
+ private function deleteCacheKey($id)
+ {
+ $ids = $this->getIds();
+ $key = array_search($id, $ids);
+ if ($key !== false) {
+ unset($ids[$key]);
+ return $this->save($this->cacheKey, $ids);
+ }
+ return false;
}
/**
View
27 tests/Doctrine/Tests/Common/Cache/CacheTest.php
@@ -55,6 +55,33 @@ public function testFlushAll()
$this->assertFalse($cache->contains('test_key1'));
$this->assertFalse($cache->contains('test_key2'));
}
+
+ public function testDeleteByRegex(){
+ $cache = $this->_getCacheDriver();
+ $cache->save('test_key1', '1');
+ $cache->save('test_key2', '2');
+ $cache->deleteByRegex('/key1/i');
+ $this->assertFalse($cache->contains('test_key1'));
+ $this->assertTrue($cache->contains('test_key2'));
+ }
+
+ public function testDeleteByPrefix(){
+ $cache = $this->_getCacheDriver();
+ $cache->save('key1_test', '1');
+ $cache->save('key2_test', '2');
+ $cache->deleteByPrefix('key1');
+ $this->assertFalse($cache->contains('key1_test'));
+ $this->assertTrue($cache->contains('key2_test'));
+ }
+
+ public function testDeleteBySuffix(){
+ $cache = $this->_getCacheDriver();
+ $cache->save('test_key1', '1');
+ $cache->save('test_key2', '2');
+ $cache->deleteBySuffix('key1');
+ $this->assertFalse($cache->contains('test_key1'));
+ $this->assertTrue($cache->contains('test_key2'));
+ }
public function testNamespace()
{
Something went wrong with that request. Please try again.