fetchLiveVaultBalance: split production/test cache paths so production caching works
This commit is contained in:
+33
-21
@@ -352,39 +352,51 @@ function backfillRunningBalances($pdo, $user) {
|
|||||||
* @return int|null The vault amount in pennies, or null on failure.
|
* @return int|null The vault amount in pennies, or null on failure.
|
||||||
*/
|
*/
|
||||||
function fetchLiveVaultBalance($user) {
|
function fetchLiveVaultBalance($user) {
|
||||||
static $cache = null;
|
static $prodCache = []; // production: hook is null, plain per-request array
|
||||||
|
static $testCache = null; // test: keyed by hook closure (weakly held)
|
||||||
if ($cache === null) {
|
|
||||||
$cache = new WeakMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
$hook = $GLOBALS['liveBalanceTestHook'] ?? null;
|
|
||||||
$cacheKey = $hook ?? true;
|
|
||||||
|
|
||||||
if (isset($cache[$cacheKey]) && array_key_exists($user, $cache[$cacheKey])) {
|
|
||||||
return $cache[$cacheKey][$user];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!array_key_exists($user, USER_KEYS)) {
|
if (!array_key_exists($user, USER_KEYS)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = 'https://api.torn.com/v2/user?selections=money';
|
$hook = $GLOBALS['liveBalanceTestHook'] ?? null;
|
||||||
|
|
||||||
|
if ($hook === null) {
|
||||||
|
// Production path: plain array cache.
|
||||||
|
if (array_key_exists($user, $prodCache)) {
|
||||||
|
return $prodCache[$user];
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
$responseData = $hook !== null
|
$responseData = executeApiCall('https://api.torn.com/v2/user?selections=money', USER_KEYS[$user]);
|
||||||
? $hook($url, USER_KEYS[$user])
|
|
||||||
: executeApiCall($url, USER_KEYS[$user]);
|
|
||||||
|
|
||||||
if (!isset($responseData['money']['vault'])) {
|
if (!isset($responseData['money']['vault'])) {
|
||||||
|
$prodCache[$user] = null;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
$prodCache[$user] = (int)$responseData['money']['vault'];
|
||||||
|
return $prodCache[$user];
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$prodCache[$user] = null;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!isset($cache[$cacheKey])) {
|
// Test path: key by hook identity so test scenarios get fresh fetches.
|
||||||
$cache[$cacheKey] = [];
|
if ($testCache === null) {
|
||||||
|
$testCache = new \WeakMap();
|
||||||
}
|
}
|
||||||
$cache[$cacheKey][$user] = (int)$responseData['money']['vault'];
|
if (isset($testCache[$hook]) && array_key_exists($user, $testCache[$hook])) {
|
||||||
return $cache[$cacheKey][$user];
|
return $testCache[$hook][$user];
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$responseData = $hook('https://api.torn.com/v2/user?selections=money', USER_KEYS[$user]);
|
||||||
|
if (!isset($responseData['money']['vault'])) {
|
||||||
|
$testCache[$hook] = [$user => null];
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$testCache[$hook] = [$user => (int)$responseData['money']['vault']];
|
||||||
|
return $testCache[$hook][$user];
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
$testCache[$hook] = [$user => null];
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user