Skip to content

Commit d3a0007

Browse files
committed
Redis 主从复制Master和Slave
1 parent 9000dad commit d3a0007

File tree

5 files changed

+174
-20
lines changed

5 files changed

+174
-20
lines changed

Backend/Home/Common/function.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,56 @@ function cli_test()
4040
sleep(3);
4141
}
4242
echo 'done';
43-
}
43+
}
44+
45+
/**
46+
* 匿名函数也叫闭包函数(closures允许创建一个没有指定没成的函数,最经常用作回调函数参数的值。
47+
* 闭包函数没有函数名称,直接在function()传入变量即可 使用时将定义的变量当作函数来处理
48+
*/
49+
function everyFunction(){
50+
$message = 'hello';
51+
$example = function() use ($message){
52+
var_dump($message);
53+
};
54+
echo $example();
55+
$message = 'world';
56+
//输出hello 因为继承变量的值的时候是函数定义的时候而不是 函数被调用的时候
57+
echo $example();
58+
//重置为hello
59+
$message = 'hello';
60+
//此处传引用
61+
$example = function() use(&$message){
62+
var_dump($message);
63+
};
64+
echo $example();
65+
//输出hello
66+
$message = 'world';
67+
echo $example();
68+
//此处输出world
69+
//闭包函数也用于正常的传值
70+
$message = 'hello';
71+
$example = function ($data) use ($message){
72+
return "{$data},{$message}";
73+
};
74+
75+
echo $example('world');
76+
}
77+
78+
79+
80+
81+
82+
83+
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+

Backend/Home/Controller/DataBaseController.class.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,31 @@ public function addAll($dataList, $options = array(), $replace = false)
356356
}
357357

358358
public function test(){
359-
$aa = '4001:user_id';
360-
$messageId = explode(':',$aa);
361-
switch($messageId[0]){
362-
case 4001:
359+
$redis = RedisInstance::Instance();
360+
$conn = $redis->connect('127.0.0.1', '6379000');
361+
if($conn == true){
362+
var_dump($conn);
363+
}else{
364+
echo 'Redis server went away';
365+
}
366+
var_dump($redis);
367+
368+
}
363369

370+
public function test2(){
371+
$redis = RedisInstance::LocationInstance();
372+
var_dump($redis);
373+
$newRedis= new \Redis();
374+
var_dump($newRedis);
375+
die;
376+
if($redis != false){
377+
var_dump($redis->keys('*'));
378+
echo '11111111';
379+
}else{
380+
echo '00000000';
364381
}
365-
echo $messageId[1];
382+
383+
366384
}
367385

368386
}

Backend/Home/Controller/IndexController.class.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Home\Controller;
33

44
use Home\Controller\BaseController;
5+
use Org\Util\Car;
56
use Org\Util\RedisInstance;
67
use Org\Util\Tree;
78
use Org\Util\UserAgent;
@@ -159,4 +160,13 @@ public function getSessionRedis(){
159160
echo session_name().'='.session_id();
160161
die;
161162
}
163+
164+
public function everyFunction(){
165+
$carObj = new Car();
166+
var_dump($carObj);
167+
$carObj->add('butter',1);
168+
$carObj->add('milk',4);
169+
$carObj->add('eggs',9);
170+
print_r($carObj->getTotal(0.05));
171+
}
162172
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Tinywan
5+
* Date: 2016/11/1
6+
* Time: 9:04
7+
8+
*/
9+
10+
namespace Org\Util;
11+
12+
13+
class Car
14+
{
15+
//在类里面定义常量用 const 关键字,而不是通常的 define() 函数。
16+
const PRICE_BUTTER = 1.00;
17+
const PRICE_MILK = 3.00;
18+
const PRICE_EGGS = 6.95;
19+
20+
protected $products = [];
21+
22+
public function add($product, $quantity)
23+
{
24+
$this->products[$product] = $quantity;
25+
}
26+
27+
public function getQuantity($product)
28+
{
29+
//是否定义了
30+
return isset($this->products[$product]) ? $this->products[$product] : FALSE;
31+
}
32+
33+
public function getTotal($tax)
34+
{
35+
$total = 0.0;
36+
$callback = function ($quantity, $product) use ($tax, &$total) {
37+
//constant 返回常量的值
38+
//__class__返回类名
39+
$price = constant(__CLASS__ . "::PRICE_" . strtoupper($product));
40+
41+
$total += ($price * $quantity) * ($tax + 1.0);
42+
};
43+
//array_walk() 函数对数组中的每个元素应用用户自定义函数。在函数中,数组的键名和键值是参数
44+
array_walk($this->products, $callback);
45+
//回调匿名函数
46+
return round($total, 2);
47+
}
48+
}

