3 * @copyright Copyright (C) 2010-2022, 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\Enum\Type;
27 use Friendica\Core\Cache\Exception\CachePersistenceException;
28 use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
29 use Friendica\Core\Config\Capability\IManageConfigValues;
31 use Psr\Log\LoggerInterface;
36 class MemcachedCache extends AbstractCache implements ICanCacheInMemory
39 use CompareDeleteTrait;
40 use MemcacheCommandTrait;
48 * @var LoggerInterface
53 * Due to limitations of the INI format, the expected configuration for Memcached servers is the following:
55 * 0 => "hostname, port(, weight)",
59 * @param string $hostname
60 * @param IManageConfigValues $config
61 * @param LoggerInterface $logger
63 * @throws InvalidCacheDriverException
64 * @throws CachePersistenceException
66 public function __construct(string $hostname, IManageConfigValues $config, LoggerInterface $logger)
68 if (!class_exists('Memcached', false)) {
69 throw new InvalidCacheDriverException('Memcached class isn\'t available');
72 parent::__construct($hostname);
74 $this->logger = $logger;
76 $this->memcached = new Memcached();
78 $memcached_hosts = $config->get('system', 'memcached_hosts');
80 array_walk($memcached_hosts, function (&$value) {
81 if (is_string($value)) {
82 $value = array_map('trim', explode(',', $value));
86 $this->server = $memcached_hosts[0][0] ?? 'localhost';
87 $this->port = $memcached_hosts[0][1] ?? 11211;
89 $this->memcached->addServers($memcached_hosts);
91 if (count($this->memcached->getServerList()) == 0) {
92 throw new CachePersistenceException('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
97 * Memcached doesn't allow spaces in keys
102 protected function getCacheKey(string $key): string
104 return str_replace(' ', '_', parent::getCacheKey($key));
110 public function getAllKeys(?string $prefix = null): array
112 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
114 return $this->filterArrayKeysByPrefix($keys, $prefix);
120 public function get(string $key)
122 $cacheKey = $this->getCacheKey($key);
124 // We fetch with the hostname as key to avoid problems with other applications
125 $value = $this->memcached->get($cacheKey);
127 if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
129 } elseif ($this->memcached->getResultCode() === Memcached::RES_NOTFOUND) {
130 $this->logger->notice('Try to use unknown key.', ['key' => $key]);
133 throw new CachePersistenceException(sprintf('Cannot get cache entry with key %s', $key), new \MemcachedException($this->memcached->getResultMessage(), $this->memcached->getResultCode()));
140 public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
142 $cacheKey = $this->getCacheKey($key);
144 // We store with the hostname as key to avoid problems with other applications
146 return $this->memcached->set(
152 return $this->memcached->set(
162 public function delete(string $key): bool
164 $cacheKey = $this->getCacheKey($key);
165 return $this->memcached->delete($cacheKey);
171 public function clear(bool $outdated = true): bool
176 return $this->memcached->flush();
183 public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
185 $cacheKey = $this->getCacheKey($key);
186 return $this->memcached->add($cacheKey, $value, $ttl);
192 public function getName(): string
194 return Type::MEMCACHED;