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 Memcached
 13:  *
 14:  * @author      Twin Huang <twinhuang@qq.com>
 15:  */
 16: class Memcached extends BaseCache
 17: {
 18:     /**
 19:      * The memcached object
 20:      *
 21:      * @var \Memcached
 22:      */
 23:     protected $object;
 24: 
 25:     /**
 26:      * The memcached server configurations
 27:      *
 28:      * @var array
 29:      * @see \Memcached::addServers
 30:      */
 31:     protected $servers = array(
 32:         array(
 33:             'host'          => '127.0.0.1',
 34:             'port'          => 11211,
 35:         )
 36:     );
 37: 
 38:     /**
 39:      * Constructor
 40:      *
 41:      * @param array $options
 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:      * Returns the memcached object, retrieve or store an item
 55:      *
 56:      * @param string $key The name of item
 57:      * @param mixed $value The value of item
 58:      * @param int $expire The expire seconds, defaults to 0, means never expired
 59:      * @return mixed
 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:      * {@inheritdoc}
 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:      * {@inheritdoc}
 84:      */
 85:     public function set($key, $value, $expire = 0)
 86:     {
 87:         return $this->object->set($this->namespace . $key, $value, $expire);
 88:     }
 89: 
 90:     /**
 91:      * {@inheritdoc}
 92:      *
 93:      * Note: setMulti method is not reimplemented for it returning only one
 94:      * "true" or "false" for all items
 95:      *
 96:      * @link http://www.php.net/manual/en/memcached.setmulti.php
 97:      * @link https://github.com/php-memcached-dev/php-memcached/blob/master/php_memcached.c#L1219
 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:      * {@inheritdoc}
112:      */
113:     public function remove($key)
114:     {
115:         return $this->object->delete($this->namespace . $key);
116:     }
117: 
118:     /**
119:      * {@inheritdoc}
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:      * {@inheritdoc}
133:      */
134:     public function add($key, $value, $expire = 0)
135:     {
136:         return $this->object->add($this->namespace . $key, $value, $expire);
137:     }
138: 
139:     /**
140:      * {@inheritdoc}
141:      */
142:     public function replace($key, $value, $expire = 0)
143:     {
144:         return $this->object->replace($this->namespace . $key, $value, $expire);
145:     }
146: 
147:     /**
148:      * {@inheritdoc}
149:      */
150:     public function incr($key, $offset = 1)
151:     {
152:         return $this->incDec($key, $offset, $offset > 0);
153:     }
154: 
155:     /**
156:      * {@inheritdoc}
157:      */
158:     public function decr($key, $offset = 1)
159:     {
160:         return $this->incDec($key, $offset, $offset < 0);
161:     }
162: 
163:     /**
164:      * Increment/Decrement an item
165:      *
166:      * Memcached do not allow negative number as $offset parameter
167:      *
168:      * @link https://github.com/php-memcached-dev/php-memcached/blob/master/php_memcached.c#L1746
169:      * @param string $key The name of item
170:      * @param int $offset The value to be increased/decreased
171:      * @param bool $inc The operation is increase or decrease
172:      * @return int|false Returns the new value on success, or false on failure
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:      * {@inheritdoc}
187:      */
188:     public function clear()
189:     {
190:         return $this->object->flush();
191:     }
192: 
193:     /**
194:      * Get memcached object
195:      *
196:      * @return \Memcached
197:      */
198:     public function getObject()
199:     {
200:         return $this->object;
201:     }
202: 
203:     /**
204:      * Set memcached object
205:      *
206:      * @param \Memcached $object
207:      * @return $this
208:      */
209:     public function setObject(\Memcached $object)
210:     {
211:         $this->object = $object;
212:         return $this;
213:     }
214: }
215: 
Wei Framework API documentation generated by ApiGen