/** * Store a response generated by a request. * * @param Request $request * @param Response $response * @param integer $ttl */ public function cache(Request $request, Response $response, $ttl = null) { // only cache GET and HEAD requests if (!in_array($request->getMethod(), array('GET', 'HEAD'))) { return; } // respect Cache-Control: no-cache if ($request->isNoCache()) { return; } // skip already cached response if ($response->headers->has('X-ServerCache-Key')) { return; } // set cache lifetime if (is_null($ttl)) { $ttl = $this->defaultTtl; } $expirationDate = date_create('NOW + ' . $ttl . ' seconds'); // save cache item $key = $this->getCacheKey($request); $metadataReponse = new MetadataResponse($response, array("expirationDate" => $expirationDate)); $item = new CacheItem($key, true, $metadataReponse); $item->expiresAt($expirationDate); $this->cache->save($item); }
public function testExpiresAfter() { $item = new CacheItem('test_key'); $this->assertNull($item->getExpirationDate()); $item->expiresAfter(null); $this->assertNull($this->getExpectedException()); $item->expiresAfter(new \DateInterval('PT1S')); $this->assertEquals(new \DateTime('+1 second'), $item->getExpirationDate()); $item->expiresAfter(1); $this->assertEquals(new \DateTime('+1 second'), $item->getExpirationDate()); }
protected function fetchObjectFromCache($key) { $object = $this->collection->findOne(['_id' => $key]); if ($object && isset($object->data)) { $item = new CacheItem($key, true, unserialize($object->data)); if (isset($object->expiresAt)) { $item->expiresAt($object->expiresAt->toDateTime()); } return $item; } return false; }
/** * @inheritDoc */ public function put(string $key, $value, $expires) : bool { $item = new CacheItem($key); $item->set($value); if (is_int($expires) || $expires instanceof \DateInterval) { $item->expiresAfter($expires); } elseif ($expires instanceof \DateTimeInterface) { // @codeCoverageIgnore $item->expiresAt($expires); } return $this->pool->save($item); }
/** * @param Cacheable $annotation * @param InterceptionSuffixInterface $interception * * @return ResultInterface */ public function interceptSuffix(CacheAnnotationInterface $annotation, InterceptionSuffixInterface $interception) : ResultInterface { if (!$interception->getReturnValue()) { return new EmptyResult(); } $key = $this->calculateKey($annotation, $interception); $item = new CacheItem($key); $item->set($interception->getReturnValue()); $item->setTags($annotation->tags); if ($annotation->ttl > 0) { $item->expiresAfter($annotation->ttl); } foreach ($annotation->pools as $poolName) { $pool = $this->poolManager->getPool($poolName); $pool->save($item); } return new MissResult($interception, $key, $annotation->pools); }
/** * @param CacheItem|TaggableItemInterface $item * @return mixed */ protected function decodeItem($item) { if ($item->isHit()) { return $item->get(); } return null; }