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 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 save($key, ParsedExpression $expression) { $item = $this->cache->getItem(md5($key)); $item->set($expression); $this->cache->save($item); }
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()); }