1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace Wei;
10:
11: 12: 13: 14: 15: 16: 17:
18: class Bicache extends BaseCache
19: {
20: 21: 22:
23: protected $providers = array(
24: 'master' => 'apc',
25: 'slave' => 'fileCache',
26: );
27:
28: 29: 30: 31: 32:
33: protected $time = 5;
34:
35: 36: 37:
38: public function get($key, $expire = null, $fn = null)
39: {
40: $key = $this->namespace . $key;
41: $result = $this->master->get($key);
42: if (false === $result) {
43: $result = $this->slave->get($key);
44: }
45: return $this->processGetResult($key, $result, $expire, $fn);
46: }
47:
48: 49: 50:
51: public function set($key, $value, $expire = 0)
52: {
53: $key = $this->namespace . $key;
54: $result = $this->master->set($key, $value, $expire);
55:
56: if ($result && $this->needUpdate($key)) {
57:
58: return $this->slave->set($key, $value, $expire);
59: }
60:
61:
62: return $result;
63: }
64:
65: 66: 67:
68: public function remove($key)
69: {
70: $key = $this->namespace . $key;
71: $result1 = $this->master->remove($key);
72: $result2 = $this->slave->remove($key);
73:
74: return $result1 && $result2;
75: }
76:
77: 78: 79:
80: public function exists($key)
81: {
82: $key = $this->namespace . $key;
83: return $this->master->exists($key) || $this->slave->exists($key);
84: }
85:
86: 87: 88:
89: public function add($key, $value, $expire = 0)
90: {
91: $key = $this->namespace . $key;
92: $result = $this->master->add($key, $value, $expire);
93:
94:
95: if ($result) {
96:
97: return $this->slave->set($key, $value, $expire);
98: }
99:
100:
101: return $result;
102: }
103:
104: 105: 106:
107: public function replace($key, $value, $expire = 0)
108: {
109: $key = $this->namespace . $key;
110: $result = $this->master->replace($key, $value, $expire);
111:
112:
113: if ($result && $this->needUpdate($key)) {
114: return $this->slave->set($key, $value, $expire);
115: }
116:
117: return $result;
118: }
119:
120: 121: 122:
123: public function incr($key, $offset = 1)
124: {
125: $key = $this->namespace . $key;
126: $result = $this->master->incr($key, $offset);
127:
128: if (false !== $result && $this->needUpdate($key)) {
129: return $this->slave->set($key, $result) ? $result : false;
130: }
131:
132: return $result;
133: }
134:
135: 136: 137:
138: public function clear()
139: {
140: $result1 = $this->master->clear();
141: $result2 = $this->slave->clear();
142: return $result1 && $result2;
143: }
144:
145: 146: 147: 148: 149: 150:
151: protected function needUpdate($key)
152: {
153: return $this->master->add('__' . $key, true, $this->time);
154: }
155: }
156: