ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilMemcache.php
Go to the documentation of this file.
1<?php
2
3require_once('./Services/GlobalCache/classes/class.ilGlobalCacheService.php');
4require_once('class.ilMemcacheServer.php');
5
13{
14
18 protected static $memcache_object;
19
20
25 public function __construct($service_id, $component)
26 {
27 if (!(self::$memcache_object instanceof Memcached) and $this->getInstallable()) {
31 $memcached = new Memcached();
32
33 if (ilMemcacheServer::count() > 0) {
34 $memcached->resetServerList();
35 $servers = array();
37 ->get();
38 foreach ($list as $ilMemcacheServer) {
39 $servers[] = array(
40 $ilMemcacheServer->getHost(),
41 $ilMemcacheServer->getPort(),
42 $ilMemcacheServer->getWeight(),
43 );
44 }
45 $memcached->addServers($servers);
46 }
47
48 self::$memcache_object = $memcached;
49 }
51 }
52
53
57 protected function getMemcacheObject()
58 {
60 }
61
62
68 public function exists($key)
69 {
70 return $this->getMemcacheObject()->get($this->returnKey($key)) != null;
71 }
72
73
81 public function set($key, $serialized_value, $ttl = null)
82 {
83 return $this->getMemcacheObject()
84 ->set($this->returnKey($key), $serialized_value, (int) $ttl);
85 }
86
87
93 public function get($key)
94 {
95 return $this->getMemcacheObject()->get($this->returnKey($key));
96 }
97
98
104 public function delete($key)
105 {
106 return $this->getMemcacheObject()->delete($this->returnKey($key));
107 }
108
109
115 public function flush($complete = false)
116 {
117 // currently a partial flushing is missing
118 return $this->getMemcacheObject()->flush();
119 }
120
121
125 protected function getActive()
126 {
127 if ($this->getInstallable()) {
128 $stats = $this->getMemcacheObject()->getStats();
129
130 if (!is_array($stats)) {
131 return false;
132 }
133
134 foreach ($stats as $server) {
135 if ($server['pid'] > 0) {
136 return true;
137 }
138 }
139
140 return false;
141 }
142
143 return false;
144 }
145
146
150 protected function getInstallable()
151 {
152 return class_exists('Memcached');
153 }
154
155
160 {
161 if ($this->getMemcacheObject() instanceof Memcached) {
162 $stats = $this->getMemcacheObject()->getStats();
163
164 if (!$stats[self::STD_SERVER . ':' . self::STD_PORT]['pid'] > 0) {
165 return 'No Memcached-Server available';
166 }
167 }
168
169 return parent::getInstallationFailureReason();
170 }
171
172
178 public function serialize($value)
179 {
180 return serialize($value);
181 }
182
183
189 public function unserialize($serialized_value)
190 {
191 return unserialize($serialized_value);
192 }
193
194
198 public function getInfo()
199 {
200 if (self::isInstallable()) {
201 $return = array();
202 $return['__cache_info'] = $this->getMemcacheObject()->getStats();
203 foreach ($this->getMemcacheObject()->getAllKeys() as $key) {
204 $return[$key] = $this->getMemcacheObject()->get($key);
205 }
206
207 return $return;
208 }
209 }
210
211
215 public function isValid($key)
216 {
217 return true;
218 }
219}
static where($where, $operator=null)
An exception for terminatinating execution or to throw for unit testing.
Class ilGlobalCacheService.
__construct($service_id, $component)
Class ilMemcache.
getInstallationFailureReason()
static $memcache_object
isValid($key)
Checks whether the cache key is valid or not.This method exists only for legacy reasons and has only ...
unserialize($serialized_value)
flush($complete=false)
$server
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc