diff --git a/README.markdown b/README.markdown index fa1f7ed..c4ea440 100644 --- a/README.markdown +++ b/README.markdown @@ -89,9 +89,6 @@ are not (atomic counter). # if "tags" is NULL (~), it will # be the same as cache (e.i. sfMemcacheTaggingCache) - metadata: - class: CacheMetadata # this class responses to save/fetch data and tags - # from/to cache with custom serialization/de-serialization logger: class: sfFileCacheTagLogger # to disable logger, set class to "sfNoCacheTagLogger" param: @@ -250,19 +247,21 @@ are not (atomic counter). # 0: without micro time, version length 10 digits # 5: with micro time part, version length 15 digits # (allowed decimal numbers in range [0, 6] + + metadata_class: CacheMetadata # this class responses to save/fetch data and tags + # from/to cache with custom serialization/de-serialization #object_class_tag_name_provider: # you can customize tag name naming # # useful for multi-environment models # - ProjectToolkit # [class name] # - formatObjectClassName # [static method name] - Minified ``app.yml`` content: + Minified recommended ``app.yml`` content: all: sfcachetaggingplugin: template_lock: "%SF_ENVIRONMENT%_lock_%s" - template_tag: "%SF_ENVIRONMENT%_tag_%s" - microtime_precision: 5 + template_tag: "%SF_ENVIRONMENT%_tag_%s" ## Usage ## diff --git a/config/sfCacheTaggingPluginConfiguration.class.php b/config/sfCacheTaggingPluginConfiguration.class.php index 4b9d1b6..0b798e4 100644 --- a/config/sfCacheTaggingPluginConfiguration.class.php +++ b/config/sfCacheTaggingPluginConfiguration.class.php @@ -8,11 +8,6 @@ * file that was distributed with this source code. */ - if (! defined('E_USER_DEPRECATED')) - { - define('E_USER_DEPRECATED', E_USER_WARNING); - } - /** * sfCacheTaggingPlugin configuration * @@ -44,12 +39,11 @@ public function initialize () ); $manager->setAttribute( - Doctrine_Core::ATTR_RESULT_CACHE, new sfDoctrineProxyCache() + Doctrine_Core::ATTR_RESULT_CACHE, new Doctrine_Cache_Proxy() ); $manager->setAttribute( - Doctrine_Core::ATTR_QUERY_CLASS, - 'Cachetaggable_Doctrine_Query' + Doctrine_Core::ATTR_QUERY_CLASS, 'Doctrine_Query_Cachetaggable' ); $manager->setAttribute(Doctrine::ATTR_USE_DQL_CALLBACKS, true); diff --git a/lib/cache/extra/sfAPCTaggingCache.class.php b/lib/cache/drivers/sfAPCTaggingCache.class.php similarity index 81% rename from lib/cache/extra/sfAPCTaggingCache.class.php rename to lib/cache/drivers/sfAPCTaggingCache.class.php index 4a60708..af58cc6 100644 --- a/lib/cache/extra/sfAPCTaggingCache.class.php +++ b/lib/cache/drivers/sfAPCTaggingCache.class.php @@ -17,13 +17,11 @@ class sfAPCTaggingCache extends sfAPCCache { public function getCacheKeys () { + // apc_cache_info returns false, if APC is disabled + // no reasons to check it - all methods available after + // calling self::__construct $infos = apc_cache_info('user'); - if (! is_array($infos['cache_list'])) - { - return; - } - $keys = array(); foreach ($infos['cache_list'] as $info) diff --git a/lib/cache/extra/sfEAcceleratorTaggingCache.class.php b/lib/cache/drivers/sfEAcceleratorTaggingCache.class.php similarity index 100% rename from lib/cache/extra/sfEAcceleratorTaggingCache.class.php rename to lib/cache/drivers/sfEAcceleratorTaggingCache.class.php diff --git a/lib/cache/extra/sfFileTaggingCache.class.php b/lib/cache/drivers/sfFileTaggingCache.class.php similarity index 100% rename from lib/cache/extra/sfFileTaggingCache.class.php rename to lib/cache/drivers/sfFileTaggingCache.class.php diff --git a/lib/cache/extra/sfMemcacheTaggingCache.class.php b/lib/cache/drivers/sfMemcacheTaggingCache.class.php similarity index 100% rename from lib/cache/extra/sfMemcacheTaggingCache.class.php rename to lib/cache/drivers/sfMemcacheTaggingCache.class.php diff --git a/lib/cache/extra/sfSQLiteTaggingCache.class.php b/lib/cache/drivers/sfSQLiteTaggingCache.class.php similarity index 100% rename from lib/cache/extra/sfSQLiteTaggingCache.class.php rename to lib/cache/drivers/sfSQLiteTaggingCache.class.php diff --git a/lib/cache/extra/sfXCacheTaggingCache.class.php b/lib/cache/drivers/sfXCacheTaggingCache.class.php similarity index 100% rename from lib/cache/extra/sfXCacheTaggingCache.class.php rename to lib/cache/drivers/sfXCacheTaggingCache.class.php diff --git a/lib/cache/sfTaggingCache.class.php b/lib/cache/sfTaggingCache.class.php index 5826233..4a630e0 100644 --- a/lib/cache/sfTaggingCache.class.php +++ b/lib/cache/sfTaggingCache.class.php @@ -274,7 +274,7 @@ public function remove ($key) { $cacheMetadata = $this->getDataCache()->get($key); - $cacheMetadataClassName = $this->getMetadataClassName(); + $cacheMetadataClassName = sfCacheTaggingToolkit::getMetadataClassName(); if ($cacheMetadata instanceof $cacheMetadataClassName) { @@ -343,7 +343,7 @@ public function addTagsToCache ($key, array $tags, $append = true) { $cacheMetadata = $this->getDataCache()->get($key); - $cacheMetadataClassName = $this->getMetadataClassName(); + $cacheMetadataClassName = sfCacheTaggingToolkit::getMetadataClassName(); if ($cacheMetadata instanceof $cacheMetadataClassName) { @@ -375,7 +375,7 @@ public function addTagsToCache ($key, array $tags, $append = true) */ public function set ($key, $data, $timeout = null, array $tags = array()) { - $cacheMetadataClassName = $this->getMetadataClassName(); + $cacheMetadataClassName = sfCacheTaggingToolkit::getMetadataClassName(); $cacheMetadata = new $cacheMetadataClassName($data, $tags); @@ -482,7 +482,7 @@ public function getTags ($key) { $value = $this->getDataCache()->get($key); - $cacheMetadataClassName = $this->getMetadataClassName(); + $cacheMetadataClassName = sfCacheTaggingToolkit::getMetadataClassName(); if ($value instanceof $cacheMetadataClassName) { @@ -536,7 +536,7 @@ public function get ($key, $default = null) { $cacheMetadata = $this->getDataCache()->get($key, $default); - $cacheMetadataClassName = $this->getMetadataClassName(); + $cacheMetadataClassName = sfCacheTaggingToolkit::getMetadataClassName(); # check data exist in cache and data content is a tags container if ($cacheMetadata instanceof $cacheMetadataClassName) @@ -672,7 +672,11 @@ public function clean ($mode = sfCache::ALL) protected function generateLockKey ($key) { return sprintf( - sfConfig::get('app_sfcachetaggingplugin_template_lock', 'lock_%s'), $key + sfConfig::get( + 'app_sfcachetaggingplugin_template_lock', + sprintf('%s_lock', sfConfig::get('sf_environment') . '_%s') + ), + $key ); } @@ -685,7 +689,11 @@ protected function generateLockKey ($key) protected function generateTagKey ($key) { return sprintf( - sfConfig::get('app_sfcachetaggingplugin_template_tag', 'tag_%s'), $key + sfConfig::get( + 'app_sfcachetaggingplugin_template_tag', + sprintf('%s_tag', sfConfig::get('sf_environment') . '_%s') + ), + $key ); } @@ -699,17 +707,6 @@ public function getContentTagHandler () return $this->contentTagHandler; } - /** - * Return option "metadata.class". - * If not set, returns default class name "CacheMetadata". - * - * @return string - */ - protected function getMetadataClassName () - { - return $this->getOption('metadata.class', 'CacheMetadata'); - } - /** * @return array registered keys in storage */ diff --git a/lib/util/sfCacheTaggingToolkit.class.php b/lib/util/sfCacheTaggingToolkit.class.php index 88ce317..7a51594 100644 --- a/lib/util/sfCacheTaggingToolkit.class.php +++ b/lib/util/sfCacheTaggingToolkit.class.php @@ -88,6 +88,14 @@ public static function getPrecision () return $presision; } + /** + * @return string + */ + public static function getMetadataClassName () + { + return sfConfig::get('app_sfcachetaggingplugin_metadata_class', 'CacheMetadata'); + } + /** * Format passed tags to the array * @@ -174,12 +182,22 @@ public static function listenOnComponentMethodNotFoundEvent (sfEvent $event) { $taggingCache = sfCacheTaggingToolkit::getTaggingCache(); } - catch (sfException $e) + catch (sfCacheDisabledException $e) { - sfCacheTaggingToolkit::notifyApplicationLog($e->getMessage(), sfLogger::NOTICE); + sfCacheTaggingToolkit::notifyApplicationLog( + __CLASS__, $e->getMessage(), sfLogger::NOTICE + ); return; } + catch (sfConfigurationException $e) + { + sfCacheTaggingToolkit::notifyApplicationLog( + __CLASS__, $e->getMessage(), sfLogger::WARNING + ); + + return; + } try { @@ -188,7 +206,8 @@ public static function listenOnComponentMethodNotFoundEvent (sfEvent $event) ); $event - ->setReturnValue(call_user_func_array($callable, $event['arguments'])); + ->setReturnValue(call_user_func_array($callable, $event['arguments'])) + ; } catch (BadMethodCallException $e) { @@ -228,11 +247,16 @@ public static function getBaseClassName ($className) return $className; } - public static function notifyApplicationLog ($message, $priority = null) + /** + * @param mixed $object object or string, or null + * @param string $message + * @param int $priority sfLogger::* see constants + */ + public static function notifyApplicationLog ($object, $message, $priority = null) { ProjectConfiguration::getActive() ->getEventDispatcher() - ->notify(new sfEvent($this, 'application.log', array($message, $priority))) + ->notify(new sfEvent($object, 'application.log', array($message, $priority))) ; } } \ No newline at end of file diff --git a/lib/doctrine/cache/sfDoctrineProxyCache.class.php b/lib/vendor/Doctrine/Cache/Proxy.php similarity index 80% rename from lib/doctrine/cache/sfDoctrineProxyCache.class.php rename to lib/vendor/Doctrine/Cache/Proxy.php index 2f00d24..ca6f43a 100644 --- a/lib/doctrine/cache/sfDoctrineProxyCache.class.php +++ b/lib/vendor/Doctrine/Cache/Proxy.php @@ -17,7 +17,7 @@ * @subpackage doctrine * @author Ilya Sabelnikov */ - class sfDoctrineProxyCache extends Doctrine_Cache_Driver + class Doctrine_Cache_Proxy extends Doctrine_Cache_Driver { /** * Short method to retrieve sfTaggingCache for internal use @@ -42,7 +42,9 @@ protected function _doSave ($id, $data, $ttl = false) } catch (sfCacheDisabledException $e) { - # be silent - sf_cache is disabled for this environment + sfCacheTaggingToolkit::notifyApplicationLog( + __CLASS__, $e->getMessage(), sfLogger::NOTICE + ); } return false; @@ -62,7 +64,9 @@ protected function _doSaveWithTags ($id, $data, $ttl, $tags) } catch (sfCacheDisabledException $e) { - # be silent - sf_cache is disabled for this environment + sfCacheTaggingToolkit::notifyApplicationLog( + __CLASS__, $e->getMessage(), sfLogger::NOTICE + ); } return false; @@ -79,7 +83,9 @@ protected function _getCacheKeys () } catch (sfCacheDisabledException $e) { - sfCacheTaggingToolkit::notifyApplicationLog($e->getMessage(), sfLogger::NOTICE); + sfCacheTaggingToolkit::notifyApplicationLog( + __CLASS__, $e->getMessage(), sfLogger::NOTICE + ); } return; @@ -97,7 +103,9 @@ protected function _doDelete ($id) } catch (sfCacheDisabledException $e) { - # be silent - sf_cache is disabled for this environment + sfCacheTaggingToolkit::notifyApplicationLog( + __CLASS__, $e->getMessage(), sfLogger::NOTICE + ); } return false; @@ -115,7 +123,9 @@ protected function _doContains ($id) } catch (sfCacheDisabledException $e) { - # be silent - sf_cache is disabled for this environment + sfCacheTaggingToolkit::notifyApplicationLog( + __CLASS__, $e->getMessage(), sfLogger::NOTICE + ); } return false; @@ -135,7 +145,9 @@ protected function _doFetch ($id, $testCacheValidity = true) } catch (sfCacheDisabledException $e) { - # be silent - sf_cache is disabled for this environment + sfCacheTaggingToolkit::notifyApplicationLog( + __CLASS__, $e->getMessage(), sfLogger::NOTICE + ); } return false; diff --git a/lib/doctrine/collection/Cachetaggable.class.php b/lib/vendor/Doctrine/Collection/Cachetaggable.php similarity index 93% rename from lib/doctrine/collection/Cachetaggable.class.php rename to lib/vendor/Doctrine/Collection/Cachetaggable.php index ef88478..edfdcd6 100644 --- a/lib/doctrine/collection/Cachetaggable.class.php +++ b/lib/vendor/Doctrine/Collection/Cachetaggable.php @@ -94,7 +94,7 @@ public function getTags ($deep = false) } catch (sfCacheDisabledException $e) { - sfCacheTaggingToolkit::notifyApplicationLog($e->getMessage(), sfLogger::NOTICE); + $this->notifyApplicationLog($e); return array(); } @@ -181,7 +181,7 @@ public function addTags ($tags) } catch (sfCacheDisabledException $e) { - sfCacheTaggingToolkit::notifyApplicationLog($e->getMessage(), sfLogger::NOTICE); + $this->notifyApplicationLog($e); } } @@ -202,7 +202,7 @@ public function addTag ($tagName, $tagVersion) } catch (sfCacheDisabledException $e) { - sfCacheTaggingToolkit::notifyApplicationLog($e->getMessage(), sfLogger::NOTICE); + $this->notifyApplicationLog($e); } } @@ -221,7 +221,7 @@ public function removeTags () } catch (sfCacheDisabledException $e) { - sfCacheTaggingToolkit::notifyApplicationLog($e->getMessage(), sfLogger::NOTICE); + $this->notifyApplicationLog($e); } } @@ -239,10 +239,17 @@ public function delete (Doctrine_Connection $conn = null, $clearColl = true) } catch (sfCacheDisabledException $e) { - sfCacheTaggingToolkit::notifyApplicationLog($e->getMessage(), sfLogger::NOTICE); + $this->notifyApplicationLog($e); } return $returnValue; } + + protected function notifyApplicationLog (Exception $e) + { + sfCacheTaggingToolkit::notifyApplicationLog( + $this, $e->getMessage(), sfLogger::NOTICE + ); + } } diff --git a/lib/doctrine/query/Cachetaggable_Doctrine_Query.class.php b/lib/vendor/Doctrine/Query/Cachetaggable.php similarity index 96% rename from lib/doctrine/query/Cachetaggable_Doctrine_Query.class.php rename to lib/vendor/Doctrine/Query/Cachetaggable.php index 2ea1451..f7d7385 100644 --- a/lib/doctrine/query/Cachetaggable_Doctrine_Query.class.php +++ b/lib/vendor/Doctrine/Query/Cachetaggable.php @@ -20,7 +20,7 @@ * @param array $params * @return Doctrine_Collection the root collection */ - class Cachetaggable_Doctrine_Query extends Doctrine_Query + class Doctrine_Query_Cachetaggable extends Doctrine_Query { /** * @see Doctrine_Query::execute() @@ -71,7 +71,7 @@ public function execute ($params = array(), $hydrationMode = null) $cached = $this->getCachedForm($result); if ( - $cacheDriver instanceof sfDoctrineProxyCache + $cacheDriver instanceof Doctrine_Cache_Proxy && $result instanceof Doctrine_Collection_Cachetaggable) { diff --git a/lib/doctrine/template/Cachetaggable.class.php b/lib/vendor/Doctrine/Template/Cachetaggable.class.php similarity index 100% rename from lib/doctrine/template/Cachetaggable.class.php rename to lib/vendor/Doctrine/Template/Cachetaggable.class.php diff --git a/lib/doctrine/listener/Cachetaggable.class.php b/lib/vendor/Doctrine/Template/Listener/Cachetaggable.php similarity index 99% rename from lib/doctrine/listener/Cachetaggable.class.php rename to lib/vendor/Doctrine/Template/Listener/Cachetaggable.php index 16b4f63..38d56bf 100644 --- a/lib/doctrine/listener/Cachetaggable.class.php +++ b/lib/vendor/Doctrine/Template/Listener/Cachetaggable.php @@ -84,7 +84,7 @@ public function postDelete (Doctrine_Event $event) } catch (sfCacheException $e) { - + } $this->preDeleteTagName = null; diff --git a/lib/view/sfViewCacheTagManager.class.php b/lib/view/sfViewCacheTagManager.class.php index f94d532..df26b8b 100644 --- a/lib/view/sfViewCacheTagManager.class.php +++ b/lib/view/sfViewCacheTagManager.class.php @@ -512,32 +512,34 @@ public function decorateContentWithDebug (sfEvent $event, $content) $hash = $this->generateCacheKey($event['uri']) ); - if ( - $cacheMetadata instanceof stdClass - && - isset($cacheMetadata->tags) - && - is_array($cacheMetadata->tags) - ) + $cacheMetadataClassName = sfCacheTaggingToolkit::getMetadataClassName(); + + /* @var $cacheMetadata CacheMetadata */ + if (! $cacheMetadata instanceof $cacheMetadataClassName) { - $tags = sprintf('[cache tags] count: %d', $tagsCount = count($cacheMetadata->tags)); + return $content; + } - if (0 != $tagsCount) - { - $tags .= ', tags:'; + $tags = $cacheMetadata->getTags(); + $tagsContent = sprintf('[cache tags] count: %d', $tagsCount = count($tags)); - foreach ($cacheMetadata->tags as $name => $version) - { - $tags .= sprintf(' %s(%s),', $name, $version); - } + if (0 != $tagsCount) + { + $tagsContent .= ', tags:'; - $tags = substr($tags, 0, -1) . '.'; + foreach ($tags as $name => $version) + { + $tagsContent .= sprintf(' %s(%s),', $name, $version); } - $updatedContent = str_replace(' 
 ', "{$tags} 
 ", $updatedContent); + $tagsContent = substr($tagsContent, 0, -1) . '.'; } - return $updatedContent; + $textToReplace = ' 
 '; + + return str_replace( + $textToReplace, "{$tagsContent}{$textToReplace}", $updatedContent + ); } /** diff --git a/package.xml b/package.xml index 233d5f5..df522de 100644 --- a/package.xml +++ b/package.xml @@ -1,13 +1,14 @@ - + sfCacheTaggingPlugin - pear.symfony-project.com + plugins.symfony-project.com Cache tagging plugin compatible with any cache backends - The ``sfCacheTaggingPlugin`` is a Symfony plugin, that helps to store cache with - associated tags and to keep cache content up-to-date based by incrementing tag - version when cache objects are edited/removed or new objects are ready to be a - part of cache content. + The ``sfCacheTaggingPlugin`` is a Symfony plugin, that helps to keep cached + content up-to-date without setting cache lifetime. + So-called "Smart caching", stores cache with fetched models tags and its versions. + Each time you save/update/delete objects, plugin increments object tag version and + all linked cache by specific tag name will automatically spoiled. Ilya Sabelnikov @@ -17,8 +18,8 @@ 2010-06-28 - 1.5.0 - 2.0.0 + 2.0.0 + 3.0.0 stable @@ -26,10 +27,14 @@ MIT - * ilya: [added] all tags now stored in symfony build in holder - * ilya: [added] more coverage tests (91%) - * ilya: [removed] all deprecated methods - * ilya: [added] added listener on sfComponents to enable short-handed methods to set tags from action or component (see README) + * ilya: [added] Storing doctrine objects/collections by using build-in Doctrine mechanism + * ilya: [added] Ability to specify differenct cache back-ends for data and tags cache + * ilya: [added] Easy customized logger + * ilya: [added] By switching cache off or migration to build-in cache manager will no + affect code functionality + * ilya: [added] cache info block (blue/yellow box) now display linked to cache + tags with its version + * ilya: [added] new tests - code coverage: XX%; @@ -40,36 +45,56 @@ - + + + + + + - + + + - - - - - - - - - - + + + - - - + + + + + + + + + + + + + + + + + + + + + + + + - @@ -84,16 +109,14 @@ - + - - @@ -103,12 +126,17 @@ - - + + - + + + + + + diff --git a/test/functional/frontend/sfDoctrineProxyCacheTest.php b/test/functional/frontend/DoctrineCacheProxyTest.php similarity index 98% rename from test/functional/frontend/sfDoctrineProxyCacheTest.php rename to test/functional/frontend/DoctrineCacheProxyTest.php index 323a7d9..1ddc1d6 100644 --- a/test/functional/frontend/sfDoctrineProxyCacheTest.php +++ b/test/functional/frontend/DoctrineCacheProxyTest.php @@ -21,7 +21,7 @@ /* @var $cacheManager sfViewCacheTagManager */ $q = BlogPostTable::getInstance()->createQuery('p'); - $t->isa_ok($q->getResultCacheDriver(), 'sfDoctrineProxyCache', 'instance of Proxy'); + $t->isa_ok($q->getResultCacheDriver(), 'Doctrine_Cache_Proxy', 'instance of Proxy'); $connection->beginTransaction();