]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/MemcachedCacheDriver.php
a6e1bad950e343c3a0aef88ec37db65162f3ff25
[friendica.git] / src / Core / Cache / MemcachedCacheDriver.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5 use Friendica\Core\Cache;
6
7 use Exception;
8 use Friendica\Network\HTTPException\InternalServerErrorException;
9 use Memcached;
10
11 /**
12  * Memcached Cache Driver
13  *
14  * @author Hypolite Petovan <hypolite@mrpetovan.com>
15  */
16 class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
17 {
18         use TraitCompareSet;
19         use TraitCompareDelete;
20
21         /**
22          * @var \Memcached
23          */
24         private $memcached;
25
26         /**
27          * Due to limitations of the INI format, the expected configuration for Memcached servers is the following:
28          * array {
29          *   0 => "hostname, port(, weight)",
30          *   1 => ...
31          * }
32          *
33          * @param array $memcached_hosts
34          * @throws \Exception
35          */
36         public function __construct(array $memcached_hosts)
37         {
38                 if (!class_exists('Memcached', false)) {
39                         throw new Exception('Memcached class isn\'t available');
40                 }
41
42                 $this->memcached = new Memcached();
43
44
45                 $this->memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, false);
46
47                 array_walk($memcached_hosts, function (&$value) {
48                         if (is_string($value)) {
49                                 $value = array_map('trim', explode(',', $value));
50                         }
51                 });
52
53                 $this->memcached->addServers($memcached_hosts);
54
55                 if (count($this->memcached->getServerList()) == 0) {
56                         throw new Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
57                 }
58         }
59
60         /**
61          * (@inheritdoc)
62          */
63         public function getAllKeys($prefix = null)
64         {
65                 $keys = $this->getOriginalKeys($this->memcached->getAllKeys());
66
67                 if ($this->memcached->getResultCode() == Memcached::RES_SUCCESS) {
68                         return $this->filterArrayKeysByPrefix($keys, $prefix);
69                 } else {
70                         logger('Memcached \'getAllKeys\' failed with ' . $this->memcached->getResultMessage(), LOGGER_ALL);
71                         return [];
72                 }
73         }
74
75         /**
76          * (@inheritdoc)
77          */
78         public function get($key)
79         {
80                 $cachekey = $this->getCacheKey($key);
81
82                 // We fetch with the hostname as key to avoid problems with other applications
83                 $value = $this->memcached->get($cachekey);
84
85                 if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
86                         return $value;
87                 } else {
88                         logger('Memcached \'get\' failed with ' . $this->memcached->getResultMessage(), LOGGER_ALL);
89                         return [];
90                 }
91         }
92
93         /**
94          * (@inheritdoc)
95          */
96         public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
97         {
98                 $cachekey = $this->getCacheKey($key);
99
100                 // We store with the hostname as key to avoid problems with other applications
101                 if ($ttl > 0) {
102                         return $this->memcached->set(
103                                 $cachekey,
104                                 $value,
105                                 $ttl
106                         );
107                 } else {
108                         return $this->memcached->set(
109                                 $cachekey,
110                                 $value
111                         );
112                 }
113         }
114
115         /**
116          * (@inheritdoc)
117          */
118         public function delete($key)
119         {
120                 $cachekey = $this->getCacheKey($key);
121                 return $this->memcached->delete($cachekey);
122         }
123
124         /**
125          * (@inheritdoc)
126          */
127         public function clear($outdated = true)
128         {
129                 if ($outdated) {
130                         return true;
131                 } else {
132                         return $this->memcached->flush();
133                 }
134         }
135
136         /**
137          * (@inheritdoc)
138          */
139         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
140         {
141                 $cachekey = $this->getCacheKey($key);
142                 return $this->memcached->add($cachekey, $value, $ttl);
143         }
144 }