/** * 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 testExpiresAt() { $item = new CacheItem('test_key'); $this->assertNull($item->getExpirationDate()); $item->expiresAt(new \DateTime('+1 second')); $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); }