安装和配置
安装redis服务和php-redis扩展:https://blog.csdn.net/why444216978/article/details/83659776
主从复制:https://blog.csdn.net/why444216978/article/details/100170179
封装服务类RedisService.php
<?php
class RedisService
{
private $getObj;
private $setObj;
private $name;
private $config;
private $host;
private $port;
private $auth;
private $try;
private $connectTimeout;
private $db;
private $readTimeout;
private $getFunction = array(
'DUMP',
'GET',
'STRLEN',
'MGET',
'HKEYS',
'HGET',
'HMGET',
'HGETALL',
'HVALS',
'SCAN',
'SSCAN',
'ZSCAN',
'MSSCAN',
'SMEMBERS',
'SISMEMBER',
'SCARD',
'SPOP',
'ZRANGEBYSCORE',
'ZREVRANGEBYSCORE',
'ZSCORE',
'ZCOUNT',
'LRANGE',
'ZRANGE',
'ZREVRANGE',
'ZRANK',
'ZREVRANK',
'ZCARD',
'TTL',
'LLEN',
'GETMULTIPLE',
'EXISTS',
'HEXISTS',
);
private $setFunction = array(
'PEXPIREAT',
'EXPIREAT',
'PEXPIRE',
'SET',
'DEL',
'MSET',
'DELETE',
'MSETNX',
'LREM',
'LTRIM',
'INCR',
'INCRBY',
'DECR',
'DECRBY',
'HSET',
'HDEL',
'LPUSH',
'LMPUSH',
'RPUSH',
'RMPUSH',
'RPOP',
'HMSET',
'SADD',
'SMADD',
'SREM',
'SREMOVE',
'ZADD',
'ZMADD',
'ZMREM',
'ZREM',
'ZDELETE',
'ZINCRBY',
'EXPIRE',
'HINCRBY',
'ZREMRANGEBYSCORE',
'ZREMRANGEBYRANK',
);
public function __construct($name = 'default')
{
$this->name = $name;
}
public function exec($func)
{
return $this->getConnectionByFuncName($func);
}
private function getConnectionByFuncName($func)
{
if (in_array(strtoupper($func), $this->getFunction)) {
$obj = $this->getReadObj();
} else if (in_array(strtoupper($func), $this->setFunction)) {
$obj = $this->getWriteObj();
}else{
throw new Exception('function not exists', 10001);
}
return $obj;
}
private function setConfig()
{
$configArr = require_once('./redis_conf.php');
$this->config = $configArr[$this->name];
$this->host = $this->config['host'] ?: '127.0.0.1';
$this->port = $this->config['port'] ?: 6379;
$this->auth = $this->config['auth'] ?: '';
$this->try = $this->config['try'] ?: 5;
$this->connectTimeout = $this->config['connect_timeout'] ?: 5;
$this->readTimeout = $this->config['read_timeout'] ?: 3;
$this->db = $this->config['db'] ?: 0;
}
private function getReadObj()
{
$this->name = $this->name . '_read';
$this->setConfig();
if (!isset($this->getObj) || !is_object($this->getObj)) {
$this->getObj = new Redis();
for ($i = $this->try; $i > 0; $i--) {
$re_con = $this->getObj->pconnect($this->host, $this->port, $this->connectTimeout);
if ($re_con) {
break;
}
}
$this->getObj->setOption(Redis::OPT_READ_TIMEOUT, $this->readTimeout);
$this->auth && $this->getObj->auth($this->auth);
$this->getObj->select($this->db);
}
return $this->getObj;
}
private function getWriteObj()
{
$this->name = $this->name . '_write';
$this->setConfig();
if (!isset($this->conn) || !is_object($this->setObj)) {
$this->setObj = new Redis();
for ($i = $this->try; $i > 0; $i--) {
$re_con = $this->setObj->pconnect($this->host, $this->port, $this->connectTimeout);
if ($re_con) {
break;
}
}
$this->auth && $this->setObj->auth($this->auth);
$this->setObj->select($this->db);
}
return $this->setObj;
}
public function __destruct()
{
}
}
配置文件redis_conf.php
<?php
return [
'default_read' => [
'host' => '127.0.0.1',
'port' => 7000,
'auth' => '',
'try' => 5,
'connect_timeout' => 5,
'read_timeout' => 3,
'db' => 0
],
'default_write' => [
'host' => '127.0.0.1',
'port' => 6379,
'auth' => '',
'try' => 5,
'connect_timeout' => 5,
'read_timeout' => 3,
'db' => 0
],
];
单例模式加载库方法
单例模式详解:https://blog.csdn.net/why444216978/article/details/103718450
<?php
function load($class, $config)
{
static $objs = array();
$class = strtolower($class);
if (isset($objs[$class]) && isset($objs[$class][$config])) {
return $objs[$class][$config];
}
if (!isset($objs[$class])) {
$objs[$class] = [];
$className = ucfirst($class);
$objs[$class][$config] = new $className($config);
return $objs[$class][$config];
}
}
测试代码
<?php
require_once './RedisService.php';
require_once './helper.php';
$redisObj = load('RedisService', 'default');
$redis = $redisObj->exec('set');
var_dump($redis->set('why', 'why'));
var_dump($redis->get('why'));
/usr/bin/php /Users/why/Desktop/php/why.php
bool(true)
string(3) "why"
Process finished with exit code 0