Overview

Namespaces

  • None
  • PHP
  • Wei
    • Validator

Classes

  • Apc
  • App
  • ArrayCache
  • Asset
  • Base
  • BaseCache
  • Bicache
  • Cache
  • Config
  • Cookie
  • Couchbase
  • Counter
  • Db
  • DbCache
  • E
  • Env
  • Error
  • FileCache
  • Gravatar
  • Http
  • Lock
  • Logger
  • Memcache
  • Memcached
  • MongoCache
  • Password
  • PhpError
  • Pinyin
  • Record
  • Redis
  • Request
  • Response
  • Router
  • SafeUrl
  • Session
  • Soap
  • T
  • Ua
  • Upload
  • Url
  • Uuid
  • Validate
  • View
  • WeChatApp
  • Wei
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Wei Framework
  4:  *
  5:  * @copyright   Copyright (c) 2008-2013 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 Memcache
 13:  *
 14:  * @author      Twin Huang <twinhuang@qq.com>
 15:  */
 16: class Memcache extends BaseCache
 17: {
 18:     /**
 19:      * The memcache object
 20:      *
 21:      * @var \Memcache
 22:      */
 23:     protected $object;
 24: 
 25:     /**
 26:      * The memcache server configurations
 27:      *
 28:      * @var array
 29:      * @see \Memcache::addServer
 30:      */
 31:     protected $servers = array(
 32:         array(
 33:             'host'          => '127.0.0.1',
 34:             'port'          => 11211,
 35:             'persistent'    => true
 36:         )
 37:     );
 38: 
 39:     /**
 40:      * The flag that use MEMCACHE_COMPRESSED to store the item compressed (uses zlib).
 41:      *
 42:      * @var int
 43:      */
 44:     protected $flag = 0;
 45: 
 46:     /**
 47:      * Constructor
 48:      *
 49:      * @param array $options
 50:      */
 51:     public function __construct(array $options = array())
 52:     {
 53:         parent::__construct($options);
 54:         $this->connect();
 55:     }
 56: 
 57:     /**
 58:      * Instance memcache object and connect to server
 59:      */
 60:     protected function connect()
 61:     {
 62:         if (!$this->object) {
 63:             $this->object = new \Memcache;
 64:         }
 65:         foreach ($this->servers as $server) {
 66:             call_user_func_array(array($this->object, 'addServer'), $server);
 67:         }
 68:     }
 69: 
 70:     /**
 71:      * Returns the memcache object, retrieve or store an item
 72:      *
 73:      * @param string $key The name of item
 74:      * @param mixed $value The value of item
 75:      * @param int $expire The expire seconds, defaults to 0, means never expired
 76:      * @return mixed
 77:      */
 78:     public function __invoke($key = null, $value = null, $expire = 0)
 79:     {
 80:         switch (func_num_args()) {
 81:             case 0:
 82:                 return $this->object;
 83:             case 1:
 84:                 return $this->get($key);
 85:             default:
 86:                 return $this->set($key, $value, $expire);
 87:         }
 88:     }
 89: 
 90:     /**
 91:      * {@inheritdoc}
 92:      */
 93:     public function get($key, $expire = null, $fn = null)
 94:     {
 95:         $result = $this->object->get($this->prefix . $key);
 96:         return $this->processGetResult($key, $result, $expire, $fn);
 97:     }
 98: 
 99:     /**
100:      * {@inheritdoc}
101:      */
102:     public function set($key, $value, $expire = 0)
103:     {
104:         return $this->object->set($this->prefix . $key, $value, $this->flag, $expire);
105:     }
106: 
107:     /**
108:      * {@inheritdoc}
109:      */
110:     public function remove($key)
111:     {
112:         return $this->object->delete($this->prefix . $key);
113:     }
114: 
115:     /**
116:      * {@inheritdoc}
117:      */
118:     public function exists($key)
119:     {
120:         $key = $this->prefix . $key;
121:         if ($this->object->add($key, true)) {
122:             $this->object->delete($key);
123:             return false;
124:         }
125:         return true;
126:     }
127: 
128:     /**
129:      * {@inheritdoc}
130:      */
131:     public function add($key, $value, $expire = 0)
132:     {
133:         return $this->object->add($this->prefix . $key, $value, $this->flag, $expire);
134:     }
135: 
136:     /**
137:      * {@inheritdoc}
138:      */
139:     public function replace($key, $value, $expire = 0)
140:     {
141:         return $this->object->replace($this->prefix . $key, $value, $this->flag, $expire);
142:     }
143: 
144:     /**
145:      * {@inheritdoc}
146:      */
147:     public function incr($key, $offset = 1)
148:     {
149:         return $this->incDec($key, $offset, $offset > 0);
150:     }
151: 
152:     /**
153:      * {@inheritdoc}
154:      */
155:     public function decr($key, $offset = 1)
156:     {
157:         return $this->incDec($key, $offset, $offset < 0);
158:     }
159: 
160:     /**
161:      * Increment/Decrement an item
162:      *
163:      * Compatible method for memcache < 3.0.3 that does not support
164:      * negative number as $offset parameter
165:      *
166:      * @param string $key The name of item
167:      * @param int $offset The value to be increased/decreased
168:      * @param bool $inc The operation is increase or decrease
169:      * @return int|false Returns the new value on success, or false on failure
170:      */
171:     protected function incDec($key, $offset, $inc = true)
172:     {
173:         $key = $this->prefix . $key;
174:         $method = $inc ? 'increment' : 'decrement';
175:         $offset = abs($offset);
176:         // IMPORTANT: memcache may return 0 in some 3.0.x beta version
177:         if (false === $this->object->$method($key, $offset)) {
178:             return $this->object->set($key, $offset) ? $offset : false;
179:         }
180:         // Convert to int for memcache extension version < 3.0.3
181:         return (int)$this->object->get($key);
182:     }
183: 
184:     /**
185:      * {@inheritdoc}
186:      */
187:     public function clear()
188:     {
189:         return $this->object->flush();
190:     }
191: 
192:     /**
193:      * Get memcache object
194:      *
195:      * @return \Memcache
196:      */
197:     public function getObject()
198:     {
199:         return $this->object;
200:     }
201: 
202:     /**
203:      * Set memcache object
204:      *
205:      * @param \Memcache $object
206:      * @return $this
207:      */
208:     public function setObject(\Memcache $object)
209:     {
210:         $this->object = $object;
211:         return $this;
212:     }
213: }
214: 
Wei Framework API documentation generated by ApiGen