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