3 * @copyright Copyright (C) 2010-2023, 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;
24 use Friendica\Core\Cache\Enum\Duration;
25 use Friendica\Core\Cache\Capability\ICanCacheInMemory;
26 use Friendica\Core\Cache\Exception\CachePersistenceException;
27 use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
28 use Friendica\Core\Config\Capability\IManageConfigValues;
30 use Psr\Log\LoggerInterface;
35 class MemcachedCache extends AbstractCache implements ICanCacheInMemory
37 static $NAME = 'memcached';
40 use CompareDeleteTrait;
41 use MemcacheCommandTrait;
49 * @var LoggerInterface
54 * Due to limitations of the INI format, the expected configuration for Memcached servers is the following:
56 * 0 => "hostname, port(, weight)",
60 * @param string $hostname
61 * @param IManageConfigValues $config
62 * @param LoggerInterface $logger
64 * @throws InvalidCacheDriverException
65 * @throws CachePersistenceException
67 public function __construct(string $hostname, IManageConfigValues $config, LoggerInterface $logger)
69 if (!class_exists('Memcached', false)) {
70 throw new InvalidCacheDriverException('Memcached class isn\'t available');
73 parent::__construct($hostname);
75 $this->logger = $logger;
77 $this->memcached = new Memcached();
79 $memcached_hosts = $config->get('system', 'memcached_hosts');
81 array_walk($memcached_hosts, function (&$value) {
82 if (is_string($value)) {
83 $value = array_map('trim', explode(',', $value));
87 $this->server = $memcached_hosts[0][0] ?? 'localhost';
88 $this->port = $memcached_hosts[0][1] ?? 11211;
90 $this->memcached->addServers($memcached_hosts);
92 if (count($this->memcached->getServerList()) == 0) {
93 throw new CachePersistenceException('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
98 * Memcached doesn't allow spaces in keys
103 protected function getCacheKey(string $key): string
105 return str_replace(' ', '_', parent::getCacheKey($key));
111 public function getAllKeys(?string $prefix = null): array
113 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
115 return $this->filterArrayKeysByPrefix($keys, $prefix);
121 public function get(string $key)
123 $cacheKey = $this->getCacheKey($key);
125 // We fetch with the hostname as key to avoid problems with other applications
126 $value = $this->memcached->get($cacheKey);
128 if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
130 } elseif ($this->memcached->getResultCode() === Memcached::RES_NOTFOUND) {
131 $this->logger->debug('Try to use unknown key.', ['key' => $key]);
134 throw new CachePersistenceException(sprintf('Cannot get cache entry with key %s', $key), new \MemcachedException($this->memcached->getResultMessage(), $this->memcached->getResultCode()));
141 public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
143 $cacheKey = $this->getCacheKey($key);
145 // We store with the hostname as key to avoid problems with other applications
147 return $this->memcached->set(
153 return $this->memcached->set(
163 public function delete(string $key): bool
165 $cacheKey = $this->getCacheKey($key);
166 return $this->memcached->delete($cacheKey);
172 public function clear(bool $outdated = true): bool
177 return $this->memcached->flush();
184 public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
186 $cacheKey = $this->getCacheKey($key);
187 return $this->memcached->add($cacheKey, $value, $ttl);