mirror of
https://github.com/SophiaAtkinson/steamwidgets-web.git
synced 2025-06-27 14:37: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);
|
||||
|
@ -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'));
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Redis;
|
||||
|
||||
/**
|
||||
* Class SteamServer
|
||||
*
|
||||
@ -8,17 +10,25 @@
|
||||
class SteamServer
|
||||
{
|
||||
const STEAM_ENDPOINT = 'https://api.steampowered.com/IGameServersService/GetServerList/v1/';
|
||||
|
||||
/**
|
||||
* Query item data from Steam
|
||||
*
|
||||
* @param $appid
|
||||
* @param $lang
|
||||
* @param $key
|
||||
* @param $addr
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
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}";
|
||||
|
||||
$handle = curl_init($url);
|
||||
@ -30,7 +40,7 @@ class SteamServer
|
||||
$response = curl_exec($handle);
|
||||
|
||||
if(curl_error($handle) !== '') {
|
||||
throw new \Exception('cURL error occured');
|
||||
throw new \Exception('cURL error occurred');
|
||||
}
|
||||
|
||||
curl_close($handle);
|
||||
@ -38,9 +48,49 @@ class SteamServer
|
||||
$obj = json_decode($response);
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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'));
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Redis;
|
||||
|
||||
/**
|
||||
* Class SteamUser
|
||||
*
|
||||
@ -12,13 +14,22 @@ class SteamUser
|
||||
/**
|
||||
* Query item data from Steam
|
||||
*
|
||||
* @param $appid
|
||||
* @param $lang
|
||||
* @param $key
|
||||
* @param $steamid
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
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}";
|
||||
|
||||
$handle = curl_init($url);
|
||||
@ -30,7 +41,7 @@ class SteamUser
|
||||
$response = curl_exec($handle);
|
||||
|
||||
if(curl_error($handle) !== '') {
|
||||
throw new \Exception('cURL error occured');
|
||||
throw new \Exception('cURL error occurred');
|
||||
}
|
||||
|
||||
curl_close($handle);
|
||||
@ -38,9 +49,49 @@ class SteamUser
|
||||
$obj = json_decode($response);
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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'));
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Redis;
|
||||
|
||||
/**
|
||||
* Class SteamWorkshop
|
||||
*
|
||||
@ -12,13 +14,21 @@ class SteamWorkshop
|
||||
/**
|
||||
* Query item data from Steam Workshop item
|
||||
*
|
||||
* @param $appid
|
||||
* @param $lang
|
||||
* @param $itemid
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
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);
|
||||
|
||||
curl_setopt($handle, CURLOPT_HEADER, false);
|
||||
@ -30,7 +40,7 @@ class SteamWorkshop
|
||||
$response = curl_exec($handle);
|
||||
|
||||
if(curl_error($handle) !== '') {
|
||||
throw new \Exception('cURL error occured');
|
||||
throw new \Exception('cURL error occurred');
|
||||
}
|
||||
|
||||
curl_close($handle);
|
||||
@ -38,16 +48,49 @@ class SteamWorkshop
|
||||
$obj = json_decode($response);
|
||||
|
||||
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 {
|
||||
$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'));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user