ThinkPHP/Library/Org/Util/RedisInstance..class.php renamed to ThinkPHP/Library/Org/Util/RedisInstance.class.php

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ class RedisInstance
2222
* @var
2323
*/
2424
private static $_connectSource;
25+
2526
/**
2627
* 私有化构造函数,防止类外实例化
2728
* RedisConnect constructor.
2829
*/
2930
private function __construct()
3031
{
3132
}
33+
3234
/**
3335
* 单例方法,用于访问实例的公共的静态方法
3436
* 这个只是一个实例
@@ -38,84 +40,108 @@ private function __construct()
3840
*/
3941
public static function Instance()
4042
{
41-
try{
43+
try {
4244
if (!(static::$_instance instanceof \Redis)) {
4345
static::$_instance = new \Redis();
4446
}
4547
return static::$_instance;
46-
}catch (\Exception $e){
48+
} catch (\Exception $e) {
4749
return false;
4850
}
4951
}
52+
5053
/**
5154
* 单例方法,用于访问Master实例的公共的静态方法
5255
* @return \Redis
5356
* @static
5457
*/
5558
public static function MasterInstance()
5659
{
57-
try{
58-
self::Instance()->connect('121.41.88.209', '63789');
60+
try {
61+
$_connectSource = self::Instance()->connect('121.41.88.209', '63789');
62+
if ($_connectSource === FALSE) return FALSE; //@return bool TRUE on success, FALSE on error.
5963
self::Instance()->auth('tinywanredis');
64+
6065
return static::$_instance;
61-
}catch (\Exception $e){
62-
return false;
66+
} catch (\Exception $e) {
67+
return FALSE;
6368
}
6469
}
70+
6571
/**
6672
* Slave1 实例
6773
* @return null
6874
* @static
6975
*/
7076
public static function SlaveOneInstance()
7177
{
72-
try{
78+
try {
7379
self::Instance()->connect('121.41.88.209', '63788');
80+
if (self::$_connectSource === FALSE) return FALSE; //@return bool TRUE on success, FALSE on error.
7481
return static::$_instance;
75-
}catch (\Exception $e){
82+
} catch (\Exception $e) {
7683
return false;
7784
}
7885
}
86+
7987
/**
8088
* Slave2 实例
8189
* @return null
8290
* @static
8391
*/
8492
public static function SlaveTwoInstance()
8593
{
86-
try{
94+
try {
8795
self::Instance()->connect('121.41.88.209', '63700');
96+
if (self::$_connectSource === FALSE) return FALSE; //@return bool TRUE on success, FALSE on error.
8897
return static::$_instance;
89-
}catch (\Exception $e){
98+
} catch (\Exception $e) {
9099
return false;
91100
}
92101
}
102+
103+
/**
104+
* Slave2 实例
105+
* @return null
106+
* @static
107+
*/
108+
public static function LocationInstance()
109+
{
110+
try {
111+
if (self::Instance()->connect('127.0.0.1', '6379') === FALSE) return FALSE; //@return bool TRUE on success, FALSE on error.
112+
if (self::Instance()->ping() != '+PONG') return FALSE;
113+
return static::$_instance;
114+
} catch (\Exception $e) {
115+
return false;
116+
}
117+
}
118+
93119
/**
94120
* Redis数据库是否连接成功
95121
* @return bool|string
96122
*/
97123
public static function connect()
98124
{
99125
// 如果连接资源不存在,则进行资源连接
100-
if (!self::$_connectSource)
101-
{
126+
if (!self::$_connectSource) {
102127
//@return bool TRUE on success, FALSE on error.
103128
self::$_connectSource = self::Instance()->connect('121.41.88.209', '63789');
104129
// 没有资源返回
105-
if (!self::$_connectSource)
106-
{
130+
if (!self::$_connectSource) {
107131
return 'Redis Server Connection Fail';
108132
}
109133
}
110134
return self::$_connectSource;
111135
}
136+
112137
/**
113138
* 私有化克隆函数,防止类外克隆对象
114139
*/
115140
private function __clone()
116141
{
117142
// TODO: Implement __clone() method.
118143
}
144+
119145
/**
120146
* @return \Redis
121147
* @static

0 commit comments

Comments
 (0)