source: spip-zone/_plugins_/memoization/memo/eaccelerator.inc @ 32462

Last change on this file since 32462 was 32462, checked in by fil@…, 11 years ago

nettoyage de l'API et appels moins cryptiques

File size: 1.4 KB
Line 
1<?php
2
3/*
4mixed cache_get(string key)
5bool  cache_set(string key, mixed value [, int ttl])
6bool  cache_isset(string key)
7bool  cache_unset(string key)
8int   cache_inc(string key [, int value [, int ttl]])
9int   cache_dec(string key [, int value [, int ttl]])
10bool  cache_lock(string key)
11bool  cache_unlock(string key)
12*/
13
14# http://bart.eaccelerator.net/doc/phpdoc/eAccelerator/_shared_memory_php.html
15
16@define('_CACHE_NAMESPACE', $_SERVER['HTTP_HOST'].':');
17
18function cache_get($key) {
19        return ($v = eaccelerator_get(_CACHE_NAMESPACE.$key))
20                ? @unserialize($v)
21                : null;
22}
23
24function cache_set($key, $value, $ttl = null) {
25        $value = serialize($value);
26        return is_null($ttl)
27                ? eaccelerator_put(_CACHE_NAMESPACE.$key, $value)
28                : eaccelerator_put(_CACHE_NAMESPACE.$key, $value, $ttl);
29}
30
31function cache_isset($key) {
32        return cache_get($key) !== null;
33}
34
35function cache_unset($key) {
36        return eaccelerator_rm(_CACHE_NAMESPACE.$key);
37}
38
39function cache_inc($key) {
40        while (!eaccelerator_lock($key)){ sleep(1); }
41        $value = 1 + intval(cache_get($key));
42        cache_set($key, $value);
43        eaccelerator_unlock($key);
44        return $value;
45}
46
47function cache_dec($key) {
48        while (!eaccelerator_lock($key)){ sleep(1); }
49        $value = -1 + intval(cache_get($key));
50        cache_set($key, $value);
51        eaccelerator_unlock($key);
52        return $value;
53}
54
55function cache_lock($key) {
56        return eaccelerator_lock(_CACHE_NAMESPACE.$key);
57}
58
59function cache_unlock($key) {
60        return eaccelerator_unlock(_CACHE_NAMESPACE.$key);
61}
62
63?>
Note: See TracBrowser for help on using the repository browser.