优化: 收银台购物延迟压至15秒内(busy_timeout 60s→8s、移除热路径冗余DDL、合并配置批量读取)
This commit is contained in:
@@ -123,7 +123,6 @@ function cashierPlayerCheck() {
|
||||
exit(json_encode(['success' => false, 'message' => '玩家名格式不正确(3-16位英文/数字/下划线)'], JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
$db = getDB();
|
||||
$db->exec("CREATE TABLE IF NOT EXISTS bond_cache (player_name TEXT PRIMARY KEY, amount INTEGER DEFAULT 0, updated_at INTEGER DEFAULT 0)");
|
||||
try {
|
||||
$db->exec("INSERT OR IGNORE INTO bond_cache (player_name, amount, updated_at) VALUES ('" . str_replace("'", "''", $player) . "', 0, " . time() . ")");
|
||||
$stmt = $db->prepare("SELECT amount FROM bond_cache WHERE player_name = :name");
|
||||
|
||||
+37
-12
@@ -249,11 +249,10 @@ function shopBuy($token) {
|
||||
], '购买成功');
|
||||
}
|
||||
|
||||
// ===== 购物车配置读取 =====
|
||||
// ===== 购物车配置读取(批量优化:单次查询读取所有需要的配置) =====
|
||||
function getShopConfig($key, $default = null) {
|
||||
try {
|
||||
$db = getDB();
|
||||
$db->exec("CREATE TABLE IF NOT EXISTS shop_config (cfg_key TEXT PRIMARY KEY, cfg_value TEXT NOT NULL)");
|
||||
$stmt = $db->prepare("SELECT cfg_value FROM shop_config WHERE cfg_key = :k");
|
||||
$stmt->bindValue(':k', $key, SQLITE3_TEXT);
|
||||
$result = $stmt->execute();
|
||||
@@ -264,13 +263,38 @@ function getShopConfig($key, $default = null) {
|
||||
}
|
||||
}
|
||||
|
||||
// 返回购物车折扣/加价配置(前端展示用)
|
||||
/**
|
||||
* 批量读取多个shop_config键(一次查询替代N次getShopConfig调用)
|
||||
* @return array key => value (不存在的key不返回)
|
||||
*/
|
||||
function getShopConfigs(array $keys) {
|
||||
if (empty($keys)) return [];
|
||||
try {
|
||||
$db = getDB();
|
||||
$placeholders = implode(',', array_fill(0, count($keys), '?'));
|
||||
$stmt = $db->prepare("SELECT cfg_key, cfg_value FROM shop_config WHERE cfg_key IN ($placeholders)");
|
||||
foreach ($keys as $i => $k) {
|
||||
$stmt->bindValue($i + 1, $k, SQLITE3_TEXT);
|
||||
}
|
||||
$result = $stmt->execute();
|
||||
$map = [];
|
||||
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||
$map[$row['cfg_key']] = $row['cfg_value'];
|
||||
}
|
||||
return $map;
|
||||
} catch (Exception $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
// 返回购物车折扣/加价配置(前端展示用,★ 批量读取)
|
||||
function cartConfig() {
|
||||
$cfg = getShopConfigs(['cart_backpack_rate', 'cart_shulker_rate', 'packmoney', 'green_discount']);
|
||||
success([
|
||||
'backpack_rate' => (float)getShopConfig('cart_backpack_rate', '0.98'),
|
||||
'shulker_rate' => (float)getShopConfig('cart_shulker_rate', '1.00'),
|
||||
'packmoney' => (int)getShopConfig('packmoney', '5'),
|
||||
'green_discount' => (float)getShopConfig('green_discount', '2')
|
||||
'backpack_rate' => (float)($cfg['cart_backpack_rate'] ?? '0.98'),
|
||||
'shulker_rate' => (float)($cfg['cart_shulker_rate'] ?? '1.00'),
|
||||
'packmoney' => (int)($cfg['packmoney'] ?? '5'),
|
||||
'green_discount' => (float)($cfg['green_discount'] ?? '2')
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -345,12 +369,13 @@ function shopBuyCart($token) {
|
||||
}
|
||||
}
|
||||
|
||||
// 结算模式对应的折扣/加价系数
|
||||
// 结算模式对应的折扣/加价系数(★ 批量读取所有配置键,1次查询替代4次)
|
||||
$cfg = getShopConfigs(['cart_shulker_rate', 'packmoney', 'cart_backpack_rate', 'green_discount']);
|
||||
if ($settlement === 'shulker') {
|
||||
$rate = (float)getShopConfig('cart_shulker_rate', '1.00');
|
||||
$rate = (float)($cfg['cart_shulker_rate'] ?? '1.00');
|
||||
$modeName = '潜影盒打包';
|
||||
// 潜影盒颜色额外收费(原色/紫色免费,其它加收打包费,打包费来自配置 packmoney)
|
||||
$packMoney = (int)getShopConfig('packmoney', '5');
|
||||
$packMoney = (int)($cfg['packmoney'] ?? '5');
|
||||
$colorFee = ($shulkerColor !== 'default' && $shulkerColor !== 'purple') ? $packMoney : 0;
|
||||
// 颜色名称映射
|
||||
$colorNames = ['default'=>'原色','purple'=>'原色','white'=>'白色','black'=>'黑色','red'=>'红色','blue'=>'蓝色','green'=>'绿色','yellow'=>'黄色','orange'=>'橙色'];
|
||||
@@ -358,12 +383,12 @@ function shopBuyCart($token) {
|
||||
$ecoPct = 0;
|
||||
} else {
|
||||
$settlement = 'backpack';
|
||||
$rate = (float)getShopConfig('cart_backpack_rate', '0.98');
|
||||
$rate = (float)($cfg['cart_backpack_rate'] ?? '0.98');
|
||||
$modeName = '塞背包(环保单)';
|
||||
$colorFee = 0;
|
||||
$colorName = '';
|
||||
// 环保单减免:不打包/塞背包时按配置比例减免(与游戏内"不打包"一致)
|
||||
$ecoPct = (float)getShopConfig('green_discount', '2');
|
||||
$ecoPct = (float)($cfg['green_discount'] ?? '2');
|
||||
}
|
||||
|
||||
$db = getDB();
|
||||
|
||||
+12
-21
@@ -7,33 +7,24 @@ require_once __DIR__ . '/core.php';
|
||||
|
||||
if (session_status() === PHP_SESSION_NONE) session_start();
|
||||
|
||||
// 读取打包费配置(彩色潜影盒加收,须与后端 shopBuyCart 保持一致)
|
||||
function cashierGetPackMoney() {
|
||||
// 读取收银台配置(★ 单次查询同时获取packmoney和green_discount)
|
||||
function cashierGetConfig() {
|
||||
try {
|
||||
$db = getDB();
|
||||
$db->exec("CREATE TABLE IF NOT EXISTS shop_config (cfg_key TEXT PRIMARY KEY, cfg_value TEXT NOT NULL)");
|
||||
$stmt = $db->prepare("SELECT cfg_value FROM shop_config WHERE cfg_key = 'packmoney'");
|
||||
$stmt = $db->prepare("SELECT cfg_key, cfg_value FROM shop_config WHERE cfg_key IN ('packmoney','green_discount')");
|
||||
$result = $stmt->execute();
|
||||
$row = $result->fetchArray(SQLITE3_ASSOC);
|
||||
return $row ? (int)$row['cfg_value'] : 5;
|
||||
$map = ['packmoney' => 5, 'green_discount' => 2]; // 默认值
|
||||
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||
$map[$row['cfg_key']] = $row['cfg_value'];
|
||||
}
|
||||
return $map;
|
||||
} catch (Exception $e) {
|
||||
return 5;
|
||||
return ['packmoney' => 5, 'green_discount' => 2];
|
||||
}
|
||||
}
|
||||
function cashierGetGreenDiscount() {
|
||||
try {
|
||||
$db = getDB();
|
||||
$db->exec("CREATE TABLE IF NOT EXISTS shop_config (cfg_key TEXT PRIMARY KEY, cfg_value TEXT NOT NULL)");
|
||||
$stmt = $db->prepare("SELECT cfg_value FROM shop_config WHERE cfg_key = 'green_discount'");
|
||||
$result = $stmt->execute();
|
||||
$row = $result->fetchArray(SQLITE3_ASSOC);
|
||||
return $row ? (float)$row['cfg_value'] : 2;
|
||||
} catch (Exception $e) {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
$packMoney = cashierGetPackMoney();
|
||||
$greenDiscount = cashierGetGreenDiscount();
|
||||
$cfg = cashierGetConfig();
|
||||
$packMoney = (int)$cfg['packmoney'];
|
||||
$greenDiscount = (float)$cfg['green_discount'];
|
||||
|
||||
$loggedIn = isAdminLoggedIn() || isCashierLoggedIn();
|
||||
$initialRole = 'guest';
|
||||
|
||||
@@ -60,10 +60,10 @@ function getDB() {
|
||||
$db->enableExceptions(true);
|
||||
// WAL模式 + 长等待超时防止database is locked
|
||||
$db->exec('PRAGMA journal_mode=WAL');
|
||||
$db->exec('PRAGMA busy_timeout=60000'); // ★ 60秒等待(原30秒不够)
|
||||
$db->exec('PRAGMA busy_timeout=8000'); // ★ 8秒等待(原60秒太长,Java轮询锁时PHP会卡住近1分钟)
|
||||
$db->exec('PRAGMA synchronous=NORMAL');
|
||||
$db->exec('PRAGMA cache_size=-64000'); // 64MB 缓存
|
||||
$db->exec('PRAGMA wal_autocheckpoint=1000');
|
||||
$db->exec('PRAGMA wal_autocheckpoint=100');// ★ 更频繁checkpoint(原1000),更快释放WAL锁减少竞争
|
||||
|
||||
// ★ initTables/migrateDatabase加try-catch,防止初始化写入失败导致整个请求500
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user