ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMemcache.php
Go to the documentation of this file.
1 <?php
2 
3 require_once('./Services/GlobalCache/classes/class.ilGlobalCacheService.php');
4 require_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  }
50  parent::__construct($service_id, $component);
51  }
52 
53 
57  protected function getMemcacheObject()
58  {
59  return self::$memcache_object;
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 }
if(isset($_REQUEST['delete'])) $list
Definition: registry.php:41
__construct($service_id, $component)
unserialize($serialized_value)
Class ilGlobalCacheService.
$server
Definition: sabredav.php:48
static where($where, $operator=null)
flush($complete=false)
static $memcache_object
getInstallationFailureReason()
Class ilMemcache.
$key
Definition: croninfo.php:18