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 9000dad commit d3a0007
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 20 deletions.
54 changes: 53 additions & 1 deletion Backend/Home/Common/function.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,56 @@ function cli_test()
sleep(3);
}
echo 'done';
}
}

/**
* 匿名函数也叫闭包函数(closures允许创建一个没有指定没成的函数,最经常用作回调函数参数的值。
* 闭包函数没有函数名称,直接在function()传入变量即可 使用时将定义的变量当作函数来处理
*/
function everyFunction(){
$message = 'hello';
$example = function() use ($message){
var_dump($message);
};
echo $example();
$message = 'world';
//输出hello 因为继承变量的值的时候是函数定义的时候而不是 函数被调用的时候
echo $example();
//重置为hello
$message = 'hello';
//此处传引用
$example = function() use(&$message){
var_dump($message);
};
echo $example();
//输出hello
$message = 'world';
echo $example();
//此处输出world
//闭包函数也用于正常的传值
$message = 'hello';
$example = function ($data) use ($message){
return "{$data},{$message}";
};

echo $example('world');
}



















28 changes: 23 additions & 5 deletions Backend/Home/Controller/DataBaseController.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,31 @@ public function addAll($dataList, $options = array(), $replace = false)
}

public function test(){
$aa = '4001:user_id';
$messageId = explode(':',$aa);
switch($messageId[0]){
case 4001:
$redis = RedisInstance::Instance();
$conn = $redis->connect('127.0.0.1', '6379000');
if($conn == true){
var_dump($conn);
}else{
echo 'Redis server went away';
}
var_dump($redis);

}

public function test2(){
$redis = RedisInstance::LocationInstance();
var_dump($redis);
$newRedis= new \Redis();
var_dump($newRedis);
die;
if($redis != false){
var_dump($redis->keys('*'));
echo '11111111';
}else{
echo '00000000';
}
echo $messageId[1];


}

}
10 changes: 10 additions & 0 deletions Backend/Home/Controller/IndexController.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Home\Controller;

use Home\Controller\BaseController;
use Org\Util\Car;
use Org\Util\RedisInstance;
use Org\Util\Tree;
use Org\Util\UserAgent;
Expand Down Expand Up @@ -159,4 +160,13 @@ public function getSessionRedis(){
echo session_name().'='.session_id();
die;
}

public function everyFunction(){
$carObj = new Car();
var_dump($carObj);
$carObj->add('butter',1);
$carObj->add('milk',4);
$carObj->add('eggs',9);
print_r($carObj->getTotal(0.05));
}
}
48 changes: 48 additions & 0 deletions ThinkPHP/Library/Org/Util/Car.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Created by PhpStorm.
* User: Tinywan
* Date: 2016/11/1
* Time: 9:04
* Mail: [email protected]
*/

namespace Org\Util;


class Car
{
//在类里面定义常量用 const 关键字,而不是通常的 define() 函数。
const PRICE_BUTTER = 1.00;
const PRICE_MILK = 3.00;
const PRICE_EGGS = 6.95;

protected $products = [];

public function add($product, $quantity)
{
$this->products[$product] = $quantity;
}

public function getQuantity($product)
{
//是否定义了
return isset($this->products[$product]) ? $this->products[$product] : FALSE;
}

public function getTotal($tax)
{
$total = 0.0;
$callback = function ($quantity, $product) use ($tax, &$total) {
//constant 返回常量的值
//__class__返回类名
$price = constant(__CLASS__ . "::PRICE_" . strtoupper($product));

$total += ($price * $quantity) * ($tax + 1.0);
};
//array_walk() 函数对数组中的每个元素应用用户自定义函数。在函数中,数组的键名和键值是参数
array_walk($this->products, $callback);
//回调匿名函数
return round($total, 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ class RedisInstance
* @var
*/
private static $_connectSource;

/**
* 私有化构造函数,防止类外实例化
* RedisConnect constructor.
*/
private function __construct()
{
}

/**
* 单例方法,用于访问实例的公共的静态方法
* 这个只是一个实例
Expand All @@ -38,84 +40,108 @@ private function __construct()
*/
public static function Instance()
{
try{
try {
if (!(static::$_instance instanceof \Redis)) {
static::$_instance = new \Redis();
}
return static::$_instance;
}catch (\Exception $e){
} catch (\Exception $e) {
return false;
}
}

/**
* 单例方法,用于访问Master实例的公共的静态方法
* @return \Redis
* @static
*/
public static function MasterInstance()
{
try{
self::Instance()->connect('121.41.88.209', '63789');
try {
$_connectSource = self::Instance()->connect('121.41.88.209', '63789');
if ($_connectSource === FALSE) return FALSE; //@return bool TRUE on success, FALSE on error.
self::Instance()->auth('tinywanredis');

return static::$_instance;
}catch (\Exception $e){
return false;
} catch (\Exception $e) {
return FALSE;
}
}

/**
* Slave1 实例
* @return null
* @static
*/
public static function SlaveOneInstance()
{
try{
try {
self::Instance()->connect('121.41.88.209', '63788');
if (self::$_connectSource === FALSE) return FALSE; //@return bool TRUE on success, FALSE on error.
return static::$_instance;
}catch (\Exception $e){
} catch (\Exception $e) {
return false;
}
}

/**
* Slave2 实例
* @return null
* @static
*/
public static function SlaveTwoInstance()
{
try{
try {
self::Instance()->connect('121.41.88.209', '63700');
if (self::$_connectSource === FALSE) return FALSE; //@return bool TRUE on success, FALSE on error.
return static::$_instance;
}catch (\Exception $e){
} catch (\Exception $e) {
return false;
}
}

/**
* Slave2 实例
* @return null
* @static
*/
public static function LocationInstance()
{
try {
if (self::Instance()->connect('127.0.0.1', '6379') === FALSE) return FALSE; //@return bool TRUE on success, FALSE on error.
if (self::Instance()->ping() != '+PONG') return FALSE;
return static::$_instance;
} catch (\Exception $e) {
return false;
}
}

/**
* Redis数据库是否连接成功
* @return bool|string
*/
public static function connect()
{
// 如果连接资源不存在,则进行资源连接
if (!self::$_connectSource)
{
if (!self::$_connectSource) {
//@return bool TRUE on success, FALSE on error.
self::$_connectSource = self::Instance()->connect('121.41.88.209', '63789');
// 没有资源返回
if (!self::$_connectSource)
{
if (!self::$_connectSource) {
return 'Redis Server Connection Fail';
}
}
return self::$_connectSource;
}

/**
* 私有化克隆函数,防止类外克隆对象
*/
private function __clone()
{
// TODO: Implement __clone() method.
}

/**
* @return \Redis
* @static
Expand Down

0 comments on commit d3a0007

Please sign in to comment.