]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/MemcachedCache.php
CleanUp Cache namespace
[friendica.git] / src / Core / Cache / MemcachedCache.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5 use Exception;
6 use Friendica\Core\BaseCache;
7 use Friendica\Core\Config\IConfiguration;
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 BaseCache implements IMemoryCache
17 {
18         use TraitCompareSet;
19         use TraitCompareDelete;
20         use TraitMemcacheCommand;
21
22         /**
23          * @var \Memcached
24          */
25         private $memcached;
26
27         /**
28          * @var LoggerInterface
29          */
30         private $logger;
31
32         /**
33          * Due to limitations of the INI format, the expected configuration for Memcached servers is the following:
34          * array {
35          *   0 => "hostname, port(, weight)",
36          *   1 => ...
37          * }
38          *
39          * @param array $memcached_hosts
40          *
41          * @throws \Exception
42          */
43         public function __construct(string $hostname, IConfiguration $config, LoggerInterface $logger)
44         {
45                 if (!class_exists('Memcached', false)) {
46                         throw new Exception('Memcached class isn\'t available');
47                 }
48
49                 parent::__construct($hostname);
50
51                 $this->logger = $logger;
52
53                 $this->memcached = new Memcached();
54
55                 $memcached_hosts = $config->get('system', 'memcached_hosts');
56
57                 array_walk($memcached_hosts, function (&$value) {
58                         if (is_string($value)) {
59                                 $value = array_map('trim', explode(',', $value));
60                         }
61                 });
62
63                 $this->server = $memcached_hosts[0][0] ?? 'localhost';
64                 $this->port = $memcached_hosts[0][1] ?? 11211;
65
66                 $this->memcached->addServers($memcached_hosts);
67
68                 if (count($this->memcached->getServerList()) == 0) {
69                         throw new Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
70                 }
71         }
72
73         /**
74          * (@inheritdoc)
75          */
76         public function getAllKeys($prefix = null)
77         {
78                 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
79
80                 return $this->filterArrayKeysByPrefix($keys, $prefix);
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 = Duration::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 = Duration::FIVE_MINUTES)
150         {
151                 $cachekey = $this->getCacheKey($key);
152                 return $this->memcached->add($cachekey, $value, $ttl);
153         }
154
155         /**
156          * {@inheritDoc}
157          */
158         public function getName()
159         {
160                 return Type::MEMCACHED;
161         }
162 }