Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New structure for static data limit and fix eune server in readme file #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@ interface CacheInterface {

/**
* @param string $key Checks whether or not the cache contains unexpired data for the specified key
* @param boolean $forceClear force the data to be refreshed - new structure static data limit
* @return bool
*/
public function has($key);
public function has($key, $forceClear);

/**
* @param string $key Gets data for specified key
* @param boolean $forceClear force the data to be refreshed - new structure static data limit
* @return string|null Returns null if the cached item doesn't exist or has expired
*/
public function get($key);
public function get($key, $forceClear);

/**
* @param string $key
* @param $data
* @param int $ttl Time in seconds before the data becomes expired
* @param boolean $static if the data is static or not - new structure static data limit
* @return mixed
*/
public function put($key, $data, $ttl = 0);
public function put($key, $data, $ttl = 0, $static);

}
24 changes: 14 additions & 10 deletions FileSystemCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,30 @@ public function __construct($directory)

/**
* @param string $key Check if the cache contains data for the specified key
* @param boolean $forceClear force the data to be refreshed - new structure static data limit
* @return bool
*/
public function has($key)
public function has($key, $forceClear)
{
if ( ! file_exists($this->getPath($key)))
return false;

$entry = $this->load($key);
return !$this->expired($entry);
return !$this->expired($entry, $forceClear);
}

