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; }
/** * Checks if cache exists and it isn't expired * * @param string $keyName * @param integer $lifetime * @return boolean */ public function exists($keyName = null, $lifetime = null) { return $this->cache->exists($keyName, $lifetime); }
}, true); $app = new Micro(); $app->setDI($di); $app->get('/test', function () use($app) { // echo $app->config->application->controllersDir; // echo $app->config->database->dbname; echo $app->mongo->selectCollection('restaurants')->count(); }); //下载对应csv文件 $app->get('/file/{pid:[0-9]+}', function ($pid) use($app) { $response = $app->response; $pid = '000000000' . $pid; $frontCache = new FrontData(array("lifetime" => 3600 * 240)); $cache = new BackFile($frontCache, array("cacheDir" => "../app/storage/cache/")); $cacheKey = $pid; if ($cache->exists($cacheKey)) { $arr = json_decode($cache->get($cacheKey), true); if (isset($arr['file_path']) && !empty($arr['file_path'])) { $content = file_get_contents($arr['file_path']); $response->setContentType('application/csv'); $response->setContent($content); $response->setHeader('Content-Disposition', 'attachment; filename=' . date('Y-m-d') . '---' . $arr['id'] . '.csv'); } else { $response->setContent('正在下载评论,客官请稍等!'); } } else { $response->setStatusCode(404, "Not Found")->sendHeaders(); $response->setContent('菜菜搞错了,还没有添加!!'); } $response->send(); });
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 exists($keyName = null, $lifetime = null) { return parent::exists($keyName, $lifetime); }