]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/MemcacheCacheDriver.php
bff543b542478ed4db0f80f9e7488f49cae758ed
[friendica.git] / src / Core / Cache / MemcacheCacheDriver.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5 use Friendica\Core\Cache;
6
7 /**
8  * Memcache Cache Driver
9  *
10  * @author Hypolite Petovan <mrpetovan@gmail.com>
11  */
12 class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
13 {
14         use TraitCompareSet;
15         use TraitCompareDelete;
16
17         /**
18          * @var \Memcache
19          */
20         private $memcache;
21
22         public function __construct($memcache_host, $memcache_port)
23         {
24                 if (!class_exists('Memcache', false)) {
25                         throw new \Exception('Memcache class isn\'t available');
26                 }
27
28                 $this->memcache = new \Memcache();
29
30                 if (!$this->memcache->connect($memcache_host, $memcache_port)) {
31                         throw new \Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');
32                 }
33         }
34
35         /**
36          * (@inheritdoc)
37          */
38         public function get($key)
39         {
40                 $return = null;
41                 $cachekey = $this->getCacheKey($key);
42
43                 // We fetch with the hostname as key to avoid problems with other applications
44                 $cached = $this->memcache->get($cachekey);
45
46                 // @see http://php.net/manual/en/memcache.get.php#84275
47                 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
48                         return $return;
49                 }
50
51                 $value = @unserialize($cached);
52
53                 // Only return a value if the serialized value is valid.
54                 // We also check if the db entry is a serialized
55                 // boolean 'false' value (which we want to return).
56                 if ($cached === serialize(false) || $value !== false) {
57                         $return = $value;
58                 }
59
60                 return $return;
61         }
62
63         /**
64          * (@inheritdoc)
65          */
66         public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
67         {
68                 $cachekey = $this->getCacheKey($key);
69
70                 // We store with the hostname as key to avoid problems with other applications
71                 if ($ttl > 0) {
72                         return $this->memcache->set(
73                                 $cachekey,
74                                 serialize($value),
75                                 MEMCACHE_COMPRESSED,
76                                 time() + $ttl
77                         );
78                 } else {
79                         return $this->memcache->set(
80                                 $cachekey,
81                                 serialize($value),
82                                 MEMCACHE_COMPRESSED
83                         );
84                 }
85         }
86
87         /**
88          * (@inheritdoc)
89          */
90         public function delete($key)
91         {
92                 $cachekey = $this->getCacheKey($key);
93                 return $this->memcache->delete($cachekey);
94         }
95
96         /**
97          * (@inheritdoc)
98          */
99         public function clear($outdated = true)
100         {
101                 if ($outdated) {
102                         return true;
103                 } else {
104                         return $this->memcache->flush();
105                 }
106         }
107
108         /**
109          * (@inheritdoc)
110          */
111         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
112         {
113                 $cachekey = $this->getCacheKey($key);
114                 return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
115         }
116 }