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;
25 use Friendica\Core\Cache\Enum\Duration;
26 use Friendica\Core\Cache\IMemoryCache;
27 use Friendica\Core\Cache\Type\TraitCompareDelete;
28 use Friendica\Core\Cache\Type\TraitCompareSet;
29 use Friendica\Core\Cache\Type\TraitMemcacheCommand;
30 use Friendica\Core\Cache\Enum\Type;
31 use Friendica\Core\Config\IConfig;
37 class MemcacheCache extends BaseCache implements IMemoryCache
40 use TraitCompareDelete;
41 use TraitMemcacheCommand;
51 public function __construct(string $hostname, IConfig $config)
53 if (!class_exists('Memcache', false)) {
54 throw new Exception('Memcache class isn\'t available');
57 parent::__construct($hostname);
59 $this->memcache = new Memcache();
61 $this->server = $config->get('system', 'memcache_host');;
62 $this->port = $config->get('system', 'memcache_port');
64 if (!@$this->memcache->connect($this->server, $this->port)) {
65 throw new Exception('Expected Memcache server at ' . $this->server . ':' . $this->port . ' isn\'t available');
72 public function getAllKeys($prefix = null)
74 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
76 return $this->filterArrayKeysByPrefix($keys, $prefix);
82 public function get($key)
85 $cachekey = $this->getCacheKey($key);
87 // We fetch with the hostname as key to avoid problems with other applications
88 $cached = $this->memcache->get($cachekey);
90 // @see http://php.net/manual/en/memcache.get.php#84275
91 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
95 $value = @unserialize($cached);
97 // Only return a value if the serialized value is valid.
98 // We also check if the db entry is a serialized
99 // boolean 'false' value (which we want to return).
100 if ($cached === serialize(false) || $value !== false) {
110 public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
112 $cachekey = $this->getCacheKey($key);
114 // We store with the hostname as key to avoid problems with other applications
116 return $this->memcache->set(
123 return $this->memcache->set(
134 public function delete($key)
136 $cachekey = $this->getCacheKey($key);
137 return $this->memcache->delete($cachekey);
143 public function clear($outdated = true)
148 return $this->memcache->flush();
155 public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
157 $cachekey = $this->getCacheKey($key);
158 return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
164 public function getName()
166 return Type::MEMCACHE;