public function testCacheHit() { $request = new Request(new Url('http://localhost.com/foo/bar'), 'GET'); $response = new Response(); $response->setBody(new StringStream()); $filters = array(); $filters[] = function ($request, $response, $filterChain) { $response->getBody()->write('foobar'); $filterChain->handle($request, $response); }; $filterChain = new FilterChain($filters); $filterChain->handle($request, $response); $cache = new Cache(new CacheHandler\Memory()); $item = $cache->getItem(md5('/foo/bar')); $item->set(array('headers' => array('Last-Modified' => 'Sat, 27 Dec 2014 15:54:49 GMT', 'Content-Type' => 'text/plain'), 'body' => 'foobar')); $cache->save($item); $filter = new StaticCache($cache); $filter->handle($request, $response, $filterChain); $result = $cache->getItem(md5('/foo/bar'))->get(); $this->assertArrayHasKey('headers', $result); $this->assertArrayHasKey('Content-Type', $result['headers']); $this->assertEquals('text/plain', $result['headers']['Content-Type']); $this->assertArrayHasKey('Last-Modified', $result['headers']); $this->assertEquals('Sat, 27 Dec 2014 15:54:49 GMT', $result['headers']['Last-Modified']); $this->assertArrayHasKey('body', $result); $this->assertEquals('foobar', $result['body']); }
public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context) { $key = md5($configuration->get('action')); $item = $this->cache->getItem($key); if (!$item->isHit()) { $response = $this->processor->execute($configuration->get('action'), $request, $context); $item->set($response, $configuration->get('expire')); $this->cache->save($item); } else { $response = $item->get(); } return $response; }
public function testGetCollection() { $cache = new Cache(new Cache\Handler\Memory()); $routing = new RoutingFile('tests/PSX/Loader/routes'); $routingParser = new CachedParser($routing, $cache); // we remove previous cache $cache->deleteItems([CachedParser::CACHE_KEY]); // get collection from the parser $collection = $routingParser->getCollection(); $this->assertInstanceOf('PSX\\Loader\\RoutingCollection', $collection); $this->assertEquals(14, count($collection)); // get collection from the cache $collection = $routingParser->getCollection(); $this->assertInstanceOf('PSX\\Loader\\RoutingCollection', $routingParser->getCollection()); $this->assertEquals(14, count($collection)); }
public function serve($services) { $services = $this->parse($services); if (empty($services)) { throw new Exception('No service selected'); } $key = 'asset-' . implode('-', $services); $cache = new Cache($key, $this->expire); if (($content = $cache->load()) === false) { foreach ($services as $srv) { $content .= $this->getContent($srv); } if ($this->config['psx_debug'] !== true) { $cache->write($content); } } header('Content-type: ' . $this->provider->getContentType()); return $content; }
/** * @httpMethod GET * @path /{service} * @nickname getApiDetails * @responseClass Declaration */ public function getApiDetails() { try { $basePath = $this->config['psx_url'] . '/' . $this->config['psx_dispatch'] . 'api'; $serviceName = strtolower($this->getUriFragments('service')); $cache = new Cache('swagger-api-detail-' . $serviceName); if (($declaration = $cache->load()) === false) { $declaration = new Declaration(Base::getVersion(), $basePath, null); $this->buildApiDetails($declaration, $serviceName); $cache->write(serialize($declaration)); } else { $declaration = unserialize($declaration); } $this->setResponse($declaration); } catch (\Exception $e) { $msg = new Message($e->getMessage(), false); $this->setResponse($msg); } }
public function fetch($key) { $item = $this->cache->getItem(md5($key)); return $item->isHit() ? $item->get() : null; }
public function testClear() { $cache = new Cache($this->getHandler()); $item = $cache->getItem('key'); $item->set('foobar'); $cache->save($item); $items = $cache->getItems(['key']); $this->assertArrayHasKey(0, $items); $item = $items[0]; $this->assertEquals('key', $item->getKey()); $this->assertInstanceOf('DateTime', $item->getExpiration()); $this->assertEquals(true, $item->isHit()); $this->assertEquals(true, $item->exists()); $this->assertEquals('foobar', $item->get()); $cache->clear(); $item = $cache->getItem('key'); $this->assertEquals('key', $item->getKey()); $this->assertInstanceOf('DateTime', $item->getExpiration()); $this->assertEquals(false, $item->isHit()); $this->assertEquals(false, $item->exists()); $this->assertEquals(null, $item->get()); }
public function getInlineContent() { $key = 'gadget-' . $this->id; $expire = (int) $this->expire; $cache = new Cache($key, $expire); if ($this->cache == 0 || ($content = $cache->load()) === false) { $content = $this->executeContent(); // if caching is enabled write the cache if ($this->cache == 1) { $cache->write($content); } } return $content; }