1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace As3\Modlr\Metadata\Cache; |
4
|
|
|
|
5
|
|
|
use Redis; |
6
|
|
|
use As3\Modlr\Metadata\EntityMetadata; |
7
|
|
|
use As3\Modlr\Exception\RuntimeException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Caches and retrieves EntityMetadata objects from Redis. |
11
|
|
|
* |
12
|
|
|
* @author Jacob Bare <[email protected]> |
13
|
|
|
*/ |
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) |
56
|
|
|
{ |
57
|
|
|
$this->redis = $redis; |
58
|
|
|
$this->ttl = (Integer) $ttl; |
59
|
|
|
$this->serializer = $serializer; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Sets a custom cache key prefix. |
64
|
|
|
* |
65
|
|
|
* @param string $prefix |
66
|
|
|
* @return self |
67
|
|
|
*/ |
68
|
|
|
public function setPrefix($prefix) |
69
|
|
|
{ |
70
|
|
|
$this->prefix = $prefix; |
71
|
|
|
return $this; |
72
|
|
|
} |
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) |
81
|
|
|
{ |
82
|
|
|
return sprintf('%s::%s', $this->prefix, $type); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritDoc} |
87
|
|
|
*/ |
88
|
|
|
public function loadMetadataFromCache($type) |
89
|
|
|
{ |
90
|
|
|
$result = $this->redis->get($this->getKey($type)); |
91
|
|
|
if (!$result) { |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
|
|
$metadata = $this->unserialize($result); |
95
|
|
|
return $metadata; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
|
|
public function putMetadataInCache(EntityMetadata $metadata) |
102
|
|
|
{ |
103
|
|
|
$value = $this->serialize($metadata); |
104
|
|
|
$this->redis->setex($this->getKey($metadata->type), $this->ttl, $value); |
105
|
|
|
return $this; |
106
|
|
|
} |
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) |
116
|
|
|
{ |
117
|
|
|
switch ($this->serializer) { |
118
|
|
|
case self::SERIALIZER_PHP: |
119
|
|
|
return serialize($metadata); |
120
|
|
|
case self::SERIALIZER_IGBINARY: |
121
|
|
|
return igbinary_serialize($metadata); |
122
|
|
|
default: |
123
|
|
|
throw new RuntimeException('Unable to handle serialization of the metadata object'); |
124
|
|
|
} |
125
|
|
|
} |
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) |
135
|
|
|
{ |
136
|
|
|
switch ($this->serializer) { |
137
|
|
|
case self::SERIALIZER_PHP: |
138
|
|
|
return unserialize($value); |
139
|
|
|
case self::SERIALIZER_IGBINARY: |
140
|
|
|
return igbinary_unserialize($value); |
141
|
|
|
default: |
142
|
|
|
throw new RuntimeException('Unable to handle unserialization of the metadata object'); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritDoc} |
148
|
|
|
*/ |
149
|
|
|
public function evictMetadataFromCache(EntityMetadata $metadata) |
150
|
|
|
{ |
151
|
|
|
$this->redis->delete($this->getKey($metadata->type)); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|