]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/MemcachedCacheDriver.php
Merge pull request #5813 from MrPetovan/task/update-composer
[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 getAllKeys()
60         {
61                 return $this->memcached->getAllKeys();
62         }
63
64         /**
65          * (@inheritdoc)
66          */
67         public function get($key)
68         {
69                 $return = null;
70                 $cachekey = $this->getCacheKey($key);
71
72                 // We fetch with the hostname as key to avoid problems with other applications
73                 $value = $this->memcached->get($cachekey);
74
75                 if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
76                         $return = $value;
77                 }
78
79                 return $return;
80         }
81
82         /**
83          * (@inheritdoc)
84          */
85         public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
86         {
87                 $cachekey = $this->getCacheKey($key);
88
89                 // We store with the hostname as key to avoid problems with other applications
90                 if ($ttl > 0) {
91                         return $this->memcached->set(
92                                 $cachekey,
93                                 $value,
94                                 $ttl
95                         );
96                 } else {
97                         return $this->memcached->set(
98                                 $cachekey,
99                                 $value
100                         );
101                 }
102
103         }
104
105         /**
106          * (@inheritdoc)
107          */
108         public function delete($key)
109         {
110                 $cachekey = $this->getCacheKey($key);
111                 return $this->memcached->delete($cachekey);
112         }
113
114         /**
115          * (@inheritdoc)
116          */
117         public function clear($outdated = true)
118         {
119                 if ($outdated) {
120                         return true;
121                 } else {
122                         return $this->memcached->flush();
123                 }
124         }
125
126         /**
127          * (@inheritdoc)
128          */
129         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
130         {
131                 $cachekey = $this->getCacheKey($key);
132                 return $this->memcached->add($cachekey, $value, $ttl);
133         }
134 }