Skip to content

Commit

Permalink
Redis 数据缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinywan committed Oct 30, 2016
1 parent 10ee91b commit 17590ce
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 77 deletions.
174 changes: 110 additions & 64 deletions Backend/Home/Controller/DataBaseController.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function index()
{
$redis = RedisInstance::MasterInstance();
$redis->select(1);
$redisInfo = $redis->lRange('message01',0,10);
$redisInfo = $redis->lRange('message01', 0, 10);
var_dump($redisInfo);
}

Expand All @@ -22,7 +22,7 @@ public function MasterInstance()
{
$redis = RedisInstance::MasterInstance();
$redis->select(1);
$redisInfo = $redis->lRange('message01',0,10);
$redisInfo = $redis->lRange('message01', 0, 10);
var_dump($redisInfo);
}

Expand All @@ -33,7 +33,7 @@ public function SlaveOneInstance()
{
$redis = RedisInstance::SlaveOneInstance();
$redis->select(1);
$redisInfo = $redis->lRange('message01',0,15);
$redisInfo = $redis->lRange('message01', 0, 15);
var_dump($redisInfo);
}

Expand All @@ -44,14 +44,15 @@ public function SlaveTwoInstance()
{
$redis = RedisInstance::SlaveTwoInstance();
$redis->select(1);
$redisInfo = $redis->lRange('message01',0,6);
$redisInfo = $redis->lRange('message01', 0, 6);
var_dump($redisInfo);
}

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

Expand All @@ -77,56 +78,34 @@ public function ObjectCompare()
$redis1 = RedisInstance::MasterInstance();
$redis2 = RedisInstance::SlaveOneInstance();
$redis3 = RedisInstance::SlaveTwoInstance();
if($redis1 === $redis2){
if ($redis1 === $redis2) {
echo 'redis1和redis2:是同一个实例--';
}else{
} else {
echo '不是同一个实例';
}

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

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

}

/**
* 使用队列生成reids测试数据
* 成功:执行 RPUSH操作后,返回列表的长度:8
*/
public function createRedis()
{
$redis = RedisInstance::MasterInstance();
$redis->select(1);
$message = [
'type' => 'say',
'userId' => $redis->incr('user_id'),
'userName' => 'Tinywan'.mt_rand(100,9999), //是否正在录像
'userImage' => '/res/pub/user-default-w.png', //是否正在录像
'openId' => 'openId'.mt_rand(100000,9999999999999999),
'roomId' => 'openId'.mt_rand(30,50),
'createTime' => date('Y-m-d H:i:s', time()),
'content' => $redis->incr('content') //当前是否正在打流状态
];
$rPushResul = $redis->rPush('message01', json_encode($message)); //执行成功后返回当前列表的长度 9
return $rPushResul;
}

/**
* php cli模式执行php文件
* 模拟队列发送邮件
*/
public function executeCli()
{
$dir = 'D:\wamp\bin\php\php5.5.12>php.exe';
exec("D:\wamp\bin\php\php5.5.12>php.exe ../cli_test.php");
$dir = 'D:\wamp\bin\php\php5.5.12>php.exe';
exec("D:\wamp\bin\php\php5.5.12>php.exe ../cli_test.php");
}

/**
Expand All @@ -136,18 +115,88 @@ public function executeCli()
public function cli_test()
{
$count = 0;
while(true){
while (true) {
$count++;
file_put_contents("./test_result.txt",$count."\r\n",FILE_APPEND);
if($count > 10){
file_put_contents("./test_result.txt", $count . "\r\n", FILE_APPEND);
if ($count > 10) {
break;
}
sleep(3);
}
echo 'done';
}

/**
* 使用队列生成reids测试数据
* 成功:执行 RPUSH操作后,返回列表的长度:8
*/
public function createRedisList($listKey = 'message01')
{
$redis = RedisInstance::MasterInstance();
$redis->select(1);
$message = [
'type' => 'say',
'userId' => $redis->incr('user_id'),
'userName' => 'Tinywan' . mt_rand(100, 9999), //是否正在录像
'userImage' => '/res/pub/user-default-w.png', //是否正在录像
'openId' => 'openId' . mt_rand(100000, 9999999999999999),
'roomId' => 'openId' . mt_rand(30, 50),
'createTime' => date('Y-m-d H:i:s', time()),
'content' => $redis->incr('content') //当前是否正在打流状态
];
$rPushResul = $redis->rPush($listKey, json_encode($message)); //执行成功后返回当前列表的长度 9
return $rPushResul;
}

