]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/MemcachedCacheDriver.php
Improve doc blocks in cache drivers
[friendica.git] / src / Core / Cache / MemcachedCacheDriver.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5 use Friendica\Core\Cache;
6
7 use Exception;
8 use Memcached;
9
10 /**
11  * Memcached Cache Driver
12  *
13  * @author Hypolite Petovan <hypolite@mrpetovan.com>
14  */
15 class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
16 {
17         use TraitCompareSet;
18         use TraitCompareDelete;
19
20         /**
21          * @var \Memcached
22          */
23         private $memcached;
24
25         /**
26          * Due to limitations of the INI format, the expected configuration for Memcached servers is the following:
27          * array {
28          *   0 => "hostname, port(, weight)",
29          *   1 => ...
30          * }
31          *
32          * @param array $memcached_hosts
33          * @throws \Exception
34          */
35         public function __construct(array $memcached_hosts)
36         {
37                 if (!class_exists('Memcached', false)) {
38                         throw new Exception('Memcached class isn\'t available');
39                 }
40
41                 $this->memcached = new Memcached();
42
43                 array_walk($memcached_hosts, function (&$value) {
44                         if (is_string($value)) {
45                                 $value = array_map('trim', explode(',', $value));
46                         }
47                 });
48
49                 $this->memcached->addServers($memcached_hosts);
50
51                 if (count($this->memcached->getServerList()) == 0) {
52                         throw new Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
53                 }
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                 $value = $this->memcached->get($cachekey);
66
67                 if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
68                         $return = $value;
69                 }
70
71                 return $return;
72         }
73
74         /**
75          * (@inheritdoc)
76          */
77         public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
78         {
79                 $cachekey = $this->getCacheKey($key);
80
81                 // We store with the hostname as key to avoid problems with other applications
82                 if ($ttl > 0) {
83                         return $this->memcached->set(
84                                 $cachekey,
85                                 $value,
86                                 $ttl
87                         );
88                 } else {
89                         return $this->memcached->set(
90                                 $cachekey,
91                                 $value
92                         );
93                 }
94
95         }
96
97         /**
98          * (@inheritdoc)
99          */
100         public function delete($key)
101         {
102                 $cachekey = $this->getCacheKey($key);
103                 return $this->memcached->delete($cachekey);
104         }
105
106         /**
107          * (@inheritdoc)
108          */
109         public function clear($outdated = true)
110         {
111                 if ($outdated) {
112                         return true;
113                 } else {
114                         return $this->memcached->flush();
115                 }
116         }
117
118         /**
119          * (@inheritdoc)
120          */
121         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
122         {
123                 $cachekey = $this->getCacheKey($key);
124                 return $this->memcached->add($cachekey, $value, $ttl);
125         }
126 }