]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/Type/MemcachedCache.php
Merge pull request #11250 from nupplaphil/bug/redis_pw
[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          * (@inheritdoc)
98          */
99         public function getAllKeys(?string $prefix = null): array
100         {
101                 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
102
103                 return $this->filterArrayKeysByPrefix($keys, $prefix);
104         }
105
106         /**
107          * (@inheritdoc)
108          */
109         public function get(string $key)
110         {
111                 $cacheKey = $this->getCacheKey($key);
112
113                 // We fetch with the hostname as key to avoid problems with other applications
114                 $value = $this->memcached->get($cacheKey);
115
116                 if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
117                         return $value;
118                 } elseif ($this->memcached->getResultCode() === Memcached::RES_NOTFOUND) {
119                         $this->logger->notice('Try to use unknown key.', ['key' => $key]);
120                         return null;
121                 } else {
122                         throw new CachePersistenceException(sprintf('Cannot get cache entry with key %s', $key), new \MemcachedException($this->memcached->getResultMessage(), $this->memcached->getResultCode()));
123                 }
124         }
125
126         /**
127          * (@inheritdoc)
128          */
129         public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
130         {
131                 $cacheKey = $this->getCacheKey($key);
132
133                 // We store with the hostname as key to avoid problems with other applications
134                 if ($ttl > 0) {
135                         return $this->memcached->set(
136                                 $cacheKey,
137                                 $value,
138                                 $ttl
139                         );
140                 } else {
141                         return $this->memcached->set(
142                                 $cacheKey,
143                                 $value
144                         );
145                 }
146         }
147
148         /**
149          * (@inheritdoc)
150          */
151         public function delete(string $key): bool
152         {
153                 $cacheKey = $this->getCacheKey($key);
154                 return $this->memcached->delete($cacheKey);
155         }
156
157         /**
158          * (@inheritdoc)
159          */
160         public function clear(bool $outdated = true): bool
161         {
162                 if ($outdated) {
163                         return true;
164                 } else {
165                         return $this->memcached->flush();
166                 }
167         }
168
169         /**
170          * (@inheritdoc)
171          */
172         public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
173         {
174                 $cacheKey = $this->getCacheKey($key);
175                 return $this->memcached->add($cacheKey, $value, $ttl);
176         }
177
178         /**
179          * {@inheritDoc}
180          */
181         public function getName(): string
182         {
183                 return Type::MEMCACHED;
184         }
185 }