]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/Type/MemcacheCache.php
Replace "notice" calls
[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          * Memcache doesn't allow spaces in keys
73          *
74          * @param string $key
75          * @return string
76          */
77         protected function getCacheKey(string $key): string
78         {
79                 return str_replace(' ', '_', parent::getCacheKey($key));
80         }
81
82         /**
83          * (@inheritdoc)
84          */
85         public function getAllKeys(?string $prefix = null): array
86         {
87                 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
88
89                 return $this->filterArrayKeysByPrefix($keys, $prefix);
90         }
91
92         /**
93          * (@inheritdoc)
94          */
95         public function get(string $key)
96         {
97                 $cacheKey = $this->getCacheKey($key);
98
99                 // We fetch with the hostname as key to avoid problems with other applications
100                 $cached = $this->memcache->get($cacheKey);
101
102                 // @see http://php.net/manual/en/memcache.get.php#84275
103                 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
104                         return null;
105                 }
106
107                 $value = @unserialize($cached);
108
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) {
113                         return $value;
114                 }
115
116                 return null;
117         }
118
119         /**
120          * (@inheritdoc)
121          */
122         public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
123         {
124                 $cacheKey = $this->getCacheKey($key);
125
126                 // We store with the hostname as key to avoid problems with other applications
127                 if ($ttl > 0) {
128                         return $this->memcache->set(
129                                 $cacheKey,
130                                 serialize($value),
131                                 MEMCACHE_COMPRESSED,
132                                 time() + $ttl
133                         );
134                 } else {
135                         return $this->memcache->set(
136                                 $cacheKey,
137                                 serialize($value),
138                                 MEMCACHE_COMPRESSED
139                         );
140                 }
141         }
142
143         /**
144          * (@inheritdoc)
145          */
146         public function delete(string $key): bool
147         {
148                 $cacheKey = $this->getCacheKey($key);
149                 return $this->memcache->delete($cacheKey);
150         }
151
152         /**
153          * (@inheritdoc)
154          */
155         public function clear(bool $outdated = true): bool
156         {
157                 if ($outdated) {
158                         return true;
159                 } else {
160                         return $this->memcache->flush();
161                 }
162         }
163
164         /**
165          * (@inheritdoc)
166          */
167         public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
168         {
169                 $cacheKey = $this->getCacheKey($key);
170                 return $this->memcache->add($cacheKey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
171         }
172
173         /**
174          * {@inheritDoc}
175          */
176         public function getName(): string
177         {
178                 return Type::MEMCACHE;
179         }
180 }