ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilGlobalCacheService.php
Go to the documentation of this file.
1 <?php
2 
9 abstract class ilGlobalCacheService {
10 
14  protected $current_time = 0;
18  protected $valid_keys = array();
22  protected static $active = array();
26  protected static $installable = array();
30  protected $service_id = '';
34  protected $component = '';
42  protected $valid_key_hash = '';
43 
44 
49  public function __construct($service_id, $component) {
50  $this->setComponent($component);
51  $this->setServiceId($service_id);
52  self::$active[get_called_class()] = $this->getActive();
53  self::$installable[get_called_class()] = ($this->getInstallable() AND $this->checkMemory());
54  // $this->current_time = time();
55  $this->readValid();
56  }
57 
58 
59  public function __destruct() {
60  $this->saveValid();
61  }
62 
63 
69  protected function saveValid() {
70  if ($this->isActive()) {
71  if ($this->valid_key_hash != md5(serialize($this->valid_keys))) {
72  $this->set('valid_keys', $this->serialize($this->valid_keys));
73  }
74  }
75  }
76 
77 
83  protected function readValid() {
84  if ($this->isActive()) {
85  $this->valid_keys = $this->unserialize($this->get('valid_keys'));
86  $this->valid_key_hash = md5(serialize($this->valid_keys));
87  }
88  }
89 
90 
94  abstract protected function getActive();
95 
96 
100  abstract protected function getInstallable();
101 
102 
108  abstract public function unserialize($serialized_value);
109 
110 
116  abstract public function get($key);
117 
118 
126  abstract public function set($key, $serialized_value, $ttl = NULL);
127 
128 
134  abstract public function serialize($value);
135 
136 
140  public function getServiceId() {
141  return $this->service_id;
142  }
143 
144 
148  public function setServiceId($service_id) {
149  $this->service_id = $service_id;
150  }
151 
152 
158  public function setValid($key) {
159  $this->valid_keys[$key] = true;
160  // $this->valid_keys[$key] = time();
161  }
162 
163 
167  public function getComponent() {
168  return $this->component;
169  }
170 
171 
175  public function setComponent($component) {
176  $this->component = $component;
177  }
178 
179 
183  public function setInvalid($key = NULL) {
184  if ($key) {
185  unset($this->valid_keys[$key]);
186  } else {
187  unset($this->valid_keys);
188  }
189  }
190 
191 
197  public function isValid($key) {
198  return isset($this->valid_keys[$key]);
199  }
200 
201 
205  public function isActive() {
206  return self::$active[get_called_class()];
207  }
208 
209 
213  public function isInstallable() {
214  return self::$installable[get_called_class()];
215  }
216 
217 
223  public function returnKey($key) {
224  return $str = $this->getServiceId() . '_' . $this->getComponent() . '_' . $key;
225 
226  return str_replace('/', '_', $str);
227  }
228 
229 
233  public function getInfo() {
234  return array();
235  }
236 
237 
241  public function getInstallationFailureReason() {
242  if (! $this->getInstallable()) {
243  return 'Not installed';
244  }
245  if (! $this->checkMemory()) {
246  return 'Not enough Cache-Memory, set to at least ' . $this->getMinMemory() . 'M';
247  }
248 
249  return 'Unknown reason';
250  }
251 
252 
256  protected function getMemoryLimit() {
257  return 9999;
258  }
259 
260 
264  protected function getMinMemory() {
265  return 0;
266  }
267 
268 
272  protected function checkMemory() {
273  $matches = array();
274  $memory_limit = $this->getMemoryLimit();
275  if (preg_match('/([0-9]*)([M|K])/uism', $memory_limit, $matches)) {
276  switch ($matches[2]) {
277  case 'M':
278  $memory_limit = $matches[1] * 1024 * 1024; // nnnM -> nnn MB
279  break;
280  case 'K':
281  $memory_limit = $matches[1] * 1024; // nnnK -> nnn KB
282  break;
283  }
284  } else {
285  $memory_limit = $memory_limit * 1024 * 1024; // nnnM -> nnn MB
286  }
287 
288  return ($memory_limit >= $this->getMinMemory() * 1024 * 1024);
289  }
290 
291 
297  abstract public function exists($key);
298 
299 
305  abstract public function delete($key);
306 
307 
311  abstract public function flush();
312 
313 
317  public function setServiceType($service_type) {
318  $this->service_type = $service_type;
319  }
320 
321 
325  public function getServiceType() {
326  return $this->service_type;
327  }
328 }
329 
330 ?>