/**
* [0]检查当前Redis是否连接成功
* [1]获取数据,首先从Redis中去获取,没有的话再从数据库中去获取
*/
public function findDataRedisOrMysql($listKey = 'message01')
{
//Check the current connection status 查看服务是否运行
if (RedisInstance::MasterInstance() != false) {
$redis = RedisInstance::MasterInstance();
$redis->select(1);
/**
* 首先从Redis中去获取数据
* lRange 获取为空的话,则表示没有数据,否则返回一个非空数组
*/
$redisData = $redis->lRange($listKey, 0, 9);
$resultData = [];
if (!empty($redisData)) {
$resultData['status_code'] = 200;
$resultData['msg'] = 'Data Source from Redis Cache';
foreach ($redisData as $key => $val) {
$resultData['listData'][] = json_decode($val, true);
}
} else {
$resultData['redis_msg'] = 'Redis is Expire';
$conditions = array('status' => ':status');
$mysqlData = M('User')->where($conditions)->bind(':status',1, \PDO::PARAM_STR)->select();
if($mysqlData){
$resultData['status_code'] = 200;
$resultData['mysql_msg'] = 'Data Source from Mysql is Success';
foreach ($mysqlData as $key => $val) {
$resultData['listData'][] = $val;
}
}else{
$resultData['status_code'] = 500;
$resultData['mysql_msg'] = 'Data Source from Mysql is Fail';
}

}
} else {
$resultData['redis_msg'] = 'Redis server went away';
$resultData['mysql_msg'] = 'Mysql Data2';
$conditions = array('status' => ':status');
$mysqlData = M('User')->where($conditions)->bind(':status', 1, \PDO::PARAM_STR)->select();
foreach ($mysqlData as $key => $val) {
$resultData['listData'][] = $val;
}
}
homePrint($resultData);
}

