|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sleepness\UberTranslationBundle\Storage; |
|
4
|
|
|
|
|
5
|
|
|
use \Redis; |
|
6
|
|
|
use Symfony\Component\Config\Resource\ResourceInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Wrapper under standard Redis class, |
|
10
|
|
|
* which ease work with Redis (using phpredis client - https://github.com/phpredis/phpredis) |
|
11
|
|
|
* |
|
12
|
|
|
* @author Viktor Novikov <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class UberRedis implements ResourceInterface, UberStorageInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var \Redis |
|
18
|
|
|
*/ |
|
19
|
|
|
private $redis; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(\Redis $redis) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->redis = $redis; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function setConnection($host, $port) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->getRedis()->connect($host, $port); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return Redis |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getRedis() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->redis; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function addItem($key, $value, $expiration = null) |
|
46
|
|
|
{ |
|
47
|
|
|
if ($expiration === null) { |
|
48
|
|
|
$expiration = 60 * 60 * 24 * 30; // default expires after 30 days |
|
49
|
|
|
} |
|
50
|
|
|
$encoded_value = json_encode($value); |
|
51
|
|
|
|
|
52
|
|
|
return $this->getRedis()->set($key, $encoded_value, $expiration); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getItem($key) |
|
59
|
|
|
{ |
|
60
|
|
|
$response = json_decode($this->getRedis()->get($key)); |
|
61
|
|
|
|
|
62
|
|
|
return $response; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getAllKeys() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->getRedis()->keys('*'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function deleteItem($key) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->getRedis()->delete($key); |
|
79
|
|
|
|
|
80
|
|
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function dropCache() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->getRedis()->flushAll(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function __toString() |
|
95
|
|
|
{ |
|
96
|
|
|
return 'uberRedis'; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function isFresh($timestamp) |
|
103
|
|
|
{ |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getResource() |
|
110
|
|
|
{ |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
|
|
|
|
|
113
|
|
|
|