From 48eb0e67e62fe0304c300e45ba0076f09ed89c9d Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Tue, 7 Oct 2014 21:07:50 +0100 Subject: [PATCH] FIX: Deliberately clear partial cache blocks on flush (fixes #1383) Move property to top of class definition Move property to top of class definition --- i18n/i18n.php | 2 +- tests/view/SSViewerCacheBlockTest.php | 12 ++++++++++ view/SSViewer.php | 34 +++++++++++++++++++++------ 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/i18n/i18n.php b/i18n/i18n.php index 4acc5eb1a..c6f8e3bac 100644 --- a/i18n/i18n.php +++ b/i18n/i18n.php @@ -101,7 +101,7 @@ class i18n extends Object implements TemplateGlobalProvider, Flushable { * Triggered early in the request when someone requests a flush. */ public static function flush() { - self::get_cache()->clean(Zend_Cache::CLEANING_MODE_ALL); + self::get_cache()->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('Zend_Translate')); } /** diff --git a/tests/view/SSViewerCacheBlockTest.php b/tests/view/SSViewerCacheBlockTest.php index 3c0cdfbae..6f7fd65a0 100644 --- a/tests/view/SSViewerCacheBlockTest.php +++ b/tests/view/SSViewerCacheBlockTest.php @@ -123,6 +123,18 @@ class SSViewerCacheBlockTest extends SapphireTest { $this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', array('Foo' => 1)), '1'); $this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', array('Foo' => 2)), '1'); } + + /** + * Test that the cacheblocks invalidate when a flush occurs. + */ + public function testBlocksInvalidateOnFlush() { + Director::test('/'); + $this->_reset(true); + $this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', array('Foo' => 1)), '1'); + + Director::test('/?flush=1'); + $this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', array('Foo' => 2)), '2'); + } public function testVersionedCache() { diff --git a/view/SSViewer.php b/view/SSViewer.php index f426b3d9c..c1cd81e3a 100644 --- a/view/SSViewer.php +++ b/view/SSViewer.php @@ -571,6 +571,16 @@ class SSViewer implements Flushable { */ private static $source_file_comments = false; + /** + * @ignore + */ + private static $template_cache_flushed = false; + + /** + * @ignore + */ + private static $cacheblock_cache_flushed = false; + /** * Set whether HTML comments indicating the source .SS file used to render this page should be * included in the output. This is enabled by default @@ -643,6 +653,7 @@ class SSViewer implements Flushable { */ public static function flush() { self::flush_template_cache(); + self::flush_cacheblock_cache(); } /** @@ -907,11 +918,6 @@ class SSViewer implements Flushable { return $founds[0]; } } - - /** - * @ignore - */ - static private $flushed = false; /** * Clears all parsed template files in the cache folder. @@ -919,12 +925,26 @@ class SSViewer implements Flushable { * Can only be called once per request (there may be multiple SSViewer instances). */ public static function flush_template_cache() { - if (!self::$flushed) { + if (!self::$template_cache_flushed) { $dir = dir(TEMP_FOLDER); while (false !== ($file = $dir->read())) { if (strstr($file, '.cache')) unlink(TEMP_FOLDER . '/' . $file); } - self::$flushed = true; + self::$template_cache_flushed = true; + } + } + + /** + * Clears all partial cache blocks. + * + * Can only be called once per request (there may be multiple SSViewer instances). + */ + public static function flush_cacheblock_cache() { + if (!self::$cacheblock_cache_flushed) { + $cache = SS_Cache::factory('cacheblock'); + $tags = $cache->getTags(); + $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, $tags); + self::$cacheblock_cache_flushed = true; } }