9-03 2,747 views
需先安装php predis 类库
详细predis 命令参考 :http://www.koukousky.com/back/1644.html
<?php
/**
* Predis缓存驱动
* 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
*/
namespace app\common\services;
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(){
$options = array (
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => '6379',
#'password' => '',
);
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返回源数据
}
//静态get
public static function sGet($name) {
$value = self::getPredisObj()->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;
}
#静态set
public static function sSet($name, $value, $expire = null) {
//对数组/对象数据进行缓存处理,保证数据完整性
$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
if(is_int($expire) && $expire) {
$result = self::getPredisObj()->setex($name, $expire, $value);
}else{
$result = self::getPredisObj()->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 );
}
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);
}
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;
}
}