1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace Wei;
10:
11: 12: 13: 14: 15:
16: class Memcache extends BaseCache
17: {
18: 19: 20: 21: 22:
23: protected $object;
24:
25: 26: 27: 28: 29: 30:
31: protected $servers = array(
32: array(
33: 'host' => '127.0.0.1',
34: 'port' => 11211,
35: 'persistent' => true
36: )
37: );
38:
39: 40: 41: 42: 43:
44: protected $flag = 0;
45:
46: 47: 48: 49: 50:
51: public function __construct(array $options = array())
52: {
53: parent::__construct($options);
54: $this->connect();
55: }
56:
57: 58: 59:
60: protected function connect()
61: {
62: if (!$this->object) {
63: $this->object = new \Memcache;
64: }
65: foreach ($this->servers as $server) {
66: call_user_func_array(array($this->object, 'addServer'), $server);
67: }
68: }
69:
70: 71: 72: 73: 74: 75: 76: 77:
78: public function __invoke($key = null, $value = null, $expire = 0)
79: {
80: switch (func_num_args()) {
81: case 0:
82: return $this->object;
83: case 1:
84: return $this->get($key);
85: default:
86: return $this->set($key, $value, $expire);
87: }
88: }
89:
90: 91: 92:
93: public function get($key, $expire = null, $fn = null)
94: {
95: $result = $this->object->get($this->prefix . $key);
96: return $this->processGetResult($key, $result, $expire, $fn);
97: }
98:
99: 100: 101:
102: public function set($key, $value, $expire = 0)
103: {
104: return $this->object->set($this->prefix . $key, $value, $this->flag, $expire);
105: }
106:
107: 108: 109:
110: public function remove($key)
111: {
112: return $this->object->delete($this->prefix . $key);
113: }
114:
115: 116: 117:
118: public function exists($key)
119: {
120: $key = $this->prefix . $key;
121: if ($this->object->add($key, true)) {
122: $this->object->delete($key);
123: return false;
124: }
125: return true;
126: }
127:
128: 129: 130:
131: public function add($key, $value, $expire = 0)
132: {
133: return $this->object->add($this->prefix . $key, $value, $this->flag, $expire);
134: }
135:
136: 137: 138:
139: public function replace($key, $value, $expire = 0)
140: {
141: return $this->object->replace($this->prefix . $key, $value, $this->flag, $expire);
142: }
143:
144: 145: 146:
147: public function incr($key, $offset = 1)
148: {
149: return $this->incDec($key, $offset, $offset > 0);
150: }
151:
152: 153: 154:
155: public function decr($key, $offset = 1)
156: {
157: return $this->incDec($key, $offset, $offset < 0);
158: }
159:
160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170:
171: protected function incDec($key, $offset, $inc = true)
172: {
173: $key = $this->prefix . $key;
174: $method = $inc ? 'increment' : 'decrement';
175: $offset = abs($offset);
176:
177: if (false === $this->object->$method($key, $offset)) {
178: return $this->object->set($key, $offset) ? $offset : false;
179: }
180:
181: return (int)$this->object->get($key);
182: }
183:
184: 185: 186:
187: public function clear()
188: {
189: return $this->object->flush();
190: }
191:
192: 193: 194: 195: 196:
197: public function getObject()
198: {
199: return $this->object;
200: }
201:
202: 203: 204: 205: 206: 207:
208: public function setObject(\Memcache $object)
209: {
210: $this->object = $object;
211: return $this;
212: }
213: }
214: