diff --git a/.env.example b/.env.example index 4885ffc..da17f7f 100644 --- a/.env.example +++ b/.env.example @@ -34,6 +34,12 @@ APP_STATSPASSWORD="test" STEAM_API_KEY="" +# Redis settings +REDIS_HOST=localhost +REDIS_PORT=6379 +REDIS_DATABASE=15 +REDIS_EXPIRATION=3600 + # Database settings DB_ENABLE=false DB_HOST=localhost diff --git a/app/modules/SteamApp.php b/app/modules/SteamApp.php index c3917b7..fb9a35a 100644 --- a/app/modules/SteamApp.php +++ b/app/modules/SteamApp.php @@ -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); diff --git a/app/modules/SteamGroup.php b/app/modules/SteamGroup.php index 58788b3..e504d54 100644 --- a/app/modules/SteamGroup.php +++ b/app/modules/SteamGroup.php @@ -19,6 +19,15 @@ class SteamGroup */ public static function querySteamData($group) { + // Generate cache key + $cacheKey = 'steam_group_' . $group; + + // Check if data is cached + $cachedData = self::getFromCache($cacheKey); + if ($cachedData !== false) { + return $cachedData; + } + $url = ''; if (is_numeric($group)) { @@ -42,9 +51,47 @@ class SteamGroup $result->members->in_game = intval($content->groupDetails->membersInGame->__toString()); $result->members->online = intval($content->groupDetails->membersOnline->__toString()); + // Store data in cache + self::setToCache($cacheKey, $result); + return $result; } return null; } + + /** + * 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')); + } } diff --git a/app/modules/SteamServer.php b/app/modules/SteamServer.php index 12e5e30..6f11bef 100644 --- a/app/modules/SteamServer.php +++ b/app/modules/SteamServer.php @@ -1,5 +1,7 @@ response->servers[0])) { - return $obj->response->servers[0]; + $serverData = $obj->response->servers[0]; + + // Store data in cache + self::setToCache($cacheKey, json_encode($serverData)); + + return $serverData; } 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')); + } } diff --git a/app/modules/SteamUser.php b/app/modules/SteamUser.php index 8ae2b8d..3acd2fe 100644 --- a/app/modules/SteamUser.php +++ b/app/modules/SteamUser.php @@ -1,5 +1,7 @@ response->players[0])) { - return $obj->response->players[0]; + $userData = $obj->response->players[0]; + + // Store data in cache + self::setToCache($cacheKey, json_encode($userData)); + + return $userData; } 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')); + } } diff --git a/app/modules/SteamWorkshop.php b/app/modules/SteamWorkshop.php index c9590f4..def4e91 100644 --- a/app/modules/SteamWorkshop.php +++ b/app/modules/SteamWorkshop.php @@ -1,5 +1,7 @@ response->result)) && ($obj->response->result) && (isset($obj->response->resultcount)) && ($obj->response->resultcount == 1)) { - $obj->response->publishedfiledetails[0]->creator_data = null; + $workshopData = $obj->response->publishedfiledetails[0]; - try { - $obj->response->publishedfiledetails[0]->creator_data = SteamUser::querySteamData(env('STEAM_API_KEY'), $obj->response->publishedfiledetails[0]->creator); - } catch (\Exception $e) { - } - - return $obj->response->publishedfiledetails[0]; + // Store data in cache + self::setToCache($cacheKey, json_encode($workshopData)); + + return $workshopData; } 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')); + } }