public function outputFrontend(UnitTester $I) { $I->wantTo("Use File cache with Output frontend"); for ($i = 0; $i < 2; $i++) { $time = date('H:i:s'); $frontCache = new Output(['lifetime' => 2]); $cache = new File($frontCache, ['cacheDir' => PATH_CACHE, 'prefix' => 'unit_']); // on the second run set useSafeKey to true to test the compatibility toggle if ($i == 1) { $cache->useSafeKey(true); } $I->assertFalse($cache->isStarted()); ob_start(); // First time cache $content = $cache->start('test_output'); $I->assertTrue($cache->isStarted()); if ($content !== null) { $I->assertTrue(false); } echo $time; $cache->save(null, null, null, true); $obContent = ob_get_contents(); ob_end_clean(); $I->assertEquals($time, $obContent); $I->amInPath(PATH_CACHE); $I->seeFileFound('unit_' . $cache->getKey('test_output')); // Same cache $content = $cache->start('test_output'); $I->assertTrue($cache->isStarted()); if ($content === null) { $I->assertTrue(false); } $I->assertEquals($time, $obContent); // Refresh cache sleep(3); $time2 = date('H:i:s'); ob_start(); $content = $cache->start('test_output'); $I->assertTrue($cache->isStarted()); if ($content !== null) { $I->assertTrue(false); } echo $time2; $cache->save(null, null, null, true); $obContent2 = ob_get_contents(); ob_end_clean(); $I->assertNotEquals($time, $obContent2); $I->assertEquals($time2, $obContent2); // Check keys $actual = $cache->queryKeys(); $I->assertTrue(2 === count($actual)); $I->assertArrayHasKey('.gitignore', array_flip($actual)); $I->assertArrayHasKey('unit_' . $cache->getKey('test_output'), array_flip($actual)); $I->assertTrue($cache->exists('test_output')); $I->assertTrue($cache->delete('test_output')); // Delete cache $I->dontSeeFileFound('unit_' . $cache->getKey('test_output')); } }
public function cacheHTML($html = null, $realtime = false, $lifetime = null, $filename = '', $cacheDir = null) { // จัดเก็บไดเรคทอรี่เก็บไฟล์ Cache $_cacheDir = !empty($cacheDir) ? ROOT_PATH . $cacheDir : ROOT_PATH . $this->cacheDir; // กรณีไม่พบไดเรคทอรี่ ให้ดำเนินการสร้างไดเรคทอรี่ใหม่ (อัตโนมัติ) if (!is_dir($_cacheDir)) { mkdir($_cacheDir, 0777); } // กำหนดระยะเวลาการใช้งานไฟล์ Cache (หมดอายุ) $setCache = new CacheOutput(array('lifetime' => 86400 * !empty($lifetime) ? $lifetime : $this->cacheTime)); // กำหนดไดเรคทอรี่เก็บไฟล์ Cache $cache = new CacheFile($setCache, array('cacheDir' => $_cacheDir)); // กำหนดชื่อไฟล์ Cache $content = $cache->start(!empty($filename) ? $filename . $this->cacheFileType : $this->cacheFileName . $this->cacheFileType); // ตรวจสอบการทำงานแบบ Realtime if (empty($realtime)) { // ปิดการทำงานแบบ Realtime // ตรวจสอบข้อมูลคำสั่ง HTML if ($content === null) { // ไม่พบข้อมูลคำสั่ง HTML echo $html; $cache->save(); // จัดเก็บไฟล์ Cache } else { // พบข้อมูลคำสั่ง HTML echo $content; } } else { // เปิดการทำงานแบบ Realtime echo $html; } }
/** * Starts a cache. The keyName allows to identify the created fragment * * @param int|string $keyName * @param integer $lifetime * @return mixed */ public function start($keyName, $lifetime = null) { return $this->cache->start($keyName, $lifetime); }