diff --git a/modules/cms/classes/CmsObject.php b/modules/cms/classes/CmsObject.php index a62690e87..8b6bfccaf 100644 --- a/modules/cms/classes/CmsObject.php +++ b/modules/cms/classes/CmsObject.php @@ -12,8 +12,6 @@ use RecursiveDirectoryIterator; use RecursiveIteratorIterator; use ArrayAccess; use Exception; -use DateTime; -use DateInterval; /** * This is a base class for all CMS objects - content files, pages, partials and layouts. @@ -49,14 +47,6 @@ class CmsObject implements ArrayAccess */ protected $loadedFromCache = false; - /** - * @var int How 'fresh' the file is for Twig caching - * 0: Changed but not noticed/accessed - * 1: Changed and noticed/accessed, will change to 2 on next load - * 2: Unchanged - */ - protected $freshness = 0; - protected static $fillable = [ 'content', 'fileName' @@ -100,10 +90,6 @@ class CmsObject implements ArrayAccess $filePath = static::getFilePath($theme, $fileName); if (array_key_exists($filePath, ObjectMemoryCache::$cache)) { - if(ObjectMemoryCache::$cache[$filePath]->freshness == 1) { - ObjectMemoryCache::$cache[$filePath]->freshness = 2; - } - return ObjectMemoryCache::$cache[$filePath]; } @@ -123,20 +109,14 @@ class CmsObject implements ArrayAccess if ($cached !== false) { /* - * The cached item exists and successfully unserialized. + * The cached item exists and successfully unserialized. * Initialize the object from the cached data. */ - if($cached['freshness'] == 1) { - $cached['freshness'] = 2; - } - Cache::put($key, serialize($cached), $cached['expire']); - $obj = new static($theme); $obj->content = $cached['content']; $obj->fileName = $fileName; $obj->mtime = File::lastModified($filePath); $obj->loadedFromCache = true; - $obj->freshness = $cached['freshness']; $obj->initFromCache($cached); return ObjectMemoryCache::$cache[$filePath] = $obj; @@ -156,15 +136,12 @@ class CmsObject implements ArrayAccess $cached = [ 'mtime' => @File::lastModified($filePath), - 'content' => $obj->content, - 'freshness' => 0, - 'expire' => (new DateTime())->add(new DateInterval('PT'.Config::get('cms.parsedPageCacheTTL', 1440).'M')) + 'content' => $obj->content ]; $obj->loadedFromCache = false; - $obj->freshness = 0; $obj->initCacheItem($cached); - Cache::put($key, serialize($cached), $cached['expire']); + Cache::put($key, serialize($cached), Config::get('cms.parsedPageCacheTTL', 1440)); return ObjectMemoryCache::$cache[$filePath] = $obj; } @@ -206,7 +183,7 @@ class CmsObject implements ArrayAccess } /** - * Returns the maximum allowed path nesting level. + * Returns the maximum allowed path nesting level. * The default value is 2, meaning that files * can only exist in the root directory, or in a subdirectory. * @return mixed Returns the maximum nesting level or null if any level is allowed. @@ -318,37 +295,6 @@ class CmsObject implements ArrayAccess return $this->loadedFromCache; } - /** - * Returns true if the object is fresh for Twig. - * Waits until it is called and the object reloaded to change the object's freshness. - * This method is used by the CMS internally. - * @return boolean - */ - public function isFresh() { - $filePath = $this->getFullPath(); - $key = self::getObjectTypeDirName().crc32($filePath); - $cached = Cache::get($key, false); - - if($cached !== false) { - $cached = @unserialize($cached); - } - - if($this->freshness < 2) { - $this->freshness = 1; - if($cached !== false) { - $cached['freshness'] = 1; - Cache::put($key, serialize($cached), $cached['expire']); - } - return false; - } - - if($cached !== false) { - $cached['freshness'] = 1; - Cache::put($key, serialize($cached), $cached['expire']); - } - return true; - } - /** * Returns the Twig content string. */ @@ -497,7 +443,7 @@ class CmsObject implements ArrayAccess return $result; } - + /** * Returns the absolute file path. * @param $theme Specifies a theme the file belongs to. diff --git a/modules/cms/twig/Loader.php b/modules/cms/twig/Loader.php index 6a9f7d748..03d6dada5 100644 --- a/modules/cms/twig/Loader.php +++ b/modules/cms/twig/Loader.php @@ -57,6 +57,6 @@ class Loader implements Twig_LoaderInterface */ public function isFresh($name, $time) { - return $this->obj->isFresh(); + return $this->obj->isLoadedFromCache(); } } diff --git a/tests/unit/cms/classes/CmsObjectTest.php b/tests/unit/cms/classes/CmsObjectTest.php index 455c4bc93..24166c19a 100644 --- a/tests/unit/cms/classes/CmsObjectTest.php +++ b/tests/unit/cms/classes/CmsObjectTest.php @@ -124,77 +124,6 @@ class CmsObjectTest extends TestCase $this->assertNull($obj); } - public function testFreshness() - { - $theme = Theme::load('test'); - $themePath = $theme->getPath(); - - $filePath1 = $themePath . '/temporary/test.htm'; - if (file_exists($filePath1)) - @unlink($filePath1); - $filePath2 = $themePath . '/temporary/test1.htm'; - if (file_exists($filePath2)) - @unlink($filePath2); - - $this->assertFileNotExists($filePath1); - $this->assertFileNotExists($filePath2); - - file_put_contents($filePath1, '
Test content 1
'); - file_put_contents($filePath2, 'Test content 2
'); - - /* - * First try - both objects should not be fresh - */ - $obj1 = TestTemporaryCmsObject::loadCached($theme, 'test.htm'); - $this->assertEquals($obj1->content, 'Test content 1
'); - $this->assertFalse($obj1->isFresh()); - $obj2 = TestTemporaryCmsObject::loadCached($theme, 'test1.htm'); - $this->assertEquals($obj2->content, 'Test content 2
'); - $this->assertFalse($obj1->isFresh()); - $this->assertFalse($obj2->isFresh()); - - /* - * Second try - both objects should be fresh - */ - CmsObject::clearInternalCache(); - $obj1 = TestTemporaryCmsObject::loadCached($theme, 'test.htm'); - $this->assertTrue($obj1->isFresh()); - $obj2 = TestTemporaryCmsObject::loadCached($theme, 'test1.htm'); - $this->assertTrue($obj1->isFresh()); - $this->assertTrue($obj2->isFresh()); - - /* - * Modify the file. The first object should show as not fresh, until it is observed and reloaded. - */ - sleep(1); // Sleep a second in order to have the update file modification time - file_put_contents($filePath1, 'Updated test content
'); - - CmsObject::clearInternalCache(); - $obj1 = TestTemporaryCmsObject::loadCached($theme, 'test.htm'); - $this->assertEquals($obj1->content, 'Updated test content
'); - $obj2 = TestTemporaryCmsObject::loadCached($theme, 'test1.htm'); - $this->assertEquals($obj2->content, 'Test content 2
'); - $this->assertFalse($obj1->isFresh()); - $this->assertTrue($obj2->isFresh()); - $obj1 = TestTemporaryCmsObject::loadCached($theme, 'test.htm'); - $this->assertTrue($obj1->isFresh()); - - /* - * Modify the file again, but wait to look for change. The object should still show as not fresh, even after the second load. - */ - sleep(1); // Sleep a second in order to have the update file modification time - file_put_contents($filePath1, 'Updated test content again
'); - - CmsObject::clearInternalCache(); - $obj1 = TestTemporaryCmsObject::loadCached($theme, 'test.htm'); - $this->assertEquals($obj1->content, 'Updated test content again
'); - $obj2 = TestTemporaryCmsObject::loadCached($theme, 'test1.htm'); - $this->assertEquals($obj2->content, 'Test content 2
'); - $this->assertTrue($obj2->isFresh()); - $obj1 = TestTemporaryCmsObject::loadCached($theme, 'test.htm'); - $this->assertFalse($obj1->isFresh()); - } - public function testFillFillable() { $theme = Theme::load('apitest'); @@ -408,4 +337,4 @@ class CmsObjectTest extends TestCase $this->assertFileExists($destFilePath); $this->assertEquals($testContents, file_get_contents($destFilePath)); } -} +} \ No newline at end of file