/**
* 获取Redis数据
Expand All @@ -157,21 +206,18 @@ public function getRedisData()
{
$redis = RedisInstance::MasterInstance();
$redis->select(1);
$redisInfo = $redis->lRange('message01',0,-1);
var_dump($redisInfo);
die;
"<hr/>";
$redisInfo = $redis->lRange('message01', 0, 20);
$dataLength = $redis->lLen('message01');
// 10 14 19 20 21
if($dataLength > 20){
$redis->lTrim('message01',10,-1);
if ($dataLength > 20) {
$redis->lTrim('message01', 10, -1);
var_dump($dataLength);
}else{
echo '不可以删除了,只剩下:'.$dataLength.'条了';
} else {
echo '不可以删除了,只剩下:' . $dataLength . '条了';
var_dump($redisInfo);
}
foreach($redisInfo as $value){
$newArr[] = json_decode($value,true);
foreach ($redisInfo as $value) {
$newArr[] = json_decode($value, true);
}
var_dump($newArr);
die;
Expand All @@ -182,20 +228,20 @@ public function getRedisData()
*/
public function RedisSaveToMysql($dataList = 'Message01')
{
$sql= "insert into twenty_million (value) values";
for($i=0;$i<10;$i++){
$sql.="('50'),";
$sql = "insert into twenty_million (value) values";
for ($i = 0; $i < 10; $i++) {
$sql .= "('50'),";
};
$sql = substr($sql,0,strlen($sql)-1);
$sql = substr($sql, 0, strlen($sql) - 1);
var_dump($sql);
die;
if(empty($dataList)) {
if (empty($dataList)) {
$this->error = L('_DATA_TYPE_INVALID_');
return false;
}
$redis = RedisInstance::getInstance();
$redis->select(1);
$redisInfo = $redis->lRange('message01',0,9);
$redisInfo = $redis->lRange('message01', 0, 9);
$dataLength = $redis->lLen('message01');
// var_dump($redisInfo);
$model = new Model();
Expand All @@ -204,15 +250,15 @@ public function RedisSaveToMysql($dataList = 'Message01')
// $result = $model->query($sql);
// var_dump($result);
// die;
$redis->set('dataLength_front',$dataLength);
$redis->set('dataLength_front', $dataLength);
try {
$model->startTrans();
foreach ($redisInfo as $action) {
$sql = "INSERT INTO tour_user (username,description) VALUES (
json_decode($action,true)['userName'],
json_decode($action,true)['content'],
)";
$result = $model->query($sql);
$result = $model->query($sql);
}
$redis->set('message_insert_success', '00000');
// $redis->lTrim('message01', 10, -1);
Expand All @@ -232,23 +278,23 @@ public function RedisSaveToMysql($dataList = 'Message01')
/*
* TP 自带批量插入数据的方法
*/
public function addAll($dataList,$options=array(),$replace=false)
public function addAll($dataList, $options = array(), $replace = false)
{
if(empty($dataList)) {
if (empty($dataList)) {
$this->error = L('_DATA_TYPE_INVALID_');
return false;
}
// 数据处理
foreach ($dataList as $key=>$data){
foreach ($dataList as $key => $data) {
$dataList[$key] = $this->_facade($data);
}
// 分析表达式
$options = $this->_parseOptions($options);
$options = $this->_parseOptions($options);
// 写入数据到数据库
$result = $this->db->insertAll($dataList,$options,$replace);
if(false !== $result ) {
$insertId = $this->getLastInsID();
if($insertId) {
$result = $this->db->insertAll($dataList, $options, $replace);
if (false !== $result) {
$insertId = $this->getLastInsID();
if ($insertId) {
return $insertId;
}
}
Expand Down
37 changes: 27 additions & 10 deletions ThinkPHP/Library/Org/Util/RedisInstance.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@ private function __construct()
*/
public static function Instance()
{
if (!(static::$_instance instanceof \Redis)) {
static::$_instance = new \Redis();
try{
if (!(static::$_instance instanceof \Redis)) {
static::$_instance = new \Redis();
}
return static::$_instance;
}catch (\Exception $e){
return false;
}
return static::$_instance;

}

/**
Expand All @@ -56,9 +61,13 @@ public static function Instance()
*/
public static function MasterInstance()
{
self::Instance()->connect('121.41.88.209', '63789');
self::Instance()->auth('tinywanredis');
return static::$_instance;
try{
self::Instance()->connect('121.41.88.209', '63789');
self::Instance()->auth('tinywanredis');
return static::$_instance;
}catch (\Exception $e){
return false;
}
}


Expand All @@ -69,8 +78,12 @@ public static function MasterInstance()
*/
public static function SlaveOneInstance()
{
self::Instance()->connect('121.41.88.209', '63788');
return static::$_instance;
try{
self::Instance()->connect('121.41.88.209', '63788');
return static::$_instance;
}catch (\Exception $e){
return false;
}
}

/**
Expand All @@ -80,8 +93,12 @@ public static function SlaveOneInstance()
*/
public static function SlaveTwoInstance()
{
self::Instance()->connect('121.41.88.209', '63700');
return static::$_instance;
try{
self::Instance()->connect('121.41.88.209', '63700');
return static::$_instance;
}catch (\Exception $e){
return false;
}
}

/**
Expand Down
Loading

0 comments on commit 17590ce

Please sign in to comment.