From 2991622853be8d18b4b2674abf8d6c955793ad4c Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Mon, 23 Nov 2009 11:47:24 +0100 Subject: [PATCH] Use a versioned cache. --- framework/Perms/lib/Horde/Perms/Datatree.php | 21 ++++++++++++++------- framework/Perms/lib/Horde/Perms/Sql.php | 21 ++++++++++++++------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/framework/Perms/lib/Horde/Perms/Datatree.php b/framework/Perms/lib/Horde/Perms/Datatree.php index e0c697bff..3af9021ab 100644 --- a/framework/Perms/lib/Horde/Perms/Datatree.php +++ b/framework/Perms/lib/Horde/Perms/Datatree.php @@ -30,6 +30,13 @@ class Horde_Perms_Datatree extends Horde_Perms protected $_cache; /** + * Incrementing version number if cached classes change. + * + * @var integer + */ + private $_cacheVersion = 2; + + /** * Cache for getPermission(). * * @var array @@ -105,10 +112,10 @@ class Horde_Perms_Datatree extends Horde_Perms return $this->_permsCache[$name]; } - $perm = $this->_cache->get('perm_' . $name, $GLOBALS['conf']['cache']['default_lifetime']); + $perm = $this->_cache->get('perm_' . $this->_cacheVersion . $name, $GLOBALS['conf']['cache']['default_lifetime']); if ($perm === false) { $perm = $this->_datatree->getObject($name, 'Horde_Perms_Permission_DataTreeObject'); - $this->_cache->set('perm_' . $name, serialize($perm), $GLOBALS['conf']['cache']['default_lifetime']); + $this->_cache->set('perm_' . $this->_cacheVersion . $name, serialize($perm), $GLOBALS['conf']['cache']['default_lifetime']); $this->_permsCache[$name] = $perm; } else { $this->_permsCache[$name] = unserialize($perm); @@ -149,8 +156,8 @@ class Horde_Perms_Datatree extends Horde_Perms if (empty($name)) { throw Horde_Perms_Exception('Permission names must be non-empty.'); } - $this->_cache->expire('perm_' . $name); - $this->_cache->expire('perm_exists_' . $name); + $this->_cache->expire('perm_' . $this->_cacheVersion . $name); + $this->_cache->expire('perm_exists_' . $this->_cacheVersion . $name); return $this->_datatree->add($perm); } @@ -171,8 +178,8 @@ class Horde_Perms_Datatree extends Horde_Perms $keys = $this->_datatree->get(DATATREE_FORMAT_FLAT, $perm->name, true); foreach ($keys as $key) { - $this->_cache->expire('perm_' . $key); - $this->_cache->expire('perm_exists_' . $key); + $this->_cache->expire('perm_' . $this->_cacheVersion . $key); + $this->_cache->expire('perm_exists_' . $this->_cacheVersion . $key); } return $this->_datatree->remove($perm->name, $force); @@ -201,7 +208,7 @@ class Horde_Perms_Datatree extends Horde_Perms */ public function exists($permission) { - $key = 'perm_exists_' . $permission; + $key = 'perm_exists_' . $this->_cacheVersion . $permission; $exists = $this->_cache->get($key, $GLOBALS['conf']['cache']['default_lifetime']); if ($exists === false) { $exists = $this->_datatree->exists($permission); diff --git a/framework/Perms/lib/Horde/Perms/Sql.php b/framework/Perms/lib/Horde/Perms/Sql.php index f04e25149..1e0c356de 100644 --- a/framework/Perms/lib/Horde/Perms/Sql.php +++ b/framework/Perms/lib/Horde/Perms/Sql.php @@ -44,6 +44,13 @@ class Horde_Perms_Sql extends Horde_Perms protected $_cache; /** + * Incrementing version number if cached classes change. + * + * @var integer + */ + private $_cacheVersion = 2; + + /** * Cache of previously retrieved permissions. * * @var array @@ -103,7 +110,7 @@ class Horde_Perms_Sql extends Horde_Perms $this->_connect(); - $perm = $this->_cache->get('perm_sql' . $name, $GLOBALS['conf']['cache']['default_lifetime']); + $perm = $this->_cache->get('perm_sql' . $this->_cacheVersion . $name, $GLOBALS['conf']['cache']['default_lifetime']); if (empty($perm)) { $query = 'SELECT perm_id, perm_data FROM horde_perms WHERE perm_name = ?'; $result = $this->_db->getRow($query, array($name), DB_FETCHMODE_ASSOC); @@ -118,7 +125,7 @@ class Horde_Perms_Sql extends Horde_Perms $object->setId($result['perm_id']); $object->setData(unserialize($result['perm_data'])); - $this->_cache->set('perm_sql' . $name, serialize($object)); + $this->_cache->set('perm_sql' . $this->_cacheVersion . $name, serialize($object)); $this->_permsCache[$name] = $object; } else { @@ -185,8 +192,8 @@ class Horde_Perms_Sql extends Horde_Perms throw new Horde_Perms_Exception('Permission name must be non-empty.'); } - $this->_cache->expire('perm_sql' . $name); - $this->_cache->expire('perm_sql_exists_' . $name); + $this->_cache->expire('perm_sql' . $this->_cacheVersion . $name); + $this->_cache->expire('perm_sql_exists_' . $this->_cacheVersion . $name); $this->_connect(); $id = $this->_write_db->nextId('horde_perms'); @@ -239,8 +246,8 @@ class Horde_Perms_Sql extends Horde_Perms } $name = $perm->getName(); - $this->_cache->expire('perm_sql' . $name); - $this->_cache->expire('perm_sql_exists_' . $name); + $this->_cache->expire('perm_sql' . $this->_cacheVersion . $name); + $this->_cache->expire('perm_sql_exists_' . $this->_cacheVersion . $name); $this->_connect(); $query = 'DELETE FROM horde_perms WHERE perm_name = ?'; @@ -284,7 +291,7 @@ class Horde_Perms_Sql extends Horde_Perms */ public function exists($permission) { - $key = 'perm_sql_exists_' . $permission; + $key = 'perm_sql_exists_' . $this->_cacheVersion . $permission; $exists = $this->_cache->get($key, $GLOBALS['conf']['cache']['default_lifetime']); if ($exists === false) { $this->_connect(); -- 2.11.0