]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/MemcacheCacheDriver.php
37327ebba92e9d6230b6a5be588682d117f802fa
[friendica.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         /**
26          * @param string $memcache_host
27          * @param int    $memcache_port
28          * @throws Exception
29          */
30         public function __construct($memcache_host, $memcache_port)
31         {
32                 if (!class_exists('Memcache', false)) {
33                         throw new Exception('Memcache class isn\'t available');
34                 }
35
36                 $this->memcache = new Memcache();
37
38                 if (!$this->memcache->connect($memcache_host, $memcache_port)) {
39                         throw new Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');
40                 }
41         }
42
43         /**
44          * (@inheritdoc)
45          */
46         public function get($key)
47         {
48                 $return = null;
49                 $cachekey = $this->getCacheKey($key);
50
51                 // We fetch with the hostname as key to avoid problems with other applications
52                 $cached = $this->memcache->get($cachekey);
53
54                 // @see http://php.net/manual/en/memcache.get.php#84275
55                 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
56                         return $return;
57                 }
58
59                 $value = @unserialize($cached);
60
61                 // Only return a value if the serialized value is valid.
62                 // We also check if the db entry is a serialized
63                 // boolean 'false' value (which we want to return).
64                 if ($cached === serialize(false) || $value !== false) {
65                         $return = $value;
66                 }
67
68                 return $return;
69         }
70
71         /**
72          * (@inheritdoc)
73          */
74         public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
75         {
76                 $cachekey = $this->getCacheKey($key);
77
78                 // We store with the hostname as key to avoid problems with other applications
79                 if ($ttl > 0) {
80                         return $this->memcache->set(
81                                 $cachekey,
82                                 serialize($value),
83                                 MEMCACHE_COMPRESSED,
84                                 time() + $ttl
85                         );
86                 } else {
87                         return $this->memcache->set(
88                                 $cachekey,
89                                 serialize($value),
90                                 MEMCACHE_COMPRESSED
91                         );
92                 }
93         }
94
95         /**
96          * (@inheritdoc)
97          */
98         public function delete($key)
99         {
100                 $cachekey = $this->getCacheKey($key);
101                 return $this->memcache->delete($cachekey);
102         }
103
104         /**
105          * (@inheritdoc)
106          */
107         public function clear($outdated = true)
108         {
109                 if ($outdated) {
110                         return true;
111                 } else {
112                         return $this->memcache->flush();
113                 }
114         }
115
116         /**
117          * (@inheritdoc)
118          */
119         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
120         {
121                 $cachekey = $this->getCacheKey($key);
122                 return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
123         }
124 }