redis edit
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
<?php
|
||||
namespace Config;
|
||||
|
||||
/**
|
||||
* mysql配置
|
||||
* @author shmilylbelva
|
||||
*/
|
||||
$database = require_once __DIR__.'/../../config/think-orm.php';
|
||||
|
||||
|
||||
// 正式
|
||||
define('DB_HOST', $database['connections']['mysql']['hostname']); //数据库地址
|
||||
define('DB_USER', $database['connections']['mysql']['username']); //用户名
|
||||
define('DB_PWD', $database['connections']['mysql']['password']); //数据库密码
|
||||
define('DB_NAME', $database['connections']['mysql']['database']); //库名
|
||||
define('DB_CHARSET', $database['connections']['mysql']['charset']); //字符集
|
||||
define('DB_PORT',3306); //端口
|
||||
|
||||
/**
|
||||
* 数据库的一个实例配置,则使用时像下面这样使用
|
||||
* $user_array = Db::instance('db1')->select('name,age')->from('users')->where('age>12')->query();print_r()exit
|
||||
* 等价于
|
||||
* $user_array = Db::instance('db1')->query('SELECT `name`,`age` FROM `users` WHERE `age`>12');
|
||||
* @var array
|
||||
*/
|
||||
class Db
|
||||
{
|
||||
public static $db = array(
|
||||
'host' => DB_HOST,
|
||||
'user' => DB_USER,
|
||||
'password' =>DB_PWD,
|
||||
'dbname' => DB_NAME,
|
||||
'port' => DB_PORT,
|
||||
'charset' => DB_CHARSET,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace extend\workermans\model;
|
||||
|
||||
use ba\Exception;
|
||||
use ba\Random;
|
||||
use support\Redis;
|
||||
use support\think\Db;
|
||||
use GatewayWorker\Lib\Gateway;
|
||||
use support\Log;
|
||||
@@ -16,7 +17,6 @@ class Base extends Model
|
||||
{
|
||||
|
||||
public static $db = null;
|
||||
public static $redis = null;
|
||||
public static $client_id;
|
||||
public static $shop_id;
|
||||
public static $account;
|
||||
@@ -124,12 +124,11 @@ class Base extends Model
|
||||
$session[$msg_id] = $time;
|
||||
Gateway::setSession($g_client_id, $session);
|
||||
$redis_name = $g_client_id . '_lock';
|
||||
$redis->get($redis_name, function ($result, $redis) use ($msg_id, $redis_name){
|
||||
$result = Redis::get($redis_name);
|
||||
if($result) {
|
||||
}else {
|
||||
$redis->set($redis_name, $msg_id, 3600);
|
||||
}
|
||||
});
|
||||
}
|
||||
print_r('锁单的key-----' . $msg_id . "\r\n");
|
||||
// 直接保存锁定
|
||||
@@ -160,11 +159,10 @@ class Base extends Model
|
||||
unset($session[$msg_id]);
|
||||
Gateway::setSession($g_client_id, $session);
|
||||
$redis_name = $g_client_id . '_lock';
|
||||
$redis->get($redis_name, function ($result, $redis) use ($redis_name){
|
||||
$result = Redis::get($redis_name);
|
||||
if($result) {
|
||||
$redis->del($redis_name);
|
||||
}
|
||||
});
|
||||
}
|
||||
print_r('解锁的key-----' . $msg_id . "\r\n");
|
||||
$rand = 'pay_unlock' . Random::build();
|
||||
@@ -268,15 +266,13 @@ class Base extends Model
|
||||
public static function geteventredis_new()
|
||||
{
|
||||
print_r('定时器' . "\r\n");
|
||||
$redis = self::$redis;
|
||||
$gateway = Gateway::class;
|
||||
// 处理消息(包含推送)
|
||||
$redis_name = Gateway::getAllClientSessions();
|
||||
foreach ($redis_name as $client_id => $client_id_data) {
|
||||
$order_print_client_id = $client_id . '_orderPrint';
|
||||
if(Gateway::isOnline($client_id)) {
|
||||
$result = Redis::get($client_id);
|
||||
// 处理普通消息
|
||||
$redis->get($client_id, function ($result, $redis) use($gateway, $client_id){
|
||||
if($result) {
|
||||
$msg_info_arr = json_decode($result, true);
|
||||
if($msg_info_arr['send_num'] < self::RETRYCOUNT) {
|
||||
@@ -297,17 +293,16 @@ class Base extends Model
|
||||
print_r('推送了-----' . "\r\n");
|
||||
// 推送以后新增推送次数
|
||||
$msg_info_arr['send_num'] += 1;
|
||||
$redis->set($client_id, json_encode($msg_info_arr), self::REDIS_TIME);
|
||||
Redis::set($client_id, json_encode($msg_info_arr), self::REDIS_TIME);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
self::add_log_file('----无回执--' .date('Y-m-d H:i:s'). '--->' . $result);
|
||||
}
|
||||
});
|
||||
|
||||
$r_name = $client_id . '_lock';
|
||||
// 处理锁单消息
|
||||
$redis->get($r_name, function ($result, $redis) use($client_id, $gateway, $r_name){
|
||||
$result = Redis::get($r_name);
|
||||
if($result) {
|
||||
$session = $gateway::getSession($client_id);
|
||||
print_r('session的内容-----' . json_encode($session) . "\r\n");
|
||||
@@ -316,18 +311,17 @@ class Base extends Model
|
||||
if(time() - $session[$result] > 10) {
|
||||
unset($session[$result]);
|
||||
$gateway::setSession($client_id, $session);
|
||||
$redis->del($r_name);
|
||||
Redis::del($r_name);
|
||||
print_r('解除锁定了-----' . json_encode($session) . "\r\n");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
$uid = $gateway::getUidByClientId($client_id);
|
||||
$result = Redis::get($uid);
|
||||
// 处理打印消息
|
||||
$redis->get($uid, function ($result, $redis) use($gateway, $uid){
|
||||
if($result) {
|
||||
$msg_info_arr = json_decode($result, true);
|
||||
foreach ($msg_info_arr as $k => $v) {
|
||||
@@ -345,17 +339,16 @@ class Base extends Model
|
||||
self::add_log_file('----推送收银机订单打印无回执--->' . $result, 'cashier');
|
||||
}
|
||||
if(empty($msg_info_arr)) {
|
||||
$redis->del($uid);
|
||||
Redis::del($uid);
|
||||
}else {
|
||||
$redis->set($uid, json_encode($msg_info_arr), self::REDIS_TIME);
|
||||
Redis::set($uid, json_encode($msg_info_arr), self::REDIS_TIME);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
$redis->del($client_id);
|
||||
$redis->del($uid);
|
||||
Redis::del($client_id);
|
||||
Redis::del($uid);
|
||||
}
|
||||
// 处理在线列表
|
||||
self::saveonlinelist($redis_name);
|
||||
@@ -365,9 +358,8 @@ class Base extends Model
|
||||
public static function saveonlinelist(array $client_id_list)
|
||||
{
|
||||
if(is_array($client_id_list)) {
|
||||
$redis = self::$redis;
|
||||
$redis_name = date('Y-m-d') . '_online_number';
|
||||
$redis->get($redis_name, function ($result, $redis) use ($redis_name, $client_id_list){
|
||||
$result = Redis::get($redis_name);
|
||||
if($result) {
|
||||
$result = json_decode($result, true);
|
||||
$client_id_arr = [];
|
||||
@@ -378,9 +370,8 @@ class Base extends Model
|
||||
$s_arr = array_values(array_intersect($client_id_arr, $redis_client_list));
|
||||
$result[$type] = $s_arr;
|
||||
}
|
||||
$redis->set($redis_name, json_encode($result), 60 * 60 * 24);
|
||||
Redis::set($redis_name, json_encode($result), 60 * 60 * 24);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -432,7 +423,7 @@ class Base extends Model
|
||||
$gateway = Gateway::class;
|
||||
$redis = self::$redis;
|
||||
// 找到本时候的数据msg_id
|
||||
$redis->get($redis_name, function ($result, $redis) use ($gateway, $redis_name) {
|
||||
$result = Redis::get($redis_name);
|
||||
if($result) {
|
||||
$msg_id_arr = json_decode($result, true);
|
||||
foreach ($msg_id_arr as $k => $msg) {
|
||||
@@ -511,7 +502,6 @@ class Base extends Model
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 购物车 多端同步 UID
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace extend\workermans\model;
|
||||
|
||||
use support\Redis;
|
||||
|
||||
/**
|
||||
* 购物车
|
||||
*/
|
||||
class RedisInit extends Base
|
||||
{
|
||||
public static function inits()
|
||||
{
|
||||
self::$redis = Redis::connection();
|
||||
}
|
||||
}
|
||||
@@ -25,13 +25,10 @@ class Events
|
||||
{
|
||||
// $url = 'http://127.0.0.1:8686/?method=sendToUid&account=101¶ms={"data_type":"order","order_id":"2541"}';
|
||||
// $url = 'https://sockets.sxczgkj.com/s/account/overview';
|
||||
// 启动redis
|
||||
RedisInit::inits();
|
||||
// 开启内部通讯接口。便于内部通讯
|
||||
$http_worker = new Worker('http://127.0.0.1:8686');
|
||||
$http_worker->reusePort = true;
|
||||
$redis = Base::$redis;
|
||||
$http_worker->onMessage = function (TcpConnection $connection, Request $request) use($redis) {
|
||||
$http_worker->onMessage = function (TcpConnection $connection, Request $request) {
|
||||
$method = $request->get('method');
|
||||
$account = $request->get('account');
|
||||
$params = $request->get('params');
|
||||
@@ -40,7 +37,7 @@ class Events
|
||||
$params_arr = json_decode($params, true);
|
||||
// 如果是订单打印并且通过uid推送, 保存redis
|
||||
if($params_arr['operate_type'] == 'order_print' && $method == 'sendToUid') {
|
||||
$redis->get($params_arr['send_id'], function ($result)use($params_arr, $params){
|
||||
$result = Redis::get($params_arr['send_id']);
|
||||
if($result) {
|
||||
$msg_id_arr = json_decode($result, true);
|
||||
$msg_id_arr_c = count($msg_id_arr);
|
||||
@@ -52,7 +49,6 @@ class Events
|
||||
print_r('数组-------->' . json_encode($params_arr_n)) . "\r\n";
|
||||
Base::setredis_new(json_encode($params_arr_n), $params_arr['send_id']);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return $connection->send('ok');
|
||||
@@ -64,8 +60,6 @@ class Events
|
||||
print_r('定时器');
|
||||
// 启动定时器 定时器处理消息回执/在线列表
|
||||
$_SESSION['auth_timer_id'] = Timer::add(10, function (){
|
||||
// print_r('时间--->' . date('Y-m-d H:i:s') . "\r\n");
|
||||
// print_r( "\r\n");
|
||||
Base::geteventredis_new();
|
||||
});
|
||||
}
|
||||
@@ -172,13 +166,11 @@ class Events
|
||||
$gate_way = GateWay::class;
|
||||
$number = $message['number'];
|
||||
try {
|
||||
$redis = self::$redis;
|
||||
$redis->select(3);
|
||||
$red_number = $redis->get('shop:' . $message['shop_id'] . ':product-stock:' . $message['product_id']);
|
||||
Redis::select(3);
|
||||
$red_number = Redis::get('shop:' . $message['shop_id'] . ':product-stock:' . $message['product_id']);
|
||||
}catch (\Exception $e) {
|
||||
print_r( "Redis 错误: " . $e->getMessage() . "\r\n");
|
||||
}
|
||||
$redis->close();
|
||||
print_r('库存数量-->' . $red_number . "\r\n" . date('Y-m-d H:i:s'));
|
||||
if(is_numeric($red_number)) {
|
||||
// 起售数量和库存之间的处理
|
||||
|
||||
Reference in New Issue
Block a user