主库只负责写数据,每次有数据更新都将更新的数据同步到它所有的从库,而从库只负责读数据
所以先复制添加一个配置redis_6380.conf
cp redis.conf redis_6380.conf
redis.conf
#给主服务器设置密码
requirepass 123456
#定义主服务器端口
port 6379
redis.conf
masterauth 123456 # 若Master设置了密码,需要此配置; 对应 Master/redis.conf requirepass 属性。
port 6380 #定义从服务器端口
slaveof 127.0.0.1 6379 #设置成为127.0.0.1的从服务器
thinkphp里的使用
config.php
中配置 //redis分布式主从配置
'REDIS_LIST' => [
'db01' => [
'master' => [
'host' => 'localhost',
'port' => '6379',
'persistent' => false,
],
'standby' => [
'host' => 'localhost',
'port' => '6380',
'persistent' => false,
],
],
'db02' => [
'master' => [
'host' => '192.168.9.228',
'port' => '6379',
'persistent' => false,
],
'standby' => [
'host' => '192.168.9.228',
'port' => '6380',
'persistent' => false,
],
]
],
RedisManagerModel.php
文件用来实例化redis
namespace app\app\model;
class RedisManagerModel {
static private $instances = array();
/**
* 实例化redis
* @param type $adapter
* @return \Redis
* @throws \Exception
*/
static private function createInstance($adapter)
{
$config = config('REDIS_LIST');
$redis = new \Redis();
if ($config[$adapter]["master"]["persistent"]) {
$result = $redis->pconnect($config[$adapter]["master"]["host"], $config[$adapter]["master"]["port"]);
} else {
$result = $redis->connect($config[$adapter]["master"]["host"], $config[$adapter]["master"]["port"]);
}
//stanby 进程
if (!$result) {
if ($config[$adapter]["standby"]["persistent"]) {
$result = $redis->pconnect($config[$adapter]["standby"]["host"], $config[$adapter]["standby"]["port"]);
} else {
$result = $redis->connect($config[$adapter]["standby"]["host"], $config[$adapter]["standby"]["port"]);
}
}
if (!$result) {
throw new \Exception('Redis Connection Error');
}
return $redis;
}
/**单例模式静态访问该方法
* @param string $adapter
* @return Redis
*/
static public function getInstance($adapter = "db01")
{
if (!isset(self::$instances[$adapter])) {
self::$instances[$adapter] = self::createInstance($adapter);
}
return self::$instances[$adapter];
}
}
RedisMdoel.php
来放所有关于redis的操作
namespace app\app\model;
use think\db;
use app\app\model\RedisManagerModel;
class RedisMdoel {
public $redis_db_no = "db01";//默认使用 db01
/**
* 删除redis的key
* @param type $key
* @return boolean
*/
public static function redisDelKey($key)
{
RedisManagerModel::getInstance()->del($key);
return true;
}
/*
* 读 redis,将数据以json的格式存入
* @param $key
* @param null $value
* @return mixed|null
*/
public static function get_from_redis($key, $value = null)
{
if (!is_null($value)) {
RedisManagerModel::getInstance()->set($key, json_encode($value, true));
}
$result = RedisManagerModel::getInstance()->get($key);
return $result ? json_decode($result, true) : null;
}
/*
* 读redis,将数据 以 list 数据格式存入
* @param $key
* @param $start 起始值
* @param $length 长度
* @param null $value
* @return
*/
public static function get_list_from_redis($key, $start = 0, $length = 0, $value = null)
{
if (!is_null($value)) {
// list 数据类型,当有新的数据应该将旧数据清除
RedisManagerModel::getInstance()->delete($key);
foreach ($value as $item) {
RedisManagerModel::getInstance()->lPush($key, json_encode($item, true));
}
}
if ($length === 0) {
$length = RedisManagerModel::getInstance()->lLen($key);
}
$lst = RedisManagerModel::getInstance()->lGetRange($key, $start, $length);
if ($lst) {
$result = array();
foreach ($lst as $item) {
array_push($result, json_decode($item, true));
}
return $result;
}
return null;
}
}
在项目中调用
namespace app\app\model;
use app\app\model\RedisMdoel;
class Test extends Model{
/**
* 删除没用的redisKey
*/
public function delRedisKey($key)
{
if(RedisMdoel::redisDelKey($key)){
return true;
}
return false
}
}