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