1 | <?php |
||
14 | class RedisCache implements CacheInterface |
||
15 | { |
||
16 | const SERIALIZER_PHP = 0; |
||
17 | const SERIALIZER_IGBINARY = 1; |
||
18 | |||
19 | /** |
||
20 | * The Redis instance. |
||
21 | * Assumes that the connection has been established and the proper database has been selected. |
||
22 | * |
||
23 | * @var Redis |
||
24 | */ |
||
25 | private $redis; |
||
26 | |||
27 | /** |
||
28 | * The TTL of the cache, in seconds. |
||
29 | * |
||
30 | * @var int |
||
31 | */ |
||
32 | private $ttl; |
||
33 | |||
34 | /** |
||
35 | * The serializer to use. |
||
36 | * |
||
37 | * @var int |
||
38 | */ |
||
39 | private $serializer; |
||
40 | |||
41 | /** |
||
42 | * The cache key prefix to use. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | private $prefix = 'ModlrData'; |
||
47 | |||
48 | /** |
||
49 | * Constructor. |
||
50 | * |
||
51 | * @param Redis $redis |
||
52 | * @param int $ttl |
||
53 | * @param int $serializer |
||
54 | */ |
||
55 | public function __construct(Redis $redis, $ttl = 3600, $serializer = self::SERIALIZER_PHP) |
||
61 | |||
62 | /** |
||
63 | * Sets a custom cache key prefix. |
||
64 | * |
||
65 | * @param string $prefix |
||
66 | * @return self |
||
67 | */ |
||
68 | public function setPrefix($prefix) |
||
73 | |||
74 | /** |
||
75 | * Gets the cache key based on entity type. |
||
76 | * |
||
77 | * @param string $type |
||
78 | * @return string |
||
79 | */ |
||
80 | protected function getKey($type) |
||
84 | |||
85 | /** |
||
86 | * {@inheritDoc} |
||
87 | */ |
||
88 | public function loadMetadataFromCache($type) |
||
97 | |||
98 | /** |
||
99 | * {@inheritDoc} |
||
100 | */ |
||
101 | public function putMetadataInCache(EntityMetadata $metadata) |
||
107 | |||
108 | /** |
||
109 | * Serializes the metadata object based on the selected serializer. |
||
110 | * |
||
111 | * @param EntityMetadata $metadata |
||
112 | * @return string |
||
113 | * @throws RuntimeException |
||
114 | */ |
||
115 | private function serialize(EntityMetadata $metadata) |
||
126 | |||
127 | /** |
||
128 | * Unserializes the metadata object based on the selected serializer. |
||
129 | * |
||
130 | * @param string $value |
||
131 | * @return EntityMetadata |
||
132 | * @throws RuntimeException |
||
133 | */ |
||
134 | private function unserialize($value) |
||
145 | |||
146 | /** |
||
147 | * {@inheritDoc} |
||
148 | */ |
||
149 | public function evictMetadataFromCache(EntityMetadata $metadata) |
||
153 | } |
||
154 |