]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/MemcacheCache.php
Merge pull request #7754 from annando/aria
[friendica.git] / src / Core / Cache / MemcacheCache.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5 use Exception;
6 use Friendica\Core\Config\Configuration;
7 use Memcache;
8
9 /**
10  * Memcache Cache
11  *
12  * @author Hypolite Petovan <hypolite@mrpetovan.com>
13  */
14 class MemcacheCache extends Cache implements IMemoryCache
15 {
16         use TraitCompareSet;
17         use TraitCompareDelete;
18         use TraitMemcacheCommand;
19
20         /**
21          * @var Memcache
22          */
23         private $memcache;
24
25         /**
26          * @throws Exception
27          */
28         public function __construct(string $hostname, Configuration $config)
29         {
30                 if (!class_exists('Memcache', false)) {
31                         throw new Exception('Memcache class isn\'t available');
32                 }
33
34                 parent::__construct($hostname);
35
36                 $this->memcache = new Memcache();
37
38                 $this->server = $config->get('system', 'memcache_host');;
39                 $this->port = $config->get('system', 'memcache_port');
40
41                 if (!@$this->memcache->connect($this->server, $this->port)) {
42                         throw new Exception('Expected Memcache server at ' . $this->server . ':' . $this->port . ' isn\'t available');
43                 }
44         }
45
46         /**
47          * (@inheritdoc)
48          */
49         public function getAllKeys($prefix = null)
50         {
51                 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
52
53                 return $this->filterArrayKeysByPrefix($keys, $prefix);
54         }
55
56         /**
57          * (@inheritdoc)
58          */
59         public function get($key)
60         {
61                 $return   = null;
62                 $cachekey = $this->getCacheKey($key);
63
64                 // We fetch with the hostname as key to avoid problems with other applications
65                 $cached = $this->memcache->get($cachekey);
66
67                 // @see http://php.net/manual/en/memcache.get.php#84275
68                 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
69                         return $return;
70                 }
71
72                 $value = @unserialize($cached);
73
74                 // Only return a value if the serialized value is valid.
75                 // We also check if the db entry is a serialized
76                 // boolean 'false' value (which we want to return).
77                 if ($cached === serialize(false) || $value !== false) {
78                         $return = $value;
79                 }
80
81                 return $return;
82         }
83
84         /**
85          * (@inheritdoc)
86          */
87         public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
88         {
89                 $cachekey = $this->getCacheKey($key);
90
91                 // We store with the hostname as key to avoid problems with other applications
92                 if ($ttl > 0) {
93                         return $this->memcache->set(
94                                 $cachekey,
95                                 serialize($value),
96                                 MEMCACHE_COMPRESSED,
97                                 time() + $ttl
98                         );
99                 } else {
100                         return $this->memcache->set(
101                                 $cachekey,
102                                 serialize($value),
103                                 MEMCACHE_COMPRESSED
104                         );
105                 }
106         }
107
108         /**
109          * (@inheritdoc)
110          */
111         public function delete($key)
112         {
113                 $cachekey = $this->getCacheKey($key);
114                 return $this->memcache->delete($cachekey);
115         }
116
117         /**
118          * (@inheritdoc)
119          */
120         public function clear($outdated = true)
121         {
122                 if ($outdated) {
123                         return true;
124                 } else {
125                         return $this->memcache->flush();
126                 }
127         }
128
129         /**
130          * (@inheritdoc)
131          */
132         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
133         {
134                 $cachekey = $this->getCacheKey($key);
135                 return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
136         }
137
138         /**
139          * {@inheritDoc}
140          */
141         public function getName()
142         {
143                 return self::TYPE_MEMCACHE;
144         }
145 }