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