|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Skautis\Nette; |
| 4 | + |
| 5 | +use Nette; |
| 6 | +use Nette\Caching\Cache; |
| 7 | +use Nette\Caching\IStorage; |
| 8 | +use Skautis\Wsdl\Decorator\Cache\CacheInterface; |
| 9 | + |
| 10 | + |
| 11 | +if (!class_exists('Nette\Utils\DateTime') && class_exists('Nette\DateTime')) { |
| 12 | + // BC with Nette 2.1 |
| 13 | + class_alias('Nette\DateTime', 'Nette\Utils\DateTime'); |
| 14 | +} |
| 15 | + |
| 16 | + |
| 17 | +/** |
| 18 | + * Nette cache adapter for Skautis library |
| 19 | + */ |
| 20 | +class CacheAdapter implements CacheInterface |
| 21 | +{ |
| 22 | + |
| 23 | + /** @var IStorage */ |
| 24 | + private $storage; |
| 25 | + |
| 26 | + /** @var string */ |
| 27 | + private $namespace; |
| 28 | + |
| 29 | + /** @var string|int|\DateTime */ |
| 30 | + private $expiration; |
| 31 | + |
| 32 | + |
| 33 | + /** |
| 34 | + * @param IStorage $storage |
| 35 | + * @param string|null $namespace |
| 36 | + */ |
| 37 | + public function __construct(IStorage $storage, $namespace = NULL) |
| 38 | + { |
| 39 | + $this->storage = $storage; |
| 40 | + $this->namespace = $namespace . Cache::NAMESPACE_SEPARATOR; |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * Returns cache storage. |
| 46 | + * @return IStorage |
| 47 | + */ |
| 48 | + public function getStorage() |
| 49 | + { |
| 50 | + return $this->storage; |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns cache namespace. |
| 56 | + * @return string |
| 57 | + */ |
| 58 | + public function getNamespace() |
| 59 | + { |
| 60 | + return (string) substr($this->namespace, 0, -1); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * @param \DateTime|int|string $expiration |
| 66 | + * @return self |
| 67 | + */ |
| 68 | + public function setExpiration($expiration) |
| 69 | + { |
| 70 | + $this->expiration = $expiration; |
| 71 | + return $this; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + /** |
| 76 | + * @inheritdoc |
| 77 | + */ |
| 78 | + public function get($key) |
| 79 | + { |
| 80 | + return $this->storage->read($this->generateKey($key)); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + /** |
| 85 | + * @inheritdoc |
| 86 | + */ |
| 87 | + public function set($key, $data) |
| 88 | + { |
| 89 | + $key = $this->generateKey($key); |
| 90 | + |
| 91 | + if ($data === NULL) { |
| 92 | + $this->storage->remove($key); |
| 93 | + } else { |
| 94 | + $this->storage->write($key, $data, $this->getDependencies()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + /** |
| 100 | + * @return array |
| 101 | + */ |
| 102 | + private function getDependencies() |
| 103 | + { |
| 104 | + $dependencies = array(); |
| 105 | + |
| 106 | + if (isset($this->expiration)) { |
| 107 | + $dependencies[Cache::EXPIRATION] = Nette\Utils\DateTime::from($this->expiration)->format('U') - time(); |
| 108 | + } |
| 109 | + |
| 110 | + return $dependencies; |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + /** |
| 115 | + * Generates internal cache key. |
| 116 | + * |
| 117 | + * @param string $key |
| 118 | + * @return string |
| 119 | + */ |
| 120 | + protected function generateKey($key) |
| 121 | + { |
| 122 | + return $this->namespace . md5(is_scalar($key) ? $key : serialize($key)); |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + /** |
| 127 | + * Clears all cached items. |
| 128 | + */ |
| 129 | + public function clean() |
| 130 | + { |
| 131 | + $this->storage->clean(array(Cache::ALL => TRUE)); |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments