]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/Type/MemcacheCache.php
Merge branch 'friendica:develop' into 6606-k-alin-mysql-unix-socket
[friendica.git] / src / Core / Cache / Type / MemcacheCache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Core\Cache\Type;
23
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;
30 use Memcache;
31
32 /**
33  * Memcache Cache
34  */
35 class MemcacheCache extends AbstractCache implements ICanCacheInMemory
36 {
37         use CompareSetTrait;
38         use CompareDeleteTrait;
39         use MemcacheCommandTrait;
40
41         /**
42          * @var Memcache
43          */
44         private $memcache;
45
46         /**
47          * @param string              $hostname
48          * @param IManageConfigValues $config
49          *
50          * @throws InvalidCacheDriverException
51          * @throws CachePersistenceException
52          */
53         public function __construct(string $hostname, IManageConfigValues $config)
54         {
55                 if (!class_exists('Memcache', false)) {
56                         throw new InvalidCacheDriverException('Memcache class isn\'t available');
57                 }
58
59                 parent::__construct($hostname);
60
61                 $this->memcache = new Memcache();
62
63                 $this->server = $config->get('system', 'memcache_host');
64                 $this->port   = $config->get('system', 'memcache_port');
65
66                 if (!@$this->memcache->connect($this->server, $this->port)) {
67                         throw new CachePersistenceException('Expected Memcache server at ' . $this->server . ':' . $this->port . ' isn\'t available');
68                 }
69         }
70
71         /**
72          * (@inheritdoc)
73          */
74         public function getAllKeys(?string $prefix = null): array
75         {
76                 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
77
78                 return $this->filterArrayKeysByPrefix($keys, $prefix);
79         }
80
81         /**
82          * (@inheritdoc)
83          */
84         public function get(string $key)
85         {
86                 $cacheKey = $this->getCacheKey($key);
87
88                 // We fetch with the hostname as key to avoid problems with other applications
89                 $cached = $this->memcache->get($cacheKey);
90
91                 // @see http://php.net/manual/en/memcache.get.php#84275
92                 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
93                         return null;
94                 }
95
96                 $value = @unserialize($cached);
97
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) {
102                         return $value;
103                 }
104
105                 return null;
106         }
107
108         /**
109          * (@inheritdoc)
110          */
111         public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
112         {
113                 $cacheKey = $this->getCacheKey($key);
114
115                 // We store with the hostname as key to avoid problems with other applications
116                 if ($ttl > 0) {
117                         return $this->memcache->set(
118                                 $cacheKey,
119                                 serialize($value),
120                                 MEMCACHE_COMPRESSED,
121                                 time() + $ttl
122                         );
123                 } else {
124                         return $this->memcache->set(
125                                 $cacheKey,
126                                 serialize($value),
127                                 MEMCACHE_COMPRESSED
128                         );
129                 }
130         }
131
132         /**
133          * (@inheritdoc)
134          */
135         public function delete(string $key): bool
136         {
137                 $cacheKey = $this->getCacheKey($key);
138                 return $this->memcache->delete($cacheKey);
139         }
140
141         /**
142          * (@inheritdoc)
143          */
144         public function clear(bool $outdated = true): bool
145         {
146                 if ($outdated) {
147                         return true;
148                 } else {
149                         return $this->memcache->flush();
150                 }
151         }
152
153         /**
154          * (@inheritdoc)
155          */
156         public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
157         {
158                 $cacheKey = $this->getCacheKey($key);
159                 return $this->memcache->add($cacheKey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
160         }
161
162         /**
163          * {@inheritDoc}
164          */
165         public function getName(): string
166         {
167                 return Type::MEMCACHE;
168         }
169 }