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