"Imported" name spaces (#5361)
[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         public function __construct(array $memcached_hosts)
26         {
27                 if (!class_exists('Memcached', false)) {
28                         throw new Exception('Memcached class isn\'t available');
29                 }
30
31                 $this->memcached = new Memcached();
32
33                 $this->memcached->addServers($memcached_hosts);
34
35                 if (count($this->memcached->getServerList()) == 0) {
36                         throw new Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
37                 }
38         }
39
40         public function get($key)
41         {
42                 $return = null;
43                 $cachekey = $this->getCacheKey($key);
44
45                 // We fetch with the hostname as key to avoid problems with other applications
46                 $value = $this->memcached->get($cachekey);
47
48                 if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
49                         $return = $value;
50                 }
51
52                 return $return;
53         }
54
55         public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
56         {
57                 $cachekey = $this->getCacheKey($key);
58
59                 // We store with the hostname as key to avoid problems with other applications
60                 if ($ttl > 0) {
61                         return $this->memcached->set(
62                                 $cachekey,
63                                 $value,
64                                 $ttl
65                         );
66                 } else {
67                         return $this->memcached->set(
68                                 $cachekey,
69                                 $value
70                         );
71                 }
72
73         }
74
75         public function delete($key)
76         {
77                 $cachekey = $this->getCacheKey($key);
78                 return $this->memcached->delete($cachekey);
79         }
80
81         public function clear($outdated = true)
82         {
83                 if ($outdated) {
84                         return true;
85                 } else {
86                         return $this->memcached->flush();
87                 }
88         }
89
90         /**
91          * @brief Sets a value if it's not already stored
92          *
93          * @param string $key      The cache key
94          * @param mixed  $value    The old value we know from the cache
95          * @param int    $ttl      The cache lifespan, must be one of the Cache constants
96          * @return bool
97          */
98         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
99         {
100                 $cachekey = $this->getCacheKey($key);
101                 return $this->memcached->add($cachekey, $value, $ttl);
102         }
103 }