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