spip_nursit/plugins-dist/dist/nursit/memoization/memo/xcache.inc

121 lines
2.4 KiB
PHP
Raw Normal View History

2023-06-01 15:30:12 +00:00
<?php
/**
* Implementation methode Memoization
*
* @package SPIP\memoization\Memo
**/
// S<>curit<69>
if (!defined('_ECRIRE_INC_VERSION')){
return;
}
# http://xcache.lighttpd.net/wiki/XcacheApi
if (!defined('_CACHE_NAMESPACE')){
define('_CACHE_NAMESPACE', $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . ":");
}
if (!defined('_LOCK_MAX')){
define('_LOCK_MAX', 30);
} # max lock time (seconds)
class MCacheBackend_xcache implements MCacheBackend {
/* Interface */
public function init($params = null){
}
public function get($key){
$a = @xcache_get(_CACHE_NAMESPACE . $key);
// maybe_unserialize
if (is_array($a)){
return @unserialize($a[0]);
} else {
return $a;
}
}
public function set($key, $value, $ttl = null){
// maybe_serialize
if (is_object($value) or is_array($value)){
$value = array(serialize($value));
}
return is_null($ttl)
? xcache_set(_CACHE_NAMESPACE . $key, $value)
: xcache_set(_CACHE_NAMESPACE . $key, $value, $ttl);
}
public function exists($key){
return xcache_isset(_CACHE_NAMESPACE . $key);
}
public function del($key){
return xcache_unset(_CACHE_NAMESPACE . $key);
}
public function inc($key, $value = null, $ttl = null){
$a = func_get_args();
$a[0] = _CACHE_NAMESPACE . $key;
return call_user_func_array('xcache_inc', $a);
}
public function dec($key, $value = null, $ttl = null){
$a = func_get_args();
$a[0] = _CACHE_NAMESPACE . $key;
return call_user_func_array('xcache_dec', $a);
}
public function lock($key, /* private */ $unlock = false){
static $locks = array();
/* unlock */
if ($unlock){
unset($locks[$key]);
$this->del('lock::' . $key);
} /* lock */
else {
# si on l'a deja, ok
if (isset($locks[$key])){
return true;
}
# sinon s'il existe, on attend qu'il se libere
$wait = 0;
while ($this->exists('lock::' . $key)){
if ($wait++<_LOCK_MAX){
sleep(1);
} else {
return false;
}
}
# ... puis on le pose
if ($this->inc('lock::' . $key, 1, _LOCK_MAX)<=1){
$locks[$key] = true;
register_shutdown_function(array($this, 'unlock'), $key);
return true;
}
return false;
}
}
public function unlock($key){
$this->lock($key, true);
}
public function size(){
# necessite xcache.admin.user
#$a = xcache_list();
#var_dump($a);
return false;
}
public function purge(){
return false;
}
}