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;
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');
74 public function getAllKeys(?string $prefix = null): array
76 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
78 return $this->filterArrayKeysByPrefix($keys, $prefix);
84 public function get(string $key)
86 $cacheKey = $this->getCacheKey($key);
88 // We fetch with the hostname as key to avoid problems with other applications
89 $cached = $this->memcache->get($cacheKey);
91 // @see http://php.net/manual/en/memcache.get.php#84275
92 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
96 $value = @unserialize($cached);
98 // Only return a value if the serialized value is valid.
99 // We also check if the db entry is a serialized
100 // boolean 'false' value (which we want to return).
101 if ($cached === serialize(false) || $value !== false) {
111 public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
113 $cacheKey = $this->getCacheKey($key);
115 // We store with the hostname as key to avoid problems with other applications
117 return $this->memcache->set(
124 return $this->memcache->set(
135 public function delete(string $key): bool
137 $cacheKey = $this->getCacheKey($key);
138 return $this->memcache->delete($cacheKey);
144 public function clear(bool $outdated = true): bool
149 return $this->memcache->flush();
156 public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
158 $cacheKey = $this->getCacheKey($key);
159 return $this->memcache->add($cacheKey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
165 public function getName(): string
167 return Type::MEMCACHE;