]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/MemcacheCache.php
Remove duplicate profile_uid key in App->profile array
[friendica.git] / src / Core / Cache / MemcacheCache.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5 use Exception;
6 use Friendica\Core\BaseCache;
7 use Friendica\Core\Config\IConfig;
8 use Memcache;
9
10 /**
11  * Memcache Cache
12  *
13  * @author Hypolite Petovan <hypolite@mrpetovan.com>
14  */
15 class MemcacheCache extends BaseCache implements IMemoryCache
16 {
17         use TraitCompareSet;
18         use TraitCompareDelete;
19         use TraitMemcacheCommand;
20
21         /**
22          * @var Memcache
23          */
24         private $memcache;
25
26         /**
27          * @throws Exception
28          */
29         public function __construct(string $hostname, IConfig $config)
30         {
31                 if (!class_exists('Memcache', false)) {
32                         throw new Exception('Memcache class isn\'t available');
33                 }
34
35                 parent::__construct($hostname);
36
37                 $this->memcache = new Memcache();
38
39                 $this->server = $config->get('system', 'memcache_host');;
40                 $this->port = $config->get('system', 'memcache_port');
41
42                 if (!@$this->memcache->connect($this->server, $this->port)) {
43                         throw new Exception('Expected Memcache server at ' . $this->server . ':' . $this->port . ' isn\'t available');
44                 }
45         }
46
47         /**
48          * (@inheritdoc)
49          */
50         public function getAllKeys($prefix = null)
51         {
52                 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
53
54                 return $this->filterArrayKeysByPrefix($keys, $prefix);
55         }
56
57         /**
58          * (@inheritdoc)
59          */
60         public function get($key)
61         {
62                 $return   = null;
63                 $cachekey = $this->getCacheKey($key);
64
65                 // We fetch with the hostname as key to avoid problems with other applications
66                 $cached = $this->memcache->get($cachekey);
67
68                 // @see http://php.net/manual/en/memcache.get.php#84275
69                 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
70                         return $return;
71                 }
72
73                 $value = @unserialize($cached);
74
75                 // Only return a value if the serialized value is valid.
76                 // We also check if the db entry is a serialized
77                 // boolean 'false' value (which we want to return).
78                 if ($cached === serialize(false) || $value !== false) {
79                         $return = $value;
80                 }
81
82                 return $return;
83         }
84
85         /**
86          * (@inheritdoc)
87          */
88         public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
89         {
90                 $cachekey = $this->getCacheKey($key);
91
92                 // We store with the hostname as key to avoid problems with other applications
93                 if ($ttl > 0) {
94                         return $this->memcache->set(
95                                 $cachekey,
96                                 serialize($value),
97                                 MEMCACHE_COMPRESSED,
98                                 time() + $ttl
99                         );
100                 } else {
101                         return $this->memcache->set(
102                                 $cachekey,
103                                 serialize($value),
104                                 MEMCACHE_COMPRESSED
105                         );
106                 }
107         }
108
109         /**
110          * (@inheritdoc)
111          */
112         public function delete($key)
113         {
114                 $cachekey = $this->getCacheKey($key);
115                 return $this->memcache->delete($cachekey);
116         }
117
118         /**
119          * (@inheritdoc)
120          */
121         public function clear($outdated = true)
122         {
123                 if ($outdated) {
124                         return true;
125                 } else {
126                         return $this->memcache->flush();
127                 }
128         }
129
130         /**
131          * (@inheritdoc)
132          */
133         public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
134         {
135                 $cachekey = $this->getCacheKey($key);
136                 return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
137         }
138
139         /**
140          * {@inheritDoc}
141          */
142         public function getName()
143         {
144                 return Type::MEMCACHE;
145         }
146 }