/
Adapter.php
103 lines (89 loc) · 2.38 KB
/
Adapter.php
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
99
100
101
102
103
<?php
class EventCacheAdapter {
protected $_config = array(
);
protected $_flushSafe = true;
public function __construct ($options = array()) {
$this->_config = array_merge($this->_config, $options);
}
public function init () {
return true;
}
public function isFlushSafe () {
return $this->_flushSafe;
}
public function get ($key) {
trigger_error(sprintf('%s::%s needs to be implemented', __CLASS__, __FUNCTION__), E_USER_ERROR);
}
public function set ($key, $val, $ttl = 0) {
trigger_error(sprintf('%s::%s needs to be implemented', __CLASS__, __FUNCTION__), E_USER_ERROR);
}
public function delete ($key) {
trigger_error(sprintf('%s::%s needs to be implemented', __CLASS__, __FUNCTION__), E_USER_ERROR);
}
public function flush () {
trigger_error(sprintf('%s::%s needs to be implemented', __CLASS__, __FUNCTION__), E_USER_ERROR);
}
public function increment ($key, $val = 1) {
if ($this->set($key, ($new = (float)$this->get($key) + $val))) {
return $new;
}
return false;
}
public function decrement ($key, $val = 1) {
if ($this->set($key, ($new = (float)$this->get($key) - $val))) {
return $new;
}
return false;
}
/**
* Add remove element from an array in cache
*
* @param <type> $ulistKey
* @param <type> $safeKey
* @param <type> $ttl
*
* @return <type>
*/
public function ulistDelete ($ulistKey, $safeKey) {
$ulist = $this->get($ulistKey);
if (is_array($ulist) && array_key_exists($safeKey, $ulist)) {
unset($ulist[$safeKey]);
return $this->set($ulistKey, $ulist);
}
// Didn't have to remove non-existing key
return null;
}
/**
* Adds item to a unique list (associative array)
*
* @param <type> $ulistKey
* @param <type> $safeKey leave null to add item at the end of numerically indexed array
* @param <type> $val
* @param <type> $ttl
*
* @return mixed boolean or null
*/
public function ulistSet ($ulistKey, $safeKey, $val) {
$ulist = $this->get($ulistKey);
if (empty($ulist)) {
$ulist = array();
}
$ulist[$safeKey] = $val;
return $this->set($ulistKey, $ulist);
}
public function getUlist ($ulistKey) {
return $this->get($ulistKey);
}
public function listAdd ($listKey, $val = null) {
$list = $this->get($listKey);
if (empty($list)) {
$list = array();
}
$list[] = $val;
return $this->set($listKey, $list);
}
public function getList ($listKey) {
return $this->get($listKey);
}
}