Merge branch 'item-activities' of github.com:annando/friendica into item-activities
[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 implements IMemoryCacheDriver
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                                 $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($outdated = true)
79         {
80                 if ($outdated) {
81                         return true;
82                 } else {
83                         return $this->memcached->flush();
84                 }
85         }
86
87         /**
88          * @brief Sets a value if it's not already stored
89          *
90          * @param string $key      The cache key
91          * @param mixed  $value    The old value we know from the cache
92          * @param int    $ttl      The cache lifespan, must be one of the Cache constants
93          * @return bool
94          */
95         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
96         {
97                 $cachekey = $this->getCacheKey($key);
98                 return $this->memcached->add($cachekey, $value, $ttl);
99         }
100 }