您的位置:首页 > 数据库 > Redis

写一个Redis封装类

2016-01-04 11:29 681 查看
打算自己封装一个Redis操作类,方便使用,且有一定log记录。
Redis的封装思路:
基于Redis类进一步封装

一般属性

单例 (配置参数从配置文件中读取还是写死?考虑多配置之间切换)

常规操作根据业务单独处理。(注意健壮性和容错)

目的不同,可能做的封装也同。
我们要基于业务做一些抽象程度较低的封装,也会根据需要,做一些tool级别的高抽象的封装。如果抽象的足够高,可以让通用性变得更好。
这次封装Redis,我先做一个通用性的Redis操作类,先仅作最基本的一些操作。

代码如下:

<?php
/**
* Created by PhpStorm.
* User: freephp
* Date: 2016/1/4
* Time: 11:09
*/

class MyRedis extends Redis {

public static $instance = null;  // 单例对象

public $isConnect = false; // 判断redis是否链接成功

public $connectMsg = ''; // redis链接信息

public $date = null; // log记录日期

private $config = [
//主(master)服务器
'master' => [
'hostname' => '192.168.1.223',
'password' => 'sewerew',
'port' => '6379',
'timeout' => '5',
],
//从(slave)服务器
'slave' => [
'hostname' => '192.168.1.230',
'password' => 'qweqq',
'port' => '6379',
'timeout' => '5',
]
];

function __construct($params = array()) {
parent::__construct();
$serverArray = $this->config;

//组装参数
$hostname = $serverArray['master']['hostname']; //连接地址
$password = $serverArray['master']['password']; //密码
$port = $serverArray['master']['port']; //端口
$timeout = $serverArray['master']['timeout']; //超时
//选择用户指定的主机和数据库
if (isset($params['redis']) && array_key_exists($params['redis'], $serverArray)) {
$hostname = $serverArray[$params['redis']]['hostname']; //连接地址
$password = $serverArray[$params['redis']]['password']; //密码
$port = $serverArray[$params['redis']]['port']; //端口
$timeout = $serverArray[$params['redis']]['timeout']; //超时
}
$this->date = date('Y-m-d', time());

$this->_connect($hostname, $port, $timeout, $password);
}

/**
*  连接数据库
*
* @param string $hostname  主机地址
* @param int    $port      redis端口
* @param int    $timeout   超时时间默认30s
* @param string $password  验证密码
* @param bool   $isPConnect  是否长连接:默认false非长连接
* @return bool               返回值:成功返回true,失败false
*/
private function _connect($hostname, $port = 6379, $timeout = 5, $password = '', $isPConnect = false) {
//开始连接数据库
try {
if ($isPConnect == false) {
$status = $this->connect($hostname, $port, $timeout);
} else {
$status = $this->pconnect($hostname, $port, $timeout);
}

if (!$status) {
error_log(date('Y-m-d H:i:s') . ":" . 'redis connect error' . "\t",3, "./application/logs/redis-error-{$this->date}.log");
return $this->response(false, 'redis connect error');
}
} catch (Exception $e) {
error_log(date('Y-m-d H:i:s') . ":" . $e->getMessage() . "\t",3, "./application/logs/redis-error-{$this->date}.log");
return $this->response(false, $e->getMessage());
}

//验证密码
if ($password && !$this->auth($password)) {
error_log(date('Y-m-d H:i:s') . ":" . 'redis password error' . "\t",3, "./application/logs/redis-error-{$this->date}.log");
return $this->response(false, 'redis password error');
}

return $this->response(true, 'redis connect success');
}

public static function getInstance($params = array(), $flag = false) {
if (!(self::$instance instanceof self) || $flag) {
self::$instance = new self($params = array());
}
return self::$instance;
}

/**
* 返回消息
*
* @param  bool   $status   状态
* @param  string $msg      消息
* @return void
*/
private function response($status = false, $msg = '') {
$this->isConnect = $status; //判断redis是否连接成功
$this->connectMsg = $msg;  //连接redis的消息通知
return $status;
}

}

// 调用
$myredis = new MyRedis();

var_dump($myredis->connectMsg);die();


我们基于更详细业务做的其他封装类可以使用这个MyRedis作为一个注入类,多用组合,实现解耦。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: