ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
17  protected static $memcache_object;
18 
19 
24  public function __construct($service_id, $component) {
25  if (!(self::$memcache_object instanceof Memcached) AND $this->getInstallable()) {
29  $memcached = new Memcached();
30 
31  if (ilMemcacheServer::count() > 0) {
32 
33  $memcached->resetServerList();
34  $servers = array();
35  foreach (ilMemcacheServer::where(array( 'status' => ilMemcacheServer::STATUS_ACTIVE ))->get() as $ilMemcacheServer) {
36  $servers[] = array( $ilMemcacheServer->getHost(), $ilMemcacheServer->getPort(), $ilMemcacheServer->getWeight() );
37  }
38  $memcached->addServers($servers);
39  }
40 
41  self::$memcache_object = $memcached;
42  }
43  parent::__construct($service_id, $component);
44  }
45 
46 
50  protected function getMemcacheObject() {
51  return self::$memcache_object;
52  }
53 
54 
60  public function exists($key) {
61  return $this->getMemcacheObject()->get($this->returnKey($key)) != null;
62  }
63 
64 
72  public function set($key, $serialized_value, $ttl = null) {
73  return $this->getMemcacheObject()->set($this->returnKey($key), $serialized_value, (int)$ttl);
74  }
75 
76 
82  public function get($key) {
83  return $this->getMemcacheObject()->get($this->returnKey($key));
84  }
85 
86 
92  public function delete($key) {
93  return $this->getMemcacheObject()->delete($this->returnKey($key));
94  }
95 
96 
100  public function flush() {
101  return $this->getMemcacheObject()->flush();
102  }
103 
104 
108  protected function getActive() {
109  if ($this->getInstallable()) {
110  $stats = $this->getMemcacheObject()->getStats();
111 
112  if (!is_array($stats)) {
113  return false;
114  }
115 
116  foreach ($stats as $server) {
117  if ($server['pid'] > 0) {
118  return true;
119  }
120  }
121 
122  return false;
123  }
124 
125  return false;
126  }
127 
128 
132  protected function getInstallable() {
133  return class_exists('Memcached');
134  }
135 
136 
140  public function getInstallationFailureReason() {
141  if ($this->getMemcacheObject() instanceof Memcached) {
142  $stats = $this->getMemcacheObject()->getStats();
143 
144  if (!$stats[self::STD_SERVER . ':' . self::STD_PORT]['pid'] > 0) {
145  return 'No Memcached-Server available';
146  }
147  }
148 
149  return parent::getInstallationFailureReason();
150  }
151 
152 
158  public function serialize($value) {
159  return serialize($value);
160  }
161 
162 
168  public function unserialize($serialized_value) {
169  return unserialize($serialized_value);
170  }
171 
172 
176  public function getInfo() {
177  if (self::isInstallable()) {
178  $return = array();
179  $return['__cache_info'] = $this->getMemcacheObject()->getStats();
180  foreach ($this->getMemcacheObject()->getAllKeys() as $key) {
181  $return[$key] = $this->getMemcacheObject()->get($key);
182  }
183 
184  return $return;
185  }
186  }
187 
188 
192  public function isValid($key) {
193  return true;
194  }
195 }
__construct($service_id, $component)
unserialize($serialized_value)
Class ilGlobalCacheService.
static where($where, $operator=null)
static $memcache_object
Create styles array
The data for the language used.
getInstallationFailureReason()
$server
Class ilMemcache.