示例#1
0
 /**
  * Returns a cached content
  *
  * @param string $keyName
  * @param int $lifetime
  * @return mixed
  */
 public function get($keyName, $lifetime = null)
 {
     if ($this->cache_status) {
         return $this->cache->get($keyName, $lifetime);
     } else {
         return null;
     }
 }
示例#2
0
 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;
 }
示例#3
0
        }
    } 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!';
});
$app->error(function (\Exception $e) {
示例#4
0
 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'));
 }
示例#5
0
 public function get($keyName, $lifetime = null)
 {
     return parent::get($keyName, $lifetime);
 }
示例#6
0
 public static function get($key, $default = null)
 {
     static::$cache === null and static::$cache = static::$di->getShared('cache');
     return null === ($value = static::$cache->get($key)) ? $default : $value;
 }