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; } }
public function jobAction() { //获取产品id $cache_dir = APP_PATH . '/storage/cache/'; $frontCache = new FrontData(array("lifetime" => 3600 * 240)); $cache = new BackFile($frontCache, array("cacheDir" => $cache_dir)); foreach (glob($cache_dir . '*') as $filepath) { $filename = pathinfo($filepath, PATHINFO_FILENAME); $cacheKey = $filename; if ($cache->exists($cacheKey)) { $arr = json_decode($cache->get($cacheKey), true); if (isset($arr['file_path']) && empty($arr['file_path'])) { echo 'product id is ' . $arr['id'] . PHP_EOL; $arr['file_path'] = $this->createFile($arr['id']); $cache->save($cacheKey, json_encode($arr)); } } $this->page = 1; } echo 'it\'s time to have a rest' . PHP_EOL; }
/** * Stores cached content * * @param string $keyName * @param string $content * @param int $lifetime * @param boolean $stopBuffer */ public function set($keyName = null, $content = null, $lifetime = null, $stopBuffer = null) { if ($this->cache_status) { $this->cache->save($keyName, $content, $lifetime, $stopBuffer); } }
} else { $response->setContent('正在下载评论,客官请稍等!'); } } else { $response->setStatusCode(404, "Not Found")->sendHeaders(); $response->setContent('菜菜搞错了,还没有添加!!'); } $response->send(); }); //添加商品id $app->get('/product/{id:[0-9]{9}}', function ($id) use($app) { $content = 'caicai 没找对id吧!!'; if (is_numeric($id) && $id > 0) { $id = '000000000' . $id; $frontCache = new FrontData(array("lifetime" => 3600 * 240)); $cache = new BackFile($frontCache, array("cacheDir" => "../app/storage/cache/")); $cacheKey = $id; $product = $cache->get($cacheKey); $content = '已经添加过了!菜菜笨'; if ($product === null) { $arr = ['id' => $id, 'file_path' => '']; $cache->save($cacheKey, json_encode($arr)); $content = '添加成功!!菜菜好样的'; } } $app->response->setContent($content); $app->response->send(); }); $app->notFound(function () use($app) { $app->response->setStatusCode(404, "Not Found")->sendHeaders(); echo 'This is crazy, but this page was not found!';
public function dataIgbinary(UnitTester $I) { if (!extension_loaded('igbinary')) { throw new \PHPUnit_Framework_SkippedTestError('Warning: igbinary extension is not loaded'); } $I->wantTo("Use File cache with Igbinary frontend"); $frontend = new Igbinary(['lifetime' => 600]); $backend = new File($frontend, ['cacheDir' => PATH_CACHE, 'prefix' => 'igbinary_']); $I->assertFalse($backend->isStarted()); $backend->save('test-data', 'nothing interesting'); $I->amInPath(PATH_CACHE); $I->seeFileFound('igbinary_' . $backend->getKey('test-data')); $I->seeFileContentsEqual(igbinary_serialize('nothing interesting')); $I->assertEquals('nothing interesting', $backend->get('test-data')); $backend->save('test-data', 'something interesting'); $I->assertEquals('something interesting', $backend->get('test-data')); $data = ['null' => null, 'array' => [1, 2, 3, 4 => 5], 'string', 123.45, 6, true, false, null, 0, ""]; $serialized = igbinary_serialize($data); $I->assertEquals($data, igbinary_unserialize($serialized)); $backend->save('test-data', $data); $I->assertEquals($data, $backend->get('test-data')); $I->assertTrue($backend->exists('test-data')); $I->assertTrue($backend->delete('test-data')); // Delete cache $I->dontSeeFileFound('igbinary_' . $backend->getKey('test-data')); }
public function useSafeKey($useSafeKey) { return parent::useSafeKey($useSafeKey); }
public static function set($key, $value, $ttl = null) { static::$cache === null and static::$cache = static::$di->getShared('cache'); static::$cache->save($key, $value, $ttl); }