Skip to content

Commit

Permalink
单例模式,多个对象共享一块内存区域(实例)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinywan committed Oct 29, 2016
1 parent 7c7e7f4 commit d876780
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 23 deletions.
81 changes: 79 additions & 2 deletions Backend/Home/Controller/DataBaseController.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,88 @@

class DataBaseController extends Controller
{
public function index(){
public function index()
{
$redis = RedisInstance::MasterInstance();
$redis->select(1);
$redisInfo = $redis->lRange('message01',0,10);
var_dump($redisInfo);
}

/**
* MasterInstance Redis主实例,适合于存储数据
*/
public function MasterInstance()
{
$redis = RedisInstance::MasterInstance();
$redis->select(1);
$redisInfo = $redis->lRange('message01',0,10);
var_dump($redisInfo);
}

/**
* SlaveOneInstance Redis从实例,适合于读取数据
*/
public function SlaveOneInstance()
{
$redis = RedisInstance::SlaveOneInstance();
$redis->select(1);
$redisInfo = $redis->lRange('message01',0,15);
var_dump($redisInfo);
}

/**
* SlaveTwoInstance Redis从实例,适合于读取数据
*/
public function SlaveTwoInstance()
{
$redis = RedisInstance::SlaveTwoInstance();
$redis->select(1);
$redisInfo = $redis->lRange('message01',0,6);
var_dump($redisInfo);
}

/**
* 连接本地的Redis实例
*/
public function localhostRedis(){
$redis = RedisInstance::Instance();
var_dump($redis);

$redis->connect('127.0.0.1');
$keys = $redis->keys('*');
var_dump($keys);
}

public function show($name){
/**
* 是否是同一个对象的比较
* 比较结果:
* 【redis1和redis2:是同一个实例--redis1和redis3:是同一个实例--redis2和redis3:是同一个实例--】
* 对象$redis1,$redis2,$redis3实际上都是使用同一个对象实例,访问的都是同一块内存区域
*/
public function ObjectCompare()
{

$redis1 = RedisInstance::MasterInstance();
$redis2 = RedisInstance::SlaveOneInstance();
$redis3 = RedisInstance::SlaveTwoInstance();
if($redis1 === $redis2){
echo 'redis1和redis2:是同一个实例--';
}else{
echo '不是同一个实例';
}

if($redis1 === $redis3){
echo 'redis1和redis3:是同一个实例--';
}else{
echo '不是同一个实例';
}

if($redis3 === $redis2){
echo 'redis2和redis3:是同一个实例--';
}else{
echo '不是同一个实例';
}

}

Expand Down
35 changes: 14 additions & 21 deletions ThinkPHP/Library/Org/Util/RedisInstance.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ private function __construct()

/**
* 单例方法,用于访问实例的公共的静态方法
* 这个只是一个实例
* 这个实例方法适合于连接到别的Redis数据库中去。列如:在项目中选择不同的Redis数据库
* @return \Redis
* @static
*/
public static function getInstance()
public static function Instance()
{
if (!(static::$_instance instanceof \Redis)) {
static::$_instance = new \Redis();
Expand All @@ -48,46 +50,37 @@ public static function getInstance()
}

/**
* 单例方法,用于访问实例的公共的静态方法
* 单例方法,用于访问Master实例的公共的静态方法
* @return \Redis
* @static
*/
public static function getMaster()
public static function MasterInstance()
{
if (!(static::$_instance instanceof \Redis)) {
static::$_instance = new \Redis();
self::getInstance()->connect('121.41.88.209', '63789');
self::getInstance()->auth('tinywanredis');
}
self::Instance()->connect('121.41.88.209', '63789');
self::Instance()->auth('tinywanredis');
return static::$_instance;
}


/**
* Slave1
* Slave1 实例
* @return null
* @static
*/
public static function getSlave1()
public static function SlaveOneInstance()
{
if (!(static::$_instance instanceof \Redis)) {
static::$_instance = new \Redis();
self::getInstance()->connect('121.41.88.209', '63788');
}
self::Instance()->connect('121.41.88.209', '63788');
return static::$_instance;
}

/**
* Slave2
* Slave2 实例
* @return null
* @static
*/
public static function getSlave2()
public static function SlaveTwoInstance()
{
if (!(static::$_instance instanceof \Redis)) {
static::$_instance = new \Redis();
self::getInstance()->connect('121.41.88.209', '63700');
}
self::Instance()->connect('121.41.88.209', '63700');
return static::$_instance;
}

Expand All @@ -101,7 +94,7 @@ public static function connect()
if (!self::$_connectSource)
{
//@return bool TRUE on success, FALSE on error.
self::$_connectSource = self::getInstance()->connect('121.41.88.209', '63789');
self::$_connectSource = self::Instance()->connect('121.41.88.209', '63789');
// 没有资源返回
if (!self::$_connectSource)
{
Expand Down

0 comments on commit d876780

Please sign in to comment.