1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace Wei;
10:
11: 12: 13: 14: 15:
16: class Redis extends BaseCache
17: {
18: 19: 20: 21: 22:
23: protected $object;
24:
25: 26: 27: 28: 29:
30: protected $host = '127.0.0.1';
31:
32: 33: 34: 35: 36:
37: protected $port = 6379;
38:
39: 40: 41: 42: 43:
44: protected $timeout = 0.0;
45:
46: 47: 48: 49: 50:
51: protected $persistent = true;
52:
53: 54: 55: 56: 57:
58: protected $auth;
59:
60: 61: 62: 63: 64:
65: protected $options = array(
66: \Redis::OPT_SERIALIZER => \Redis::SERIALIZER_PHP
67: );
68:
69: 70: 71: 72: 73:
74: public function __construct(array $options = array())
75: {
76: parent::__construct($options);
77: $this->connect();
78: }
79:
80: 81: 82: 83: 84: 85: 86: 87:
88: public function __invoke($key = null, $value = null, $expire = 0)
89: {
90: switch (func_num_args()) {
91: case 0:
92: return $this->object;
93: case 1:
94: return $this->get($key);
95: default:
96: return $this->set($key, $value, $expire);
97: }
98: }
99:
100: 101: 102: 103: 104:
105: public function connect()
106: {
107: if ($this->object) {
108: return true;
109: }
110:
111: $this->object = new \Redis;
112: $connect = $this->persistent ? 'pconnect' : 'connect';
113: $result = $this->object->$connect($this->host, $this->port, $this->timeout);
114:
115: if ($result && $this->auth) {
116: $result = $this->object->auth($this->auth);
117: }
118:
119: if ($result) {
120: foreach ($this->options as $key => $value) {
121: $this->object->setOption($key, $value);
122: }
123: }
124:
125: return $result;
126: }
127:
128: 129: 130:
131: public function get($key, $expire = null, $fn = null)
132: {
133: $result = $this->object->get($this->namespace . $key);
134: return $this->processGetResult($key, $result, $expire, $fn);
135: }
136:
137: 138: 139:
140: public function set($key, $value, $expire = 0)
141: {
142: return $this->object->set($this->namespace . $key, $value, $expire);
143: }
144:
145: 146: 147:
148: public function getMulti(array $keys)
149: {
150: $keysWithPrefix = array();
151: foreach ($keys as $key) {
152: $keysWithPrefix[] = $this->namespace . $key;
153: }
154: return array_combine($keys, $this->object->mGet($keysWithPrefix));
155: }
156:
157: 158: 159: 160: 161: 162: 163: 164: 165: 166:
167: public function setMulti(array $items, $expire = 0)
168: {
169: $keys = array_keys($items);
170: $keysWithPrefix = array();
171: foreach ($keys as $key) {
172: $keysWithPrefix[] = $this->namespace . $key;
173: }
174: $items = array_combine($keysWithPrefix, $items);
175: $result = $this->object->mset($items);
176: return array_combine($keys, array_pad(array(), count($items), $result));
177: }
178:
179: 180: 181:
182: public function remove($key)
183: {
184: return (bool)$this->object->del($this->namespace . $key);
185: }
186:
187: 188: 189:
190: public function exists($key)
191: {
192: return $this->object->exists($this->namespace . $key);
193: }
194:
195: 196: 197:
198: public function add($key, $value, $expire = 0)
199: {
200: $key = $this->namespace . $key;
201: $result = $this->object->setnx($key, $value);
202: if (true === $result) {
203: $this->object->expire($key, $expire === 0 ? -1 : $expire);
204: }
205: return $result;
206: }
207:
208: 209: 210: 211: 212:
213: public function replace($key, $value, $expire = 0)
214: {
215: if (false === $this->object->get($this->namespace . $key)) {
216: return false;
217: }
218: return $this->object->set($this->namespace . $key, $value, $expire);
219: }
220:
221: 222: 223:
224: public function incr($key, $offset = 1)
225: {
226: return $this->object->incrBy($this->namespace . $key, $offset);
227: }
228:
229: 230: 231:
232: public function clear()
233: {
234: return $this->object->flushAll();
235: }
236:
237: 238: 239: 240: 241:
242: public function getObject()
243: {
244: return $this->object;
245: }
246:
247: 248: 249: 250: 251: 252:
253: public function setObject(\Redis $object)
254: {
255: $this->object = $object;
256: return $this;
257: }
258: }
259: