diff --git a/CacheInterface.php b/CacheInterface.php index 9bcf129..64e3977 100644 --- a/CacheInterface.php +++ b/CacheInterface.php @@ -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); } \ No newline at end of file diff --git a/FileSystemCache.php b/FileSystemCache.php index d6202c5..aa933da 100644 --- a/FileSystemCache.php +++ b/FileSystemCache.php @@ -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; @@ -52,11 +54,12 @@ 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) @@ -64,12 +67,13 @@ 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)); @@ -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) diff --git a/README.md b/README.md index fb0d25c..bfca9f9 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/php-riot-api.php b/php-riot-api.php index 9349acc..13a892e 100644 --- a/php-riot-api.php +++ b/php-riot-api.php @@ -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; @@ -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. @@ -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. @@ -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) { @@ -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]); @@ -477,7 +483,6 @@ private function format_url($call){ return str_replace('{platform}', $this->PLATFORM, $call); } - public function getLastResponseCode(){ return $this->responseCode; } @@ -488,7 +493,6 @@ public function debug($message) { echo ""; } - public function setPlatform($platform) { $this->PLATFORM = $platform; } @@ -522,8 +526,7 @@ private function multiple_threads_request($nodes){ } curl_multi_close($mh); return $res; -} - + } }