]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/MemcachedCacheDriver.php
82df98f13bb5c6c584fbb348caa01516aab209ee
[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                 // Doesn't work because of https://github.com/php-memcached-dev/php-memcached/issues/367
66                 // returns everytime an empty array
67                 throw new InternalServerErrorException('getAllKeys for Memcached not supported yet');
68
69                 $list = $this->getOriginalKeys($this->memcached->getAllKeys());
70
71                 return $this->filterPrefix($list, $prefix);
72         }
73
74         /**
75          * (@inheritdoc)
76          */
77         public function get($key)
78         {
79                 $return = null;
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                 }
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         /**
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 }