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;
32 class MemcacheCache extends BaseCache implements IMemoryCache
35 use TraitCompareDelete;
36 use TraitMemcacheCommand;
46 public function __construct(string $hostname, IConfig $config)
48 if (!class_exists('Memcache', false)) {
49 throw new Exception('Memcache class isn\'t available');
52 parent::__construct($hostname);
54 $this->memcache = new Memcache();
56 $this->server = $config->get('system', 'memcache_host');;
57 $this->port = $config->get('system', 'memcache_port');
59 if (!@$this->memcache->connect($this->server, $this->port)) {
60 throw new Exception('Expected Memcache server at ' . $this->server . ':' . $this->port . ' isn\'t available');
67 public function getAllKeys($prefix = null)
69 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
71 return $this->filterArrayKeysByPrefix($keys, $prefix);
77 public function get($key)
80 $cachekey = $this->getCacheKey($key);
82 // We fetch with the hostname as key to avoid problems with other applications
83 $cached = $this->memcache->get($cachekey);
85 // @see http://php.net/manual/en/memcache.get.php#84275
86 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
90 $value = @unserialize($cached);
92 // Only return a value if the serialized value is valid.
93 // We also check if the db entry is a serialized
94 // boolean 'false' value (which we want to return).
95 if ($cached === serialize(false) || $value !== false) {
105 public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
107 $cachekey = $this->getCacheKey($key);
109 // We store with the hostname as key to avoid problems with other applications
111 return $this->memcache->set(
118 return $this->memcache->set(
129 public function delete($key)
131 $cachekey = $this->getCacheKey($key);
132 return $this->memcache->delete($cachekey);
138 public function clear($outdated = true)
143 return $this->memcache->flush();
150 public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
152 $cachekey = $this->getCacheKey($key);
153 return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
159 public function getName()
161 return Type::MEMCACHE;