diff --git a/Backend/Home/Controller/DataBaseController.class.php b/Backend/Home/Controller/DataBaseController.class.php index ff23c96..ed67615 100644 --- a/Backend/Home/Controller/DataBaseController.class.php +++ b/Backend/Home/Controller/DataBaseController.class.php @@ -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); } @@ -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); } @@ -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); } @@ -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); @@ -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"); } /** @@ -136,10 +115,10 @@ 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); @@ -147,7 +126,77 @@ public function cli_test() 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数据 @@ -157,21 +206,18 @@ public function getRedisData() { $redis = RedisInstance::MasterInstance(); $redis->select(1); - $redisInfo = $redis->lRange('message01',0,-1); - var_dump($redisInfo); - die; - "
"; + $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; @@ -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(); @@ -204,7 +250,7 @@ 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) { @@ -212,7 +258,7 @@ public function RedisSaveToMysql($dataList = 'Message01') 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); @@ -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; } } diff --git a/ThinkPHP/Library/Org/Util/RedisInstance.class.php b/ThinkPHP/Library/Org/Util/RedisInstance.class.php index 75c53e9..d04815b 100644 --- a/ThinkPHP/Library/Org/Util/RedisInstance.class.php +++ b/ThinkPHP/Library/Org/Util/RedisInstance.class.php @@ -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; + } /** @@ -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; + } } @@ -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; + } } /** @@ -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; + } } /** diff --git a/ThinkPHP/Mode/cli.php b/ThinkPHP/Mode/cli.php new file mode 100644 index 0000000..536b128 --- /dev/null +++ b/ThinkPHP/Mode/cli.php @@ -0,0 +1,70 @@ + +// +---------------------------------------------------------------------- + +/** + * ThinkPHP 普通模式定义 + */ +return array( + // 配置文件 + 'config' => array( + THINK_PATH.'Conf/convention.php', // 系统惯例配置 + CONF_PATH.'config'.CONF_EXT, // 应用公共配置 + ), + + // 别名定义 + 'alias' => array( + 'Think\Log\Driver\File' => CORE_PATH . 'Log/Driver/File'.EXT, + 'Think\Exception' => CORE_PATH . 'Exception'.EXT, + 'Think\Model' => CORE_PATH . 'Model'.EXT, + 'Think\Db' => CORE_PATH . 'Db'.EXT, + 'Think\Template' => CORE_PATH . 'Template'.EXT, + 'Think\Cache' => CORE_PATH . 'Cache'.EXT, + 'Think\Cache\Driver\File' => CORE_PATH . 'Cache/Driver/File'.EXT, + 'Think\Storage' => CORE_PATH . 'Storage'.EXT, + ), + + // 函数和类文件 + 'core' => array( + THINK_PATH.'Common/functions.php', + COMMON_PATH.'Common/function.php', + CORE_PATH . 'Hook'.EXT, + CORE_PATH . 'App'.EXT, + CORE_PATH . 'Dispatcher'.EXT, + //CORE_PATH . 'Log'.EXT, + CORE_PATH . 'Route'.EXT, + CORE_PATH . 'Controller'.EXT, + CORE_PATH . 'View'.EXT, + BEHAVIOR_PATH . 'BuildLiteBehavior'.EXT, + BEHAVIOR_PATH . 'ParseTemplateBehavior'.EXT, + BEHAVIOR_PATH . 'ContentReplaceBehavior'.EXT, + ), + // 行为扩展定义 + 'tags' => array( + 'app_init' => array( + 'Behavior\BuildLiteBehavior', // 生成运行Lite文件 + ), + 'app_begin' => array( + 'Behavior\ReadHtmlCacheBehavior', // 读取静态缓存 + ), + 'app_end' => array( + 'Behavior\ShowPageTraceBehavior', // 页面Trace显示 + ), + 'view_parse' => array( + 'Behavior\ParseTemplateBehavior', // 模板解析 支持PHP、内置模板引擎和第三方模板引擎 + ), + 'template_filter'=> array( + 'Behavior\ContentReplaceBehavior', // 模板输出替换 + ), + 'view_filter' => array( + 'Behavior\WriteHtmlCacheBehavior', // 写入静态缓存 + ), + ), +); diff --git a/admin.php b/admin.php index c702f10..6447bf5 100644 --- a/admin.php +++ b/admin.php @@ -1,3 +1,6 @@ + + + +// +---------------------------------------------------------------------- + +// 应用入口文件 + +// 检测PHP环境 +if(version_compare(PHP_VERSION,'5.3.0','<')) die('require PHP > 5.3.0 !'); + +// 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false +define('APP_DEBUG',True); + +/** + * 绑定Home模块到当前入口文件(3.2.1以上版本写法) + * old:http://serverName/index.php/Home/Index/index + * new:http://serverName/home.php/Index/index + */ +//define('BIND_MODULE','Home'); + + +/** + * 绑定Index控制器到当前入口文件(3.2.1以上版本写法) + * old:http://serverName/index.php/Home/Index/index + * new:http://serverName/home.php/index + */ +//define('BIND_CONTROLLER','Index'); +//php-cli http://www.thinkphp.cn/topic/34761.html + +// 定义应用目录 +define('APP_PATH',dirname(__FILE__).'/Backend/'); + +// 引入ThinkPHP入口文件 +require dirname( __FILE__).'/ThinkPHP/ThinkPHP.php';