]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/MemcacheCacheDriver.php
code standards / simplifications
[friendica.git] / src / Core / Cache / MemcacheCacheDriver.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5 use Friendica\BaseObject;
6 use Friendica\Core\Cache;
7
8 /**
9  * Memcache Cache Driver
10  *
11  * @author Hypolite Petovan <mrpetovan@gmail.com>
12  */
13 class MemcacheCacheDriver extends BaseObject implements IMemoryCacheDriver
14 {
15         use TraitCompareSet;
16         use TraitCompareDelete;
17
18         /**
19          * @var \Memcache
20          */
21         private $memcache;
22
23         public function __construct($memcache_host, $memcache_port)
24         {
25                 if (!class_exists('Memcache', false)) {
26                         throw new \Exception('Memcache class isn\'t available');
27                 }
28
29                 $this->memcache = new \Memcache();
30
31                 if (!$this->memcache->connect($memcache_host, $memcache_port)) {
32                         throw new \Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');
33                 }
34         }
35
36         /**
37          * (@inheritdoc)
38          */
39         public function get($key)
40         {
41                 $return = null;
42
43                 // We fetch with the hostname as key to avoid problems with other applications
44                 $cached = $this->memcache->get(self::getApp()->get_hostname() . ':' . $key);
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                 // We store with the hostname as key to avoid problems with other applications
69                 if ($ttl > 0) {
70                         return $this->memcache->set(
71                                 self::getApp()->get_hostname() . ":" . $key,
72                                 serialize($value),
73                                 MEMCACHE_COMPRESSED,
74                                 time() + $ttl
75                         );
76                 } else {
77                         return $this->memcache->set(
78                                 self::getApp()->get_hostname() . ":" . $key,
79                                 serialize($value),
80                                 MEMCACHE_COMPRESSED
81                         );
82                 }
83         }
84
85         /**
86          * (@inheritdoc)
87          */
88         public function delete($key)
89         {
90                 return $this->memcache->delete($key);
91         }
92
93         /**
94          * (@inheritdoc)
95          */
96         public function clear()
97         {
98                 return $this->memcache->flush();
99         }
100
101         /**
102          * (@inheritdoc)
103          */
104         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
105         {
106                 return $this->memcache->add(self::getApp()->get_hostname() . ":" . $key, $value, $ttl);
107         }
108 }