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;
35 class MemcacheCache extends AbstractCache implements ICanCacheInMemory
38 use CompareDeleteTrait;
39 use MemcacheCommandTrait;
47 * @param string $hostname
48 * @param IManageConfigValues $config
50 * @throws InvalidCacheDriverException
51 * @throws CachePersistenceException
53 public function __construct(string $hostname, IManageConfigValues $config)
55 if (!class_exists('Memcache', false)) {
56 throw new InvalidCacheDriverException('Memcache class isn\'t available');
59 parent::__construct($hostname);
61 $this->memcache = new Memcache();
63 $this->server = $config->get('system', 'memcache_host');
64 $this->port = $config->get('system', 'memcache_port');
66 if (!@$this->memcache->connect($this->server, $this->port)) {
67 throw new CachePersistenceException('Expected Memcache server at ' . $this->server . ':' . $this->port . ' isn\'t available');
72 * Memcache doesn't allow spaces in keys
77 protected function getCacheKey(string $key): string
79 return str_replace(' ', '_', parent::getCacheKey($key));
85 public function getAllKeys(?string $prefix = null): array
87 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
89 return $this->filterArrayKeysByPrefix($keys, $prefix);
95 public function get(string $key)
97 $cacheKey = $this->getCacheKey($key);
99 // We fetch with the hostname as key to avoid problems with other applications
100 $cached = $this->memcache->get($cacheKey);
102 // @see http://php.net/manual/en/memcache.get.php#84275
103 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
107 $value = @unserialize($cached);
109 // Only return a value if the serialized value is valid.
110 // We also check if the db entry is a serialized
111 // boolean 'false' value (which we want to return).
112 if ($cached === serialize(false) || $value !== false) {
122 public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
124 $cacheKey = $this->getCacheKey($key);
126 // We store with the hostname as key to avoid problems with other applications
128 return $this->memcache->set(
135 return $this->memcache->set(
146 public function delete(string $key): bool
148 $cacheKey = $this->getCacheKey($key);
149 return $this->memcache->delete($cacheKey);
155 public function clear(bool $outdated = true): bool
160 return $this->memcache->flush();
167 public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
169 $cacheKey = $this->getCacheKey($key);
170 return $this->memcache->add($cacheKey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
176 public function getName(): string
178 return Type::MEMCACHE;