Skip to content

Commit

Permalink
Redis 主从复制Master和Slave
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinywan committed Nov 1, 2016
1 parent 1a63fd7 commit 9000dad
Showing 1 changed file with 127 additions and 0 deletions.
127 changes: 127 additions & 0 deletions ThinkPHP/Library/Org/Util/RedisInstance..class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* Created by PhpStorm.
* User: Tinywan
* Date: 2016/11/1
* Time: 8:54
* Mail: [email protected]
*/

namespace Org\Util;


class RedisInstance
{
/**
* 类对象实例数组,共有静态变量
* @var null
*/
private static $_instance;
/**
* 数据库连接资源句柄
* @var
*/
private static $_connectSource;
/**
* 私有化构造函数,防止类外实例化
* RedisConnect constructor.
*/
private function __construct()
{
}
/**
* 单例方法,用于访问实例的公共的静态方法
* 这个只是一个实例
* 这个实例方法适合于连接到别的Redis数据库中去。列如:在项目中选择不同的Redis数据库
* @return \Redis
* @static
*/
public static function Instance()
{
try{
if (!(static::$_instance instanceof \Redis)) {
static::$_instance = new \Redis();
}
return static::$_instance;
}catch (\Exception $e){
return false;
}
}
/**
* 单例方法,用于访问Master实例的公共的静态方法
* @return \Redis
* @static
*/
public static function MasterInstance()
{
try{
self::Instance()->connect('121.41.88.209', '63789');
self::Instance()->auth('tinywanredis');
return static::$_instance;
}catch (\Exception $e){
return false;
}
}
/**
* Slave1 实例
* @return null
* @static
*/
public static function SlaveOneInstance()
{
try{
self::Instance()->connect('121.41.88.209', '63788');
return static::$_instance;
}catch (\Exception $e){
return false;
}
}
/**
* Slave2 实例
* @return null
* @static
*/
public static function SlaveTwoInstance()
{
try{
self::Instance()->connect('121.41.88.209', '63700');
return static::$_instance;
}catch (\Exception $e){
return false;
}
}
/**
* Redis数据库是否连接成功
* @return bool|string
*/
public static function connect()
{
// 如果连接资源不存在,则进行资源连接
if (!self::$_connectSource)
{
//@return bool TRUE on success, FALSE on error.
self::$_connectSource = self::Instance()->connect('121.41.88.209', '63789');
// 没有资源返回
if (!self::$_connectSource)
{
return 'Redis Server Connection Fail';
}
}
return self::$_connectSource;
}
/**
* 私有化克隆函数,防止类外克隆对象
*/
private function __clone()
{
// TODO: Implement __clone() method.
}
/**
* @return \Redis
* @static
*/
public static function test()
{
return 'test';
}
}

0 comments on commit 9000dad

Please sign in to comment.