|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sleepness\UberTranslationBundle\Storage; |
|
4
|
|
|
|
|
5
|
|
|
use \Memcached; |
|
6
|
|
|
use Symfony\Component\Config\Resource\ResourceInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Wrapper under standard Memcached class, |
|
10
|
|
|
* which ease work with memcached |
|
11
|
|
|
* |
|
12
|
|
|
* @author Viktor Novikov <[email protected]> |
|
13
|
|
|
* @author Alexandr Zhulev <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class UberMemcached implements ResourceInterface, UberStorageInterface |
|
16
|
|
|
{ |
|
17
|
|
|
private $memcached; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(\Memcached $memcached) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->memcached = $memcached; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __toString() |
|
28
|
|
|
{ |
|
29
|
|
|
return 'uberMemcached'; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return Memcached |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getMemcached() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->memcached; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setConnection($host, $port) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->getMemcached()->addServer($host, $port); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function addItem($key, $value, $expiration = null) |
|
52
|
|
|
{ |
|
53
|
|
|
if ($expiration === null) { |
|
54
|
|
|
$expiration = 60 * 60 * 24 * 30; // default expires after 30 days |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this->getMemcached()->set($key, $value, $expiration); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getItem($key) |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->getMemcached()->get($key); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getAllKeys() |
|
72
|
|
|
{ |
|
73
|
|
|
$allKeys = $this->getMemcached()->getAllKeys(); |
|
74
|
|
|
$locales = array(); |
|
75
|
|
|
foreach ($allKeys as $key) { |
|
76
|
|
|
if (!preg_match('/^[a-z]{2}_[a-zA-Z]{2}$|[a-z]{2}/', $key)) { |
|
77
|
|
|
continue; |
|
78
|
|
|
} |
|
79
|
|
|
$locales[] = $key; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $locales; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
public function deleteItem($key) |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->getMemcached()->delete($key); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
|
|
public function dropCache() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->getMemcached()->flush(0); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
|
|
public function isFresh($timestamp) |
|
105
|
|
|
{ |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getResource() |
|
112
|
|
|
{ |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|