3 * @copyright Copyright (C) 2020, Friendica
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;
25 use Friendica\Core\BaseCache;
26 use Friendica\Core\Config\IConfig;
28 use Psr\Log\LoggerInterface;
33 class MemcachedCache extends BaseCache implements IMemoryCache
36 use TraitCompareDelete;
37 use TraitMemcacheCommand;
45 * @var LoggerInterface
50 * Due to limitations of the INI format, the expected configuration for Memcached servers is the following:
52 * 0 => "hostname, port(, weight)",
56 * @param array $memcached_hosts
60 public function __construct(string $hostname, IConfig $config, LoggerInterface $logger)
62 if (!class_exists('Memcached', false)) {
63 throw new Exception('Memcached class isn\'t available');
66 parent::__construct($hostname);
68 $this->logger = $logger;
70 $this->memcached = new Memcached();
72 $memcached_hosts = $config->get('system', 'memcached_hosts');
74 array_walk($memcached_hosts, function (&$value) {
75 if (is_string($value)) {
76 $value = array_map('trim', explode(',', $value));
80 $this->server = $memcached_hosts[0][0] ?? 'localhost';
81 $this->port = $memcached_hosts[0][1] ?? 11211;
83 $this->memcached->addServers($memcached_hosts);
85 if (count($this->memcached->getServerList()) == 0) {
86 throw new Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
93 public function getAllKeys($prefix = null)
95 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
97 return $this->filterArrayKeysByPrefix($keys, $prefix);
103 public function get($key)
106 $cachekey = $this->getCacheKey($key);
108 // We fetch with the hostname as key to avoid problems with other applications
109 $value = $this->memcached->get($cachekey);
111 if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
114 $this->logger->debug('Memcached \'get\' failed', ['result' => $this->memcached->getResultMessage()]);
123 public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
125 $cachekey = $this->getCacheKey($key);
127 // We store with the hostname as key to avoid problems with other applications
129 return $this->memcached->set(
135 return $this->memcached->set(
145 public function delete($key)
147 $cachekey = $this->getCacheKey($key);
148 return $this->memcached->delete($cachekey);
154 public function clear($outdated = true)
159 return $this->memcached->flush();
166 public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
168 $cachekey = $this->getCacheKey($key);
169 return $this->memcached->add($cachekey, $value, $ttl);
175 public function getName()
177 return Type::MEMCACHED;