]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/Type/MemcachedCache.php
Merge pull request #11684 from MrPetovan/bug/11651-ap-fetch-queue
[friendica.git] / src / Core / Cache / Type / MemcachedCache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core\Cache\Type;
23
24 use Friendica\Core\Cache\Enum\Duration;
25 use Friendica\Core\Cache\Capability\ICanCacheInMemory;
26 use Friendica\Core\Cache\Enum\Type;
27 use Friendica\Core\Cache\Exception\CachePersistenceException;
28 use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
29 use Friendica\Core\Config\Capability\IManageConfigValues;
30 use Memcached;
31 use Psr\Log\LoggerInterface;
32
33 /**
34  * Memcached Cache
35  */
36 class MemcachedCache extends AbstractCache implements ICanCacheInMemory
37 {
38         use CompareSetTrait;
39         use CompareDeleteTrait;
40         use MemcacheCommandTrait;
41
42         /**
43          * @var \Memcached
44          */
45         private $memcached;
46
47         /**
48          * @var LoggerInterface
49          */
50         private $logger;
51
52         /**
53          * Due to limitations of the INI format, the expected configuration for Memcached servers is the following:
54          * array {
55          *   0 => "hostname, port(, weight)",
56          *   1 => ...
57          * }
58          *
59          * @param string              $hostname
60          * @param IManageConfigValues $config
61          * @param LoggerInterface     $logger
62          *
63          * @throws InvalidCacheDriverException
64          * @throws CachePersistenceException
65          */
66         public function __construct(string $hostname, IManageConfigValues $config, LoggerInterface $logger)
67         {
68                 if (!class_exists('Memcached', false)) {
69                         throw new InvalidCacheDriverException('Memcached class isn\'t available');
70                 }
71
72                 parent::__construct($hostname);
73
74                 $this->logger = $logger;
75
76                 $this->memcached = new Memcached();
77
78                 $memcached_hosts = $config->get('system', 'memcached_hosts');
79
80                 array_walk($memcached_hosts, function (&$value) {
81                         if (is_string($value)) {
82                                 $value = array_map('trim', explode(',', $value));
83                         }
84                 });
85
86                 $this->server = $memcached_hosts[0][0] ?? 'localhost';
87                 $this->port   = $memcached_hosts[0][1] ?? 11211;
88
89                 $this->memcached->addServers($memcached_hosts);
90
91                 if (count($this->memcached->getServerList()) == 0) {
92                         throw new CachePersistenceException('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
93                 }
94         }
95
96         /**
97          * Memcached doesn't allow spaces in keys
98          *
99          * @param string $key
100          * @return string
101          */
102         protected function getCacheKey(string $key): string
103         {
104                 return str_replace(' ', '_', parent::getCacheKey($key));
105         }
106
107         /**
108          * (@inheritdoc)
109          */
110         public function getAllKeys(?string $prefix = null): array
111         {
112                 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
113
114                 return $this->filterArrayKeysByPrefix($keys, $prefix);
115         }
116
117         /**
118          * (@inheritdoc)
119          */
120         public function get(string $key)
121         {
122                 $cacheKey = $this->getCacheKey($key);
123
124                 // We fetch with the hostname as key to avoid problems with other applications
125                 $value = $this->memcached->get($cacheKey);
126
127                 if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
128                         return $value;
129                 } elseif ($this->memcached->getResultCode() === Memcached::RES_NOTFOUND) {
130                         $this->logger->notice('Try to use unknown key.', ['key' => $key]);
131                         return null;
132                 } else {
133                         throw new CachePersistenceException(sprintf('Cannot get cache entry with key %s', $key), new \MemcachedException($this->memcached->getResultMessage(), $this->memcached->getResultCode()));
134                 }
135         }
136
137         /**
138          * (@inheritdoc)
139          */
140         public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
141         {
142                 $cacheKey = $this->getCacheKey($key);
143
144                 // We store with the hostname as key to avoid problems with other applications
145                 if ($ttl > 0) {
146                         return $this->memcached->set(
147                                 $cacheKey,
148                                 $value,
149                                 $ttl
150                         );
151                 } else {
152                         return $this->memcached->set(
153                                 $cacheKey,
154                                 $value
155                         );
156                 }
157         }
158
159         /**
160          * (@inheritdoc)
161          */
162         public function delete(string $key): bool
163         {
164                 $cacheKey = $this->getCacheKey($key);
165                 return $this->memcached->delete($cacheKey);
166         }
167
168         /**
169          * (@inheritdoc)
170          */
171         public function clear(bool $outdated = true): bool
172         {
173                 if ($outdated) {
174                         return true;
175                 } else {
176                         return $this->memcached->flush();
177                 }
178         }
179
180         /**
181          * (@inheritdoc)
182          */
183         public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
184         {
185                 $cacheKey = $this->getCacheKey($key);
186                 return $this->memcached->add($cacheKey, $value, $ttl);
187         }
188
189         /**
190          * {@inheritDoc}
191          */
192         public function getName(): string
193         {
194                 return Type::MEMCACHED;
195         }
196 }