Merge pull request #5394 from MrPetovan/bug/fix-config-set-return-value
[friendica.git/.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 <mrpetovan@gmail.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         public function get($key)
57         {
58                 $return = null;
59                 $cachekey = $this->getCacheKey($key);
60
61                 // We fetch with the hostname as key to avoid problems with other applications
62                 $value = $this->memcached->get($cachekey);
63
64                 if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
65                         $return = $value;
66                 }
67
68                 return $return;
69         }
70
71         public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
72         {
73                 $cachekey = $this->getCacheKey($key);
74
75                 // We store with the hostname as key to avoid problems with other applications
76                 if ($ttl > 0) {
77                         return $this->memcached->set(
78                                 $cachekey,
79                                 $value,
80                                 $ttl
81                         );
82                 } else {
83                         return $this->memcached->set(
84                                 $cachekey,
85                                 $value
86                         );
87                 }
88
89         }
90
91         public function delete($key)
92         {
93                 $cachekey = $this->getCacheKey($key);
94                 return $this->memcached->delete($cachekey);
95         }
96
97         public function clear($outdated = true)
98         {
99                 if ($outdated) {
100                         return true;
101                 } else {
102                         return $this->memcached->flush();
103                 }
104         }
105
106         /**
107          * @brief Sets a value if it's not already stored
108          *
109          * @param string $key      The cache key
110          * @param mixed  $value    The old value we know from the cache
111          * @param int    $ttl      The cache lifespan, must be one of the Cache constants
112          * @return bool
113          */
114         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
115         {
116                 $cachekey = $this->getCacheKey($key);
117                 return $this->memcached->add($cachekey, $value, $ttl);
118         }
119 }