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