Skip to content

Commit

Permalink
FIX: Deliberately clear partial cache blocks on flush (fixes #1383)
Browse files Browse the repository at this point in the history
Move property to top of class definition

Move property to top of class definition
  • Loading branch information
kinglozzer committed Oct 9, 2014
1 parent b58d42f commit 48eb0e6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion i18n/i18n.php
Expand Up @@ -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'));
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/view/SSViewerCacheBlockTest.php
Expand Up @@ -123,6 +123,18 @@ public function testBlocksCache() {
$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() {

Expand Down
34 changes: 27 additions & 7 deletions view/SSViewer.php
Expand Up @@ -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
Expand Down Expand Up @@ -643,6 +653,7 @@ public static function get_source_file_comments() {
*/
public static function flush() {
self::flush_template_cache();
self::flush_cacheblock_cache();
}

/**
Expand Down Expand Up @@ -907,24 +918,33 @@ public static function getTemplateFileByType($identifier, $type) {
return $founds[0];
}
}

/**
* @ignore
*/
static private $flushed = false;

/**
* Clears all parsed template files in the cache folder.
*
* 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);

This comment has been minimized.

Copy link
@halkyon

halkyon Oct 15, 2014

Contributor

Hrm. This seems to break if you're using a backend like APC. Zend_Cache_Exception: tags are not supported by the current backend. We need to find a way to make that work still. Maybe it just clears out everything if the backend doesn't support tags?

This comment has been minimized.

Copy link
@kinglozzer

kinglozzer Oct 15, 2014

Author Member

Oh 💩. I’ve had a look and I think that’s the only real solution to this. Presumably the same thing happens for i18n::flush()? Will test.

This comment has been minimized.

Copy link
@kinglozzer

kinglozzer Oct 15, 2014

Author Member

Installed APC and sorted it: #3558 :)

self::$cacheblock_cache_flushed = true;
}
}

Expand Down

0 comments on commit 48eb0e6

Please sign in to comment.