mirror of
https://github.com/SophiaAtkinson/steamwidgets-web.git
synced 2025-06-27 10:17:41 -07:00
base redis commit
This commit is contained in:
@ -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);
|
||||
|
Reference in New Issue
Block a user