]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/MemcachedCacheDriver.php
Logger Levels
[friendica.git] / src / Core / Cache / MemcachedCacheDriver.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5 use Friendica\Core\Cache;
6 use Friendica\Core\Logger;
7
8 use Exception;
9 use Friendica\Network\HTTPException\InternalServerErrorException;
10 use Memcached;
11
12 /**
13  * Memcached Cache Driver
14  *
15  * @author Hypolite Petovan <hypolite@mrpetovan.com>
16  */
17 class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
18 {
19         use TraitCompareSet;
20         use TraitCompareDelete;
21
22         /**
23          * @var \Memcached
24          */
25         private $memcached;
26
27         /**
28          * Due to limitations of the INI format, the expected configuration for Memcached servers is the following:
29          * array {
30          *   0 => "hostname, port(, weight)",
31          *   1 => ...
32          * }
33          *
34          * @param array $memcached_hosts
35          * @throws \Exception
36          */
37         public function __construct(array $memcached_hosts)
38         {
39                 if (!class_exists('Memcached', false)) {
40                         throw new Exception('Memcached class isn\'t available');
41                 }
42
43                 $this->memcached = new Memcached();
44
45                 array_walk($memcached_hosts, function (&$value) {
46                         if (is_string($value)) {
47                                 $value = array_map('trim', explode(',', $value));
48                         }
49                 });
50
51                 $this->memcached->addServers($memcached_hosts);
52
53                 if (count($this->memcached->getServerList()) == 0) {
54                         throw new Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
55                 }
56         }
57
58         /**
59          * (@inheritdoc)
60          */
61         public function getAllKeys($prefix = null)
62         {
63                 $keys = $this->getOriginalKeys($this->memcached->getAllKeys());
64
65                 if ($this->memcached->getResultCode() == Memcached::RES_SUCCESS) {
66                         return $this->filterArrayKeysByPrefix($keys, $prefix);
67                 } else {
68                         Logger::log('Memcached \'getAllKeys\' failed with ' . $this->memcached->getResultMessage(), Logger::ALL);
69                         return [];
70                 }
71         }
72
73         /**
74          * (@inheritdoc)
75          */
76         public function get($key)
77         {
78                 $return = null;
79                 $cachekey = $this->getCacheKey($key);
80
81                 // We fetch with the hostname as key to avoid problems with other applications
82                 $value = $this->memcached->get($cachekey);
83
84                 if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
85                         $return = $value;
86                 } else {
87                         Logger::log('Memcached \'get\' failed with ' . $this->memcached->getResultMessage(), Logger::ALL);
88                 }
89
90                 return $return;
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 }