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

@ -34,6 +34,12 @@ APP_STATSPASSWORD="test"
STEAM_API_KEY="" STEAM_API_KEY=""
# Redis settings
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_DATABASE=15
REDIS_EXPIRATION=3600
# Database settings # Database settings
DB_ENABLE=false DB_ENABLE=false
DB_HOST=localhost DB_HOST=localhost

View File

@ -19,6 +19,13 @@ class SteamApp
*/ */
public static function querySteamData($appid, $lang) 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}"; $url = self::STEAM_ENDPOINT . "?appids={$appid}&l={$lang}";
$handle = curl_init($url); $handle = curl_init($url);
@ -52,12 +59,50 @@ class SteamApp
} catch (\Exception $e) { } catch (\Exception $e) {
} }
// Store data in cache
self::setToCache($cacheKey, $obj->$appid->data);
return $obj->$appid->data; return $obj->$appid->data;
} }
throw new \Exception('Invalid data response'); 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 * Query Steam product player/user count
* *
@ -78,7 +123,7 @@ class SteamApp
$response = curl_exec($handle); $response = curl_exec($handle);
if(curl_error($handle) !== '') { if(curl_error($handle) !== '') {
throw new \Exception('cURL error occured'); throw new \Exception('cURL error occurred');
} }
curl_close($handle); curl_close($handle);
@ -111,7 +156,7 @@ class SteamApp
$response = curl_exec($handle); $response = curl_exec($handle);
if(curl_error($handle) !== '') { if(curl_error($handle) !== '') {
throw new \Exception('cURL error occured'); throw new \Exception('cURL error occurred');
} }
curl_close($handle); curl_close($handle);

View File

@ -19,6 +19,15 @@ class SteamGroup
*/ */
public static function querySteamData($group) 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 = ''; $url = '';
if (is_numeric($group)) { if (is_numeric($group)) {
@ -42,9 +51,47 @@ class SteamGroup
$result->members->in_game = intval($content->groupDetails->membersInGame->__toString()); $result->members->in_game = intval($content->groupDetails->membersInGame->__toString());
$result->members->online = intval($content->groupDetails->membersOnline->__toString()); $result->members->online = intval($content->groupDetails->membersOnline->__toString());
// Store data in cache
self::setToCache($cacheKey, $result);
return $result; return $result;
} }
return null; 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'));
}
} }

View File

@ -1,5 +1,7 @@
<?php <?php
use Redis;
/** /**
* Class SteamServer * Class SteamServer
* *
@ -8,17 +10,25 @@
class SteamServer class SteamServer
{ {
const STEAM_ENDPOINT = 'https://api.steampowered.com/IGameServersService/GetServerList/v1/'; const STEAM_ENDPOINT = 'https://api.steampowered.com/IGameServersService/GetServerList/v1/';
/** /**
* Query item data from Steam * Query item data from Steam
* *
* @param $appid * @param $key
* @param $lang * @param $addr
* @return mixed * @return mixed
* @throws \Exception * @throws \Exception
*/ */
public static function querySteamData($key, $addr) public static function querySteamData($key, $addr)
{ {
// Generate cache key
$cacheKey = 'steam_server_' . md5($key . $addr);
// Check if data is cached
$cachedData = self::getFromCache($cacheKey);
if ($cachedData !== false) {
return json_decode($cachedData);
}
$url = self::STEAM_ENDPOINT . "?key={$key}&filter=addr\\{$addr}"; $url = self::STEAM_ENDPOINT . "?key={$key}&filter=addr\\{$addr}";
$handle = curl_init($url); $handle = curl_init($url);
@ -30,7 +40,7 @@ class SteamServer
$response = curl_exec($handle); $response = curl_exec($handle);
if(curl_error($handle) !== '') { if(curl_error($handle) !== '') {
throw new \Exception('cURL error occured'); throw new \Exception('cURL error occurred');
} }
curl_close($handle); curl_close($handle);
@ -38,9 +48,49 @@ class SteamServer
$obj = json_decode($response); $obj = json_decode($response);
if (isset($obj->response->servers[0])) { if (isset($obj->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'); 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'));
}
} }

View File

@ -1,5 +1,7 @@
<?php <?php
use Redis;
/** /**
* Class SteamUser * Class SteamUser
* *
@ -12,13 +14,22 @@ class SteamUser
/** /**
* Query item data from Steam * Query item data from Steam
* *
* @param $appid * @param $key
* @param $lang * @param $steamid
* @return mixed * @return mixed
* @throws \Exception * @throws \Exception
*/ */
public static function querySteamData($key, $steamid) public static function querySteamData($key, $steamid)
{ {
// Generate cache key
$cacheKey = 'steam_user_' . $steamid;
// Check if data is cached
$cachedData = self::getFromCache($cacheKey);
if ($cachedData !== false) {
return json_decode($cachedData);
}
$url = self::STEAM_ENDPOINT . "?key={$key}&steamids={$steamid}"; $url = self::STEAM_ENDPOINT . "?key={$key}&steamids={$steamid}";
$handle = curl_init($url); $handle = curl_init($url);
@ -30,7 +41,7 @@ class SteamUser
$response = curl_exec($handle); $response = curl_exec($handle);
if(curl_error($handle) !== '') { if(curl_error($handle) !== '') {
throw new \Exception('cURL error occured'); throw new \Exception('cURL error occurred');
} }
curl_close($handle); curl_close($handle);
@ -38,9 +49,49 @@ class SteamUser
$obj = json_decode($response); $obj = json_decode($response);
if (isset($obj->response->players[0])) { if (isset($obj->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'); 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'));
}
} }

View File

@ -1,5 +1,7 @@
<?php <?php
use Redis;
/** /**
* Class SteamWorkshop * Class SteamWorkshop
* *
@ -12,13 +14,21 @@ class SteamWorkshop
/** /**
* Query item data from Steam Workshop item * Query item data from Steam Workshop item
* *
* @param $appid * @param $itemid
* @param $lang
* @return mixed * @return mixed
* @throws \Exception * @throws \Exception
*/ */
public static function querySteamData($itemid) public static function querySteamData($itemid)
{ {
// Generate cache key
$cacheKey = 'steam_workshop_' . $itemid;
// Check if data is cached
$cachedData = self::getFromCache($cacheKey);
if ($cachedData !== false) {
return json_decode($cachedData);
}
$handle = curl_init(self::STEAM_ENDPOINT); $handle = curl_init(self::STEAM_ENDPOINT);
curl_setopt($handle, CURLOPT_HEADER, false); curl_setopt($handle, CURLOPT_HEADER, false);
@ -30,7 +40,7 @@ class SteamWorkshop
$response = curl_exec($handle); $response = curl_exec($handle);
if(curl_error($handle) !== '') { if(curl_error($handle) !== '') {
throw new \Exception('cURL error occured'); throw new \Exception('cURL error occurred');
} }
curl_close($handle); curl_close($handle);
@ -38,16 +48,49 @@ class SteamWorkshop
$obj = json_decode($response); $obj = json_decode($response);
if ((isset($obj->response->result)) && ($obj->response->result) && (isset($obj->response->resultcount)) && ($obj->response->resultcount == 1)) { if ((isset($obj->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 { // Store data in cache
$obj->response->publishedfiledetails[0]->creator_data = SteamUser::querySteamData(env('STEAM_API_KEY'), $obj->response->publishedfiledetails[0]->creator); self::setToCache($cacheKey, json_encode($workshopData));
} catch (\Exception $e) {
}
return $obj->response->publishedfiledetails[0]; return $workshopData;
} }
throw new \Exception('Invalid data response'); 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'));
}
} }