3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Core\Cache\Type;
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;
33 use Psr\Log\LoggerInterface;
38 class MemcachedCache extends BaseCache implements IMemoryCache
41 use TraitCompareDelete;
42 use TraitMemcacheCommand;
50 * @var LoggerInterface
55 * Due to limitations of the INI format, the expected configuration for Memcached servers is the following:
57 * 0 => "hostname, port(, weight)",
61 * @param array $memcached_hosts
65 public function __construct(string $hostname, IConfig $config, LoggerInterface $logger)
67 if (!class_exists('Memcached', false)) {
68 throw new Exception('Memcached class isn\'t available');
71 parent::__construct($hostname);
73 $this->logger = $logger;
75 $this->memcached = new Memcached();
77 $memcached_hosts = $config->get('system', 'memcached_hosts');
79 array_walk($memcached_hosts, function (&$value) {
80 if (is_string($value)) {
81 $value = array_map('trim', explode(',', $value));
85 $this->server = $memcached_hosts[0][0] ?? 'localhost';
86 $this->port = $memcached_hosts[0][1] ?? 11211;
88 $this->memcached->addServers($memcached_hosts);
90 if (count($this->memcached->getServerList()) == 0) {
91 throw new Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
98 public function getAllKeys($prefix = null)
100 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
102 return $this->filterArrayKeysByPrefix($keys, $prefix);
108 public function get($key)
111 $cachekey = $this->getCacheKey($key);
113 // We fetch with the hostname as key to avoid problems with other applications
114 $value = $this->memcached->get($cachekey);
116 if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
119 $this->logger->debug('Memcached \'get\' failed', ['result' => $this->memcached->getResultMessage()]);
128 public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
130 $cachekey = $this->getCacheKey($key);
132 // We store with the hostname as key to avoid problems with other applications
134 return $this->memcached->set(
140 return $this->memcached->set(
150 public function delete($key)
152 $cachekey = $this->getCacheKey($key);
153 return $this->memcached->delete($cachekey);
159 public function clear($outdated = true)
164 return $this->memcached->flush();
171 public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
173 $cachekey = $this->getCacheKey($key);
174 return $this->memcached->add($cachekey, $value, $ttl);
180 public function getName()
182 return Type::MEMCACHED;