- /************************************************ **********************************
- * InitPHP 2.0 Domestic PHP development framework Dao-Nosql-Redis
- *------------------------------------------------ -------------------------------
- * Copyright: CopyRight By initphp.com
- * You can use the source code freely, but During use, please retain the author information.尊重他人劳动成果就是尊重自己
- *-------------------------------------------------------------------------------
- * $Author:zhuli
- * $Dtime:2011-10-09
- ***********************************************************************************/
- class redisInit {
-
- private $redis; //redis对象
-
- /**
- * Initialize Redis
- * $config = array(
- * 'server' => '127.0.0.1' server
- * 'port' => '6379' port number
- * )
- * @param array $config
- */
- public function init($config = array()) {
- if ($config['server'] == '') $config['server'] = '127.0.0.1';
- if ($config['port'] == '') $config['port'] = '6379';
- $this->redis = new Redis();
- $this->redis->connect($config['server'], $config['port']);
- return $this->redis;
- }
-
- /**
- * Set value
- * @param string $key KEY name
- * @param string|array $value Get the data
- * @param int $timeOut time
- */
- public function set($key, $value, $timeOut = 0) {
- $value = json_encode($value, TRUE);
- $retRes = $this->redis->set($key, $value);
- if ($timeOut > 0) $this->redis->setTimeout($key, $timeOut);
- return $retRes;
- }
-
- /**
- * Get data through KEY
- * @param string $key KEY name
- */
- public function get($key) {
- $result = $this->redis->get($key);
- return json_decode($result, TRUE);
- }
-
- /**
- * Delete a piece of data
- * @param string $key KEY name
- */
- public function delete($key) {
- return $this->redis->delete($key);
- }
-
- /**
- * Clear data
- */
- public function flushAll() {
- return $this->redis->flushAll();
- }
-
- /**
- * Data is put into the queue
- * @param string $key KEY name
- * @param string|array $value Get the obtained data
- * @param bool $right Whether to start from the right
- */
- public function push($key, $value ,$right = true) {
- $value = json_encode($value);
- return $right ? $this->redis->rPush($key, $value) : $this->redis->lPush($key, $value);
- }
-
- /**
- * Dequeue data
- * @param string $key KEY name
- * @param bool $left Whether to start dequeuing data from the left
- */
- public function pop($key , $left = true) {
- $val = $left ? $this->redis->lPop($key) : $this->redis->rPop($key);
- return json_decode($val);
- }
-
- /**
- * Data auto-increment
- * @param string $key KEY name
- */
- public function increment($key) {
- return $this->redis->incr($key);
- }
-
- /**
- * Data decrement
- * @param string $key KEY name
- */
- public function decrement($key) {
- return $this->redis->decr($key);
- }
-
- /**
- * Whether the key exists, true is returned if it exists
- * @param string $key KEY name
- */
- public function exists($key) {
- return $this->redis->exists($key);
- }
-
- /**
- * Return the redis object
- * Redis has many operation methods, we only encapsulate some of them
- * With this object, you can directly call redis's own methods
- */
- public function redis() {
- return $this->redis;
- }
- }
复制代码
|