1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98:
<?php
namespace Apptus\Util\Cache;
class ArrayStateCache implements StateCache {
private $times = array ();
private $cache = array ();
public function clear() {
$this->times = array ();
$this->cache = array ();
}
private function expireKey($key) {
if (isset($this->times[$key]) && microtime(true) > $this->times[$key]) {
unset($this->times[$key]);
unset($this->cache[$key]);
}
}
private function insert($key, $value, $expire) {
if ($expire !== 0) {
$this->times[$key] = microtime(true) + $expire;
}
$this->cache[$key] = $value;
}
public function add($key, $value, $expire) {
$key = (string) $key;
$value = (string) $value;
$this->expireKey($key);
if (isset($this->cache[$key])) return false;
$this->insert($key, $value, $expire);
return true;
}
public function delete($key) {
$key = (string) $key;
$this->expireKey($key);
if (!isset($this->cache[$key])) return false;
unset($this->times[$key]);
unset($this->cache[$key]);
return true;
}
public function get($key) {
$key = (string) $key;
$this->expireKey($key);
if (isset($this->cache[$key])) return $this->cache[$key];
return false;
}
public function getMulti($keys) {
$result = array ();
foreach ($keys as $key) {
$key = (string) $key;
$r = $this->get($key);
if ($r !== false) {
$result[$key] = $r;
}
}
return $result;
}
public function replace($key, $value, $expire) {
$key = (string) $key;
$value = (string) $value;
$this->expireKey($key);
if (isset($this->cache[$key])) {
$this->insert($key, $value, $expire);
return true;
}
return false;
}
public function set($key, $value, $expire) {
$key = (string) $key;
$value = (string) $value;
$this->insert($key, $value, $expire);
return true;
}
public function setMulti($items, $expire) {
foreach ($items as $key => $value) {
$key = (string) $key;
$value = (string) $value;
$this->insert($key, $value, $expire);
}
return true;
}
}