]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/MemcachedCacheDriver.php
Merge remote-tracking branch 'upstream/develop' into item-thread
[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                 array_walk($memcached_hosts, function (&$value) {
45                         if (is_string($value)) {
46                                 $value = array_map('trim', explode(',', $value));
47                         }
48                 });
49
50                 $this->memcached->addServers($memcached_hosts);
51
52                 if (count($this->memcached->getServerList()) == 0) {
53                         throw new Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
54                 }
55         }
56
57         /**
58          * (@inheritdoc)
59          */
60         public function getAllKeys($prefix = null)
61         {
62                 $keys = $this->getOriginalKeys($this->memcached->getAllKeys());
63
64                 if ($this->memcached->getResultCode() == Memcached::RES_SUCCESS) {
65                         return $this->filterArrayKeysByPrefix($keys, $prefix);
66                 } else {
67                         logger('Memcached \'getAllKeys\' failed with ' . $this->memcached->getResultMessage(), LOGGER_ALL);
68                         return [];
69                 }
70         }
71
72         /**
73          * (@inheritdoc)
74          */
75         public function get($key)
76         {
77                 $return = null;
78                 $cachekey = $this->getCacheKey($key);
79
80                 // We fetch with the hostname as key to avoid problems with other applications
81                 $value = $this->memcached->get($cachekey);
82
83                 if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
84                         $return = $value;
85                 } else {
86                         logger('Memcached \'get\' failed with ' . $this->memcached->getResultMessage(), LOGGER_ALL);
87                 }
88
89                 return $return;
90         }
91
92         /**
93          * (@inheritdoc)
94          */
95         public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
96         {
97                 $cachekey = $this->getCacheKey($key);
98
99                 // We store with the hostname as key to avoid problems with other applications
100                 if ($ttl > 0) {
101                         return $this->memcached->set(
102                                 $cachekey,
103                                 $value,
104                                 $ttl
105                         );
106                 } else {
107                         return $this->memcached->set(
108                                 $cachekey,
109                                 $value
110                         );
111                 }
112         }
113
114         /**
115          * (@inheritdoc)
116          */
117         public function delete($key)
118         {
119                 $cachekey = $this->getCacheKey($key);
120                 return $this->memcached->delete($cachekey);
121         }
122
123         /**
124          * (@inheritdoc)
125          */
126         public function clear($outdated = true)
127         {
128                 if ($outdated) {
129                         return true;
130                 } else {
131                         return $this->memcached->flush();
132                 }
133         }
134
135         /**
136          * (@inheritdoc)
137          */
138         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
139         {
140                 $cachekey = $this->getCacheKey($key);
141                 return $this->memcached->add($cachekey, $value, $ttl);
142         }
143 }