/**
* @param string $key Gets data for specified key
* @param boolean $forceClear force the data to be refreshed - new structure static data limit
* @return string|null
*/
public function get($key)
public function get($key, $forceClear)
{
$entry = $this->load($key);

$data = null;

if ( ! $this->expired($entry))
if ( ! $this->expired($entry, $forceClear))
$data = $entry->data;

return $data;
Expand All @@ -52,24 +54,26 @@ public function get($key)
* @param string $key
* @param $data
* @param int $ttl Time for the data to live inside the cache
* @param boolean $static if the data is static or not - new structure static data limit
* @return mixed
*/
public function put($key, $data, $ttl = 0)
public function put($key, $data, $ttl = 0, $static)
{
$this->store($key, $data, $ttl, time());
$this->store($key, $data, $ttl, time(), $static);
}

private function load($key)
{
return json_decode(file_get_contents($this->getPath($key)));
}

private function store($key, $data, $ttl, $createdAt)
private function store($key, $data, $ttl, $createdAt, $static)
{
$entry = array(
'createdAt' => $createdAt,
'ttl' => $ttl,
'data' => $data
'data' => $data,
'static' => $static
);

file_put_contents($this->getPath($key), json_encode($entry));
Expand All @@ -80,9 +84,9 @@ private function getPath($key)
return $this->directory . $this->hash($key);
}

private function expired($entry)
private function expired($entry, $forceClear)
{
return $entry === null || time() >= ($entry->createdAt + $entry->ttl);
return $entry === null || (time() >= ($entry->createdAt + $entry->ttl) && !$entry->static) || $forceClear;
}

private function hash($key)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Getting Started
- Replace INSERT_API_KEY_HERE
- Create folder called 'cache' wherever the script is (make sure it's writeable by php-riot-api)
- Create an instance of riotapi - $instance = new riotapi($platform);
- $platform can be na1, euw1, eune1, br1, ru, kr, oc1, la1, la2, jp1, pbe1, tr1 (br/tr only can call getLeague() and getTeam() functions)
- $platform can be na1, euw1, eun1, br1, ru, kr, oc1, la1, la2, jp1, pbe1, tr1 (br/tr only can call getLeague() and getTeam() functions)
- Make Calls to the functions listed below and receive JSON data
- Caching is done locally, instantiate php-riot-api with "new riotapi('na1', new FileSystemCache('cache/'));" to create a cache in the subfolder 'cache'
- DECODE_ENABLED is true by default. If you want your returns to be pure JSON and not an associative array, set it to false
Expand Down
81 changes: 42 additions & 39 deletions php-riot-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ class riotapi {

// Rate limit for 10 minutes
const LONG_LIMIT_INTERVAL = 600;
const RATE_LIMIT_LONG = 500;
const RATE_LIMIT_LONG = 180000;

// Rate limit for 10 seconds'
const SHORT_LIMIT_INTERVAL = 10;
const RATE_LIMIT_SHORT = 10;
const RATE_LIMIT_SHORT = 3000;

// Cache variables
const CACHE_LIFETIME_MINUTES = 60;
const CACHE_LIFETIME_MINUTES = 30;
private $cache;

private $PLATFORM;
Expand Down Expand Up @@ -131,17 +131,25 @@ public function getCurrentGame($id){
//$call is what is asked (champion, item...)
//$id the id for a specific item, champion. Set at null to get all champions, items...
//$params is the string you get after the "?"
// getStatic("champions", 1, "locale=fr_FR&tags=image&tags=spells") will get you image data and spells data in French from champion whose ID is 1, here Annie.
public function getStatic($call, $id = null, $params = null) {
// getStatic("champions", 1, "locale=fr_FR&tags=image&tags=spells") will get you image data and spells data in French from champion whose ID is 1, here Annie.
// $fromAPI is forcing the data to be refreshed - new structure static data limit
public function getStatic($call, $id = null, $params = null, $fromAPI = false) {

$call = self::API_URL_STATIC_3 . $call;

if( $id !=null)
$call.="/" . $id;

if( $params !=null)
$call.="?" . $params;

return $this->request($call, true);

$data = $this->request($call, true, $fromAPI);

if( $id == null)
return $data;

foreach ($data['data'] as $key => $value) {
if($value['id'] == $id)
return $data['data'][$key];
}

}

//New to my knowledge. Returns match details.
Expand Down Expand Up @@ -245,28 +253,28 @@ public function getMaster($queue = "RANKED_SOLO_5x5") {

//returns a summoner's id
public function getSummonerId($name) {
$name = strtolower($name);
$summoner = $this->getSummonerByName($name);
if (self::DECODE_ENABLED) {
return $summoner['id'];
}
else {
$summoner = json_decode($summoner, true);
return $summoner['id'];
}
$name = strtolower($name);
$summoner = $this->getSummonerByName($name);
if (self::DECODE_ENABLED) {
return $summoner['id'];
}
else {
$summoner = json_decode($summoner, true);
return $summoner['id'];
}
}

//returns an account id
public function getSummonerAccountId($name) {
$name = strtolower($name);
$summoner = $this->getSummonerByName($name);
if (self::DECODE_ENABLED) {
return $summoner['accountId'];
}
else {
$summoner = json_decode($summoner, true);
return $summoner['accountId'];
}
$name = strtolower($name);
$summoner = $this->getSummonerByName($name);
if (self::DECODE_ENABLED) {
return $summoner['accountId'];
}
else {
$summoner = json_decode($summoner, true);
return $summoner['accountId'];
}
}

//Returns summoner info given summoner id or account id.
Expand Down Expand Up @@ -393,14 +401,13 @@ private function updateLimitQueue($queue, $interval, $call_limit){
$queue->enqueue(time());
}

private function request($call, $static = false) {
//format the full URL

private function request($call, $static = false, $forceClear = false) {
//format the full URL
$url = $this->format_url($call);
//echo $url;
//caching
if($this->cache !== null && $this->cache->has($url)){
$result = $this->cache->get($url);
if($this->cache !== null && $this->cache->has($url, $forceClear)){
$result = $this->cache->get($url, $forceClear);
} else {
// Check rate-limiting queues if this is not a static call.
if (!$static) {
Expand All @@ -420,10 +427,9 @@ private function request($call, $static = false) {
$this->responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);


if($this->responseCode == 200) {
if($this->cache !== null){
$this->cache->put($url, $result, self::CACHE_LIFETIME_MINUTES * 60);
$this->cache->put($url, $result, self::CACHE_LIFETIME_MINUTES * 60, $static);
}
} else {
throw new Exception(self::$errorCodes[$this->responseCode]);
Expand Down Expand Up @@ -477,7 +483,6 @@ private function format_url($call){
return str_replace('{platform}', $this->PLATFORM, $call);
}


public function getLastResponseCode(){
return $this->responseCode;
}
Expand All @@ -488,7 +493,6 @@ public function debug($message) {
echo "</pre>";
}


public function setPlatform($platform) {
$this->PLATFORM = $platform;
}
Expand Down Expand Up @@ -522,8 +526,7 @@ private function multiple_threads_request($nodes){
}
curl_multi_close($mh);
return $res;
}

}

}

Expand Down