BUG Fix flushing of SSViewer cache via testing

This commit is contained in:
Damian Mooyman 2014-10-14 09:36:24 +13:00
parent 97170dd42d
commit 793784e9d7
2 changed files with 20 additions and 14 deletions

View File

@ -128,18 +128,18 @@ class SSViewerCacheBlockTest extends SapphireTest {
* Test that the cacheblocks invalidate when a flush occurs.
*/
public function testBlocksInvalidateOnFlush() {
// Enable caching
Director::test('/?flush=1');
$this->_reset(true);
// This property must be false, or the flush won't occur. This can be affected by other tests
// if they trigger a flush via Flushable
$reflectionProp = new ReflectionProperty('SSViewer', 'cacheblock_cache_flushed');
$reflectionProp->setAccessible(true);
$reflectionProp->setValue(false);
// Generate cached value for foo = 1
$this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', array('Foo' => 1)), '1');
SSViewer::flush_cacheblock_cache();
// Test without flush
Director::test('/');
$this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', array('Foo' => 3)), '1');
// Test with flush
Director::test('/?flush=1');
$this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', array('Foo' => 2)), '2');
}

View File

@ -652,8 +652,8 @@ class SSViewer implements Flushable {
* Triggered early in the request when someone requests a flush.
*/
public static function flush() {
self::flush_template_cache();
self::flush_cacheblock_cache();
self::flush_template_cache(true);
self::flush_cacheblock_cache(true);
}
/**
@ -923,9 +923,12 @@ class SSViewer implements Flushable {
* Clears all parsed template files in the cache folder.
*
* Can only be called once per request (there may be multiple SSViewer instances).
*
* @param bool $force Set this to true to force a re-flush. If left to false, flushing
* may only be performed once a request.
*/
public static function flush_template_cache() {
if (!self::$template_cache_flushed) {
public static function flush_template_cache($force = false) {
if (!self::$template_cache_flushed || $force) {
$dir = dir(TEMP_FOLDER);
while (false !== ($file = $dir->read())) {
if (strstr($file, '.cache')) unlink(TEMP_FOLDER . '/' . $file);
@ -938,9 +941,12 @@ class SSViewer implements Flushable {
* Clears all partial cache blocks.
*
* Can only be called once per request (there may be multiple SSViewer instances).
*
* @param bool $force Set this to true to force a re-flush. If left to false, flushing
* may only be performed once a request.
*/
public static function flush_cacheblock_cache() {
if (!self::$cacheblock_cache_flushed) {
public static function flush_cacheblock_cache($force = false) {
if (!self::$cacheblock_cache_flushed || $force) {
$cache = SS_Cache::factory('cacheblock');
$tags = $cache->getTags();
$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, $tags);