4-22 4,411 views
<?php
/**
* Predis缓存驱动
* 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
@date 2016-06-15
@edit 2019-08-01
*/
namespace App\Common\Lib\Redis;
class PredisService{
//predis对象
private $handler;
//redis服务器状态默认开始
// private $redis_status = 1;
// 存储对象
private static $moduleObj = array();
public function __construct() {
$this->handler = self::getPredisObj();
}
/**
* [get_servers 获得redis服务器配置]
* @return [type] [description]
*/
public static function get_servers(){
$redis_conf = C('MSG');
$options = array (
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
// 'password' => '123456',
);
return $options;
}
/**
* 获取predis 单例
* @return object
*/
public static function getPredisObj(){
if (empty(self::$moduleObj['predisObj']) || is_null(self::$moduleObj['predisObj'])){
self::$moduleObj['predisObj'] = new \Predis\Client(self::get_servers());;
}
return self::$moduleObj['predisObj'];
}
/**
* 读取缓存
* @access public
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name) {
$value = $this->handler->get($name);
$jsonData = json_decode( $value, true );
return ($jsonData === NULL) ? $value : $jsonData; //检测是否为JSON数据 true 返回JSON解析数组, false返回源数据
}
/**
* 写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param integer $expire 有效时间(秒)
* @return boolean
*/
public function set($name, $value, $expire = null) {
//对数组/对象数据进行缓存处理,保证数据完整性
$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
if(is_int($expire) && $expire) {
$result = $this->handler->setex($name, $expire, $value);
}else{
$result = $this->handler->set($name, $value);
}
return $result;
}
/**
* 删除缓存
* @access public
* @param string $name 缓存变量名
* @return boolean
*/
public function del($name) {
return $this->handler->del($name);
}
/**
* 检查key值是否存在
* @access public
* @param string $name 缓存变量名
* @return boolean
*/
public function exists($name) {
return $this->handler->exists($name);
}
public function hset($hash_key = '',$key = '',$val = ''){
return $this->handler->hset($hash_key,$key,$val);
}
public function hget($hash_key = '',$key = ''){
return $this->handler->hget($hash_key,$key);
}
public function hdel($hash_key = '',$key = ''){
return $this->handler->hdel($hash_key,$key);
}
//hmset/hmget 存取多个元素到hash表
//$redis -> hmset ( 'hash1' , array ( 'key3' => 'v3' , 'key4' => 'v4' ) ) ;
//$redis -> hmget ( 'hash1' , array ( 'key3' , 'key4' ) ) ; //返回相应的值 array('v3','v4')
public function hmset($hash_key = '', $array = []){
return $this->handler->hmset($hash_key, $array);
}
//hsetnx 增加一个元素,但不能重复
public function hsetnx($hash_key = '', $key , $val = ''){
return $this->handler->hsetnx($hash_key, $key, $val );
}
//hash mget
public function hmget($hash_key = '', $array = []){
return $this->handler->hmget($hash_key, $array);
}
//hgetall 返回整个hash表元素
public function hgetall($hash_key = ''){
return $this->handler->hgetall($hash_key);
}
//hash exist
public function hexists($hash_key = '',$key = ''){
return $this->handler->hexists($hash_key, $key);
}
/**
* [get_status 获得redis服务器状态]
* @return [type] [description]
*/
// public function get_redis_status(){
// return $this->redis_status;
// }
/**
* [set_status 设置redis服务器方法]
*/
public function set_status(){
$this->redis_status = 0;
}
}