/** * Get market price from Redis with database fallback * @param string $market Market code (e.g., 'OP', 'BUFF', 'DM', 'SP') * @param int $itemID Item ID * @param object $redis Redis connection * @param object $dbh Database connection * @return array|null Market price data or null */ function getMarketPrice($market, $itemID, $redis, $dbh) { // Try Redis first (fast cache) $cache_key = $market . '_' . $itemID; $data = json_decode($redis->get($cache_key), true); // If Redis has the data, return it if ($data && isset($data['amount'])) { return $data; } // Fallback to database // Map market codes to site names in pricelist.list table $site_map = [ 'OP' => 'opskins', 'BUFF' => 'buff', 'DM' => 'dmarket', 'SP' => 'skinport', 'SBAY' => 'shadowpay', 'WM' => 'whitemarket', 'CSM' => 'csmoney' ]; $site = isset($site_map[$market]) ? $site_map[$market] : strtolower($market); try { $stmt = $dbh->prepare(" SELECT price, market_name FROM pricelist.list WHERE site = ? AND item_id = ? ORDER BY last_updated DESC LIMIT 1 "); $stmt->execute([$site, $itemID]); $row = $stmt->fetch(PDO::FETCH_ASSOC); if ($row && $row['price']) { $data = [ 'amount' => $row['price'], 'market_name' => $row['market_name'] ]; // Cache it in Redis for next time (1 hour expiry) $redis->setex($cache_key, 3600, json_encode($data)); return $data; } } catch (Exception $e) { // If DB query fails, return null error_log("getMarketPrice DB fallback failed: " . $e->getMessage()); } return null; } Browse all CS2 and CS:GO Collections Skins - SteamAnalyst.com
SteamAnalyst.com