Bugfixings for Cache-Lock
[friendica.git/.git] / src / Core / Cache / MemcachedCacheDriver.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5 use Friendica\Core\Cache;
6
7 /**
8  * Memcached Cache Driver
9  *
10  * @author Hypolite Petovan <mrpetovan@gmail.com>
11  */
12 class MemcachedCacheDriver extends AbstractCacheDriver
13 {
14         use TraitCompareSet;
15         use TraitCompareDelete;
16
17         /**
18          * @var \Memcached
19          */
20         private $memcached;
21
22         public function __construct(array $memcached_hosts)
23         {
24                 if (!class_exists('Memcached', false)) {
25                         throw new \Exception('Memcached class isn\'t available');
26                 }
27
28                 $this->memcached = new \Memcached();
29
30                 $this->memcached->addServers($memcached_hosts);
31
32                 if (count($this->memcached->getServerList()) == 0) {
33                         throw new \Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
34                 }
35         }
36
37         public function get($key)
38         {
39                 $return = null;
40                 $cachekey = $this->getCacheKey($key);
41
42                 // We fetch with the hostname as key to avoid problems with other applications
43                 $value = $this->memcached->get($cachekey);
44
45                 if ($this->memcached->getResultCode() === \Memcached::RES_SUCCESS) {
46                         $return = $value;
47                 }
48
49                 return $return;
50         }
51
52         public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
53         {
54                 $cachekey = $this->getCacheKey($key);
55
56                 // We store with the hostname as key to avoid problems with other applications
57                 if ($ttl > 0) {
58                         return $this->memcached->set(
59                                 $cachekey,
60                                 $value,
61                                 time() + $ttl
62                         );
63                 } else {
64                         return $this->memcached->set(
65                                 $cachekey,
66                                 $value
67                         );
68                 }
69
70         }
71
72         public function delete($key)
73         {
74                 $cachekey = $this->getCacheKey($key);
75                 return $this->memcached->delete($cachekey);
76         }
77
78         public function clear()
79         {
80                 return true;
81         }
82
83         /**
84          * @brief Sets a value if it's not already stored
85          *
86          * @param string $key      The cache key
87          * @param mixed  $value    The old value we know from the cache
88          * @param int    $ttl      The cache lifespan, must be one of the Cache constants
89          * @return bool
90          */
91         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
92         {
93                 $cachekey = $this->getCacheKey($key);
94                 return $this->memcached->add($cachekey, $value, $ttl);
95         }
96 }