ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 }
50 parent::__construct($service_id, $component);
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
113 public function flush()
114 {
115 return $this->getMemcacheObject()->flush();
116 }
117
118
122 protected function getActive()
123 {
124 if ($this->getInstallable()) {
125 $stats = $this->getMemcacheObject()->getStats();
126
127 if (!is_array($stats)) {
128 return false;
129 }
130
131 foreach ($stats as $server) {
132 if ($server['pid'] > 0) {
133 return true;
134 }
135 }
136
137 return false;
138 }
139
140 return false;
141 }
142
143
147 protected function getInstallable()
148 {
149 return class_exists('Memcached');
150 }
151
152
157 {
158 if ($this->getMemcacheObject() instanceof Memcached) {
159 $stats = $this->getMemcacheObject()->getStats();
160
161 if (!$stats[self::STD_SERVER . ':' . self::STD_PORT]['pid'] > 0) {
162 return 'No Memcached-Server available';
163 }
164 }
165
166 return parent::getInstallationFailureReason();
167 }
168
169
175 public function serialize($value)
176 {
177 return serialize($value);
178 }
179
180
186 public function unserialize($serialized_value)
187 {
188 return unserialize($serialized_value);
189 }
190
191
195 public function getInfo()
196 {
197 if (self::isInstallable()) {
198 $return = array();
199 $return['__cache_info'] = $this->getMemcacheObject()->getStats();
200 foreach ($this->getMemcacheObject()->getAllKeys() as $key) {
201 $return[$key] = $this->getMemcacheObject()->get($key);
202 }
203
204 return $return;
205 }
206 }
207
208
212 public function isValid($key)
213 {
214 return true;
215 }
216}
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)
$key
Definition: croninfo.php:18
$server
Definition: getUserInfo.php:12
$stats
if(isset($_REQUEST['delete'])) $list
Definition: registry.php:41