|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace StadLine\ExecutionCacheBundle\Cache; |
| 4 | + |
| 5 | +use Cache\Adapter\Common\CacheItem; |
| 6 | +use Psr\Cache\CacheItemPoolInterface; |
| 7 | +use Symfony\Component\HttpFoundation\Request; |
| 8 | +use Symfony\Component\HttpFoundation\Response; |
| 9 | + |
| 10 | +/** |
| 11 | + * Default cache storage implementation |
| 12 | + */ |
| 13 | +class Storage |
| 14 | +{ |
| 15 | + /** @var string */ |
| 16 | + protected $keyPrefix; |
| 17 | + |
| 18 | + /** @var CacheItemPoolInterface Cache used to store cache data */ |
| 19 | + protected $cache; |
| 20 | + |
| 21 | + /** @var int Default cache TTL */ |
| 22 | + protected $defaultTtl; |
| 23 | + |
| 24 | + /** @var KeyProvider Alternative CacheKey provider */ |
| 25 | + protected $keyProvider; |
| 26 | + |
| 27 | + /** |
| 28 | + * @param CacheItemPoolInterface $cache Cache used to store cache data |
| 29 | + * @param string $keyPrefix Provide an optional key prefix to prefix on all cache keys |
| 30 | + * @param int $defaultTtl Default cache TTL |
| 31 | + */ |
| 32 | + public function __construct(CacheItemPoolInterface $cache, $keyPrefix = '', $defaultTtl = 3600) |
| 33 | + { |
| 34 | + $this->cache = $cache; |
| 35 | + $this->keyPrefix = $keyPrefix; |
| 36 | + $this->defaultTtl = $defaultTtl; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @param KeyProvider $keyProvider |
| 41 | + */ |
| 42 | + public function setKeyProvider(KeyProvider $keyProvider) |
| 43 | + { |
| 44 | + $this->keyProvider = $keyProvider; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param Request $request |
| 49 | + * @param Response $response |
| 50 | + */ |
| 51 | + public function cache(Request $request, Response $response) |
| 52 | + { |
| 53 | + if (!in_array($request->getMethod(), array('GET', 'HEAD'))) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if ($request->isNoCache()) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if ($response->headers->has('X-ServerCache-Key')) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + $key = $this->getCacheKey($request); |
| 66 | + $expirationDate = date_create('NOW + ' . $this->defaultTtl . ' seconds'); |
| 67 | + |
| 68 | + $item = new CacheItem($key, true, $response, $expirationDate); |
| 69 | + $this->cache->save($item); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @param Request $request |
| 74 | + */ |
| 75 | + public function delete(Request $request) |
| 76 | + { |
| 77 | + $key = $this->getCacheKey($request); |
| 78 | + |
| 79 | + $this->cache->deleteItem($key); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param type $url |
| 84 | + * @throws \BadMethodCallException |
| 85 | + */ |
| 86 | + public function purge($url) |
| 87 | + { |
| 88 | + throw new \BadMethodCallException('Not implemented'); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param Request $request |
| 93 | + * @return Response |
| 94 | + */ |
| 95 | + public function fetch(Request $request) |
| 96 | + { |
| 97 | + $key = $this->getCacheKey($request); |
| 98 | + |
| 99 | + $item = $this->cache->getItem($key); |
| 100 | + $response = $item->get(); |
| 101 | + |
| 102 | + if ($response instanceof Response) { |
| 103 | + $expirationDate = $item->getExpirationDate(); |
| 104 | + $ttl = $expirationDate->getTimestamp() - date_create('NOW')->getTimestamp(); |
| 105 | + |
| 106 | + $response->headers->set('X-ServerCache-Key', $key); |
| 107 | + $response->headers->set('X-ServerCache-Expires', $ttl); |
| 108 | + } |
| 109 | + |
| 110 | + return $response; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Hash a request URL into a string that returns cache metadata |
| 115 | + * |
| 116 | + * @param Request $request |
| 117 | + * @return string |
| 118 | + */ |
| 119 | + protected function getCacheKey(Request $request) |
| 120 | + { |
| 121 | + if ($this->keyProvider) { |
| 122 | + $key = $this->keyProvider->getCacheKey($request); |
| 123 | + } else { |
| 124 | + $key = md5($request->getMethod() . ' ' . $request->getUri()); |
| 125 | + } |
| 126 | + |
| 127 | + return $this->keyPrefix . $key; |
| 128 | + } |
| 129 | +} |
0 commit comments