base redis commit

This commit is contained in:
2024-03-31 17:17:40 -07:00
parent eab0025735
commit 141665dbf1
6 changed files with 263 additions and 21 deletions

View File

@ -19,6 +19,13 @@ class SteamApp
*/
public static function querySteamData($appid, $lang)
{
// Check if data is cached
$cacheKey = 'steam_' . $appid . '_' . $lang;
$cachedData = self::getFromCache($cacheKey);
if ($cachedData !== false) {
return $cachedData;
}
$url = self::STEAM_ENDPOINT . "?appids={$appid}&l={$lang}";
$handle = curl_init($url);
@ -52,12 +59,50 @@ class SteamApp
} catch (\Exception $e) {
}
// Store data in cache
self::setToCache($cacheKey, $obj->$appid->data);
return $obj->$appid->data;
}
throw new \Exception('Invalid data response');
}
/**
* Get data from Redis cache
*
* @param $key
* @return mixed|bool
*/
private static function getFromCache($key)
{
$redis = new Redis();
$redis->connect(env('REDIS_HOST'), env('REDIS_PORT'));
$redis->select(env('REDIS_DATABASE')); // Selecting Redis database index 5
$cachedData = $redis->get($key);
if ($cachedData !== false) {
return json_decode($cachedData);
}
return false;
}
/**
* Set data to Redis cache
*
* @param $key
* @param $value
*/
private static function setToCache($key, $value)
{
$redis = new Redis();
$redis->connect(env('REDIS_HOST'), env('REDIS_PORT'));
$redis->select(env('REDIS_DATABASE')); // Selecting Redis database index 5
$redis->set($key, json_encode($value), env('REDIS_EXPIRATION'));
}
/**
* Query Steam product player/user count
*
@ -78,7 +123,7 @@ class SteamApp
$response = curl_exec($handle);
if(curl_error($handle) !== '') {
throw new \Exception('cURL error occured');
throw new \Exception('cURL error occurred');
}
curl_close($handle);
@ -111,7 +156,7 @@ class SteamApp
$response = curl_exec($handle);
if(curl_error($handle) !== '') {
throw new \Exception('cURL error occured');
throw new \Exception('cURL error occurred');
}
curl_close($handle);