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