Overview

Namespaces

  • None
  • Wei
    • Validator

Classes

  • Wei\Validator\All
  • Wei\Validator\AllOf
  • Wei\Validator\Alnum
  • Wei\Validator\Alpha
  • Wei\Validator\BaseValidator
  • Wei\Validator\Between
  • Wei\Validator\Blank
  • Wei\Validator\Callback
  • Wei\Validator\CharLength
  • Wei\Validator\Chinese
  • Wei\Validator\Color
  • Wei\Validator\Contains
  • Wei\Validator\CreditCard
  • Wei\Validator\Date
  • Wei\Validator\DateTime
  • Wei\Validator\Decimal
  • Wei\Validator\Digit
  • Wei\Validator\Dir
  • Wei\Validator\DivisibleBy
  • Wei\Validator\DoubleByte
  • Wei\Validator\Email
  • Wei\Validator\EndsWith
  • Wei\Validator\EqualTo
  • Wei\Validator\Exists
  • Wei\Validator\FieldExists
  • Wei\Validator\File
  • Wei\Validator\GreaterThan
  • Wei\Validator\GreaterThanOrEqual
  • Wei\Validator\IdCardCn
  • Wei\Validator\IdCardHk
  • Wei\Validator\IdCardMo
  • Wei\Validator\IdCardTw
  • Wei\Validator\IdenticalTo
  • Wei\Validator\Image
  • Wei\Validator\In
  • Wei\Validator\Ip
  • Wei\Validator\Length
  • Wei\Validator\LessThan
  • Wei\Validator\LessThanOrEqual
  • Wei\Validator\Lowercase
  • Wei\Validator\Luhn
  • Wei\Validator\MaxLength
  • Wei\Validator\MinLength
  • Wei\Validator\MobileCn
  • Wei\Validator\NaturalNumber
  • Wei\Validator\NoneOf
  • Wei\Validator\Null
  • Wei\Validator\Number
  • Wei\Validator\OneOf
  • Wei\Validator\Password
  • Wei\Validator\Phone
  • Wei\Validator\PhoneCn
  • Wei\Validator\PlateNumberCn
  • Wei\Validator\PositiveInteger
  • Wei\Validator\PostcodeCn
  • Wei\Validator\Present
  • Wei\Validator\QQ
  • Wei\Validator\RecordExists
  • Wei\Validator\Regex
  • Wei\Validator\Required
  • Wei\Validator\SomeOf
  • Wei\Validator\StartsWith
  • Wei\Validator\Time
  • Wei\Validator\Tld
  • Wei\Validator\Type
  • Wei\Validator\Uppercase
  • Wei\Validator\Url
  • Wei\Validator\Uuid
  • Overview
  • Namespace
  • Function
  1: <?php
  2: /**
  3:  * Wei Framework
  4:  *
  5:  * @copyright   Copyright (c) 2008-2015 Twin Huang
  6:  * @license     http://opensource.org/licenses/mit-license.php MIT License
  7:  */
  8: 
  9: namespace Wei;
 10: 
 11: /**
 12:  * A cache service that stored data in Redis
 13:  *
 14:  * @author      Twin Huang <twinhuang@qq.com>
 15:  */
 16: class Redis extends BaseCache
 17: {
 18:     /**
 19:      * The redis object
 20:      *
 21:      * @var \Redis
 22:      */
 23:     protected $object;
 24: 
 25:     /**
 26:      * The host, or the path to a unix domain socket
 27:      *
 28:      * @var string
 29:      */
 30:     protected $host = '127.0.0.1';
 31: 
 32:     /**
 33:      * The port of the host
 34:      *
 35:      * @var int
 36:      */
 37:     protected $port = 6379;
 38: 
 39:     /**
 40:      * The timeout seconds
 41:      *
 42:      * @var float|int
 43:      */
 44:     protected $timeout = 0.0;
 45: 
 46:     /**
 47:      * Whether persistent connection or not
 48:      *
 49:      * @var bool
 50:      */
 51:     protected $persistent = true;
 52: 
 53:     /**
 54:      * A password to authenticate the connection
 55:      *
 56:      * @var string
 57:      */
 58:     protected $auth;
 59: 
 60:     /**
 61:      * The options for \Redis::setOptions()
 62:      *
 63:      * @var array
 64:      */
 65:     protected $options = array(
 66:         \Redis::OPT_SERIALIZER => \Redis::SERIALIZER_PHP
 67:     );
 68: 
 69:     /**
 70:      * Constructor
 71:      *
 72:      * @param array $options
 73:      */
 74:     public function __construct(array $options = array())
 75:     {
 76:         parent::__construct($options);
 77:         $this->connect();
 78:     }
 79: 
 80:     /**
 81:      * Returns the redis object, retrieve or store an item
 82:      *
 83:      * @param string $key The name of item
 84:      * @param mixed $value The value of item
 85:      * @param int $expire The expire seconds, defaults to 0, means never expired
 86:      * @return mixed
 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:      * Connect the redis server and set redis options
102:      *
103:      * @return bool true on success, false on error
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:      * {@inheritdoc}
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:      * {@inheritdoc}
139:      */
140:     public function set($key, $value, $expire = 0)
141:     {
142:         return $this->object->set($this->namespace . $key, $value, $expire);
143:     }
144: 
145:     /**
146:      * {@inheritdoc}
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:      * {@inheritdoc}
159:      *
160:      * Note:
161:      * 1. The "$expire" parameter is not support by redis MSET command
162:      * 2. The elements in returning values are all true or false, see links for more detail
163:      *
164:      * @link http://redis.io/commands/mset
165:      * @link https://github.com/nicolasff/phpredis/blob/master/redis_array.c#L844
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:      * {@inheritdoc}
181:      */
182:     public function remove($key)
183:     {
184:         return (bool)$this->object->del($this->namespace . $key);
185:     }
186: 
187:     /**
188:      * {@inheritdoc}
189:      */
190:     public function exists($key)
191:     {
192:         return $this->object->exists($this->namespace . $key);
193:     }
194: 
195:     /**
196:      * {@inheritdoc}
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:      * Note: This method is not an atomic operation
210:      *
211:      * {@inheritdoc}
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:      * {@inheritdoc}
223:      */
224:     public function incr($key, $offset = 1)
225:     {
226:         return $this->object->incrBy($this->namespace . $key, $offset);
227:     }
228: 
229:     /**
230:      * {@inheritdoc}
231:      */
232:     public function clear()
233:     {
234:         return $this->object->flushAll();
235:     }
236: 
237:     /**
238:      * Get the redis object
239:      *
240:      * @return \Redis
241:      */
242:     public function getObject()
243:     {
244:         return $this->object;
245:     }
246: 
247:     /**
248:      * Set the redis object
249:      *
250:      * @param \Redis $object
251:      * @return $this
252:      */
253:     public function setObject(\Redis $object)
254:     {
255:         $this->object = $object;
256:         return $this;
257:     }
258: }
259: 
Wei Framework API documentation generated by ApiGen