]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/Type/MemcachedCache.php
Restructure Cache to follow new paradigm
[friendica.git] / src / Core / Cache / Type / MemcachedCache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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 Exception;
25 use Friendica\Core\Cache\Enum\Duration;
26 use Friendica\Core\Cache\IMemoryCache;
27 use Friendica\Core\Cache\Type\TraitCompareDelete;
28 use Friendica\Core\Cache\Type\TraitCompareSet;
29 use Friendica\Core\Cache\Type\TraitMemcacheCommand;
30 use Friendica\Core\Cache\Enum\Type;
31 use Friendica\Core\Config\IConfig;
32 use Memcached;
33 use Psr\Log\LoggerInterface;
34
35 /**
36  * Memcached Cache
37  */
38 class MemcachedCache extends BaseCache implements IMemoryCache
39 {
40         use TraitCompareSet;
41         use TraitCompareDelete;
42         use TraitMemcacheCommand;
43
44         /**
45          * @var \Memcached
46          */
47         private $memcached;
48
49         /**
50          * @var LoggerInterface
51          */
52         private $logger;
53
54         /**
55          * Due to limitations of the INI format, the expected configuration for Memcached servers is the following:
56          * array {
57          *   0 => "hostname, port(, weight)",
58          *   1 => ...
59          * }
60          *
61          * @param array $memcached_hosts
62          *
63          * @throws \Exception
64          */
65         public function __construct(string $hostname, IConfig $config, LoggerInterface $logger)
66         {
67                 if (!class_exists('Memcached', false)) {
68                         throw new Exception('Memcached class isn\'t available');
69                 }
70
71                 parent::__construct($hostname);
72
73                 $this->logger = $logger;
74
75                 $this->memcached = new Memcached();
76
77                 $memcached_hosts = $config->get('system', 'memcached_hosts');
78
79                 array_walk($memcached_hosts, function (&$value) {
80                         if (is_string($value)) {
81                                 $value = array_map('trim', explode(',', $value));
82                         }
83                 });
84
85                 $this->server = $memcached_hosts[0][0] ?? 'localhost';
86                 $this->port = $memcached_hosts[0][1] ?? 11211;
87
88                 $this->memcached->addServers($memcached_hosts);
89
90                 if (count($this->memcached->getServerList()) == 0) {
91                         throw new Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
92                 }
93         }
94
95         /**
96          * (@inheritdoc)
97          */
98         public function getAllKeys($prefix = null)
99         {
100                 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
101
102                 return $this->filterArrayKeysByPrefix($keys, $prefix);
103         }
104
105         /**
106          * (@inheritdoc)
107          */
108         public function get($key)
109         {
110                 $return   = null;
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                 } else {
119                         $this->logger->debug('Memcached \'get\' failed', ['result' => $this->memcached->getResultMessage()]);
120                 }
121
122                 return $return;
123         }
124
125         /**
126          * (@inheritdoc)
127          */
128         public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
129         {
130                 $cachekey = $this->getCacheKey($key);
131
132                 // We store with the hostname as key to avoid problems with other applications
133                 if ($ttl > 0) {
134                         return $this->memcached->set(
135                                 $cachekey,
136                                 $value,
137                                 $ttl
138                         );
139                 } else {
140                         return $this->memcached->set(
141                                 $cachekey,
142                                 $value
143                         );
144                 }
145         }
146
147         /**
148          * (@inheritdoc)
149          */
150         public function delete($key)
151         {
152                 $cachekey = $this->getCacheKey($key);
153                 return $this->memcached->delete($cachekey);
154         }
155
156         /**
157          * (@inheritdoc)
158          */
159         public function clear($outdated = true)
160         {
161                 if ($outdated) {
162                         return true;
163                 } else {
164                         return $this->memcached->flush();
165                 }
166         }
167
168         /**
169          * (@inheritdoc)
170          */
171         public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
172         {
173                 $cachekey = $this->getCacheKey($key);
174                 return $this->memcached->add($cachekey, $value, $ttl);
175         }
176
177         /**
178          * {@inheritDoc}
179          */
180         public function getName()
181         {
182                 return Type::MEMCACHED;
183         }
184 }