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