75 lines
1.3 KiB
PHP
Executable File
75 lines
1.3 KiB
PHP
Executable File
<?php
|
|
|
|
class MemcachedTimer extends ArrayTimer {
|
|
|
|
private $prefix = '', $cache = null, $period = 60, $min = 0;
|
|
|
|
public function setPrefix ( $prefix ) {
|
|
|
|
$this->cache = new Memcached();
|
|
$this->prefix = $prefix;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
public function sleepSkip ( $id ) {
|
|
|
|
$skip = !$this->sleepTime($id);
|
|
|
|
if ( $skip )
|
|
$this->update($id);
|
|
|
|
return $skip;
|
|
|
|
}
|
|
|
|
public function offsetGet ( $id ) {
|
|
|
|
$value = $this->memcached->get($this->prefix . $id);
|
|
|
|
if ( $value === false && $this->memcached->getResultCode() === Memcached::RES_NOTFOUND )
|
|
$value = $this[$id] = time() - $this->period;
|
|
|
|
parent::offsetSet($value);
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
public function offsetSet ( $id, $value ) {
|
|
|
|
if ( $this->memcached->set($this->prefix . $id, $value) ) {
|
|
|
|
parent::offsetSet($id, $value);
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
throw new Exception('Memcached error ! Cannot set value !');
|
|
|
|
}
|
|
|
|
public function offsetUnset ( $id ) {
|
|
|
|
parent::offsetUnset($id);
|
|
|
|
$this->memcached->delete($this->prefix . $id);
|
|
|
|
}
|
|
|
|
public function offsetExists ( $id ) {
|
|
|
|
$value = $this->memcached->get($this->prefix . $id);
|
|
|
|
if ( $value === false && $this->memcached->getResultCode() === Memcached::RES_NOTFOUND )
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|