/** * The constructor for the BCMAPICache class. * @access Public * @since 1.0.0 * @param string [$type] The type of caching method to use, either 'file' or 'memcached' * @param int [$time] How many seconds until cache files are considered cold * @param string [$location] The absolute path of the cache directory (file) or host (memcached) * @param string [$extension] The file extension for cache items (file only) * @param int [$port] The port to use (Memcached only) */ public function __construct($type = 'file', $time = 600, $location, $extension = '.c', $port = 11211) { if (strtolower($type) == 'file') { $type = 'file'; } else { if (strtolower($type) == 'memcache' || strtolower($type) == 'memcached') { $type = 'memcached'; $memcached = new Memcached(); $memcached->addServer($location, $port); self::$memcached = $memcached; } else { $type = FALSE; } } self::$extension = $extension; self::$location = $location; self::$port = $port; self::$time = $time; self::$type = $type; }
/** * Retrieves API data from provided URL. * @access Private * @since 0.1.0 * @param string [$url] The complete API request URL * @return object An object containing all API return data */ private function getData($url) { if (class_exists('BCMAPICache')) { $cache = BCMAPICache::get($url); if ($cache !== FALSE) { $response_object = json_decode($cache); if (isset($response_object->items)) { $data = $response_object->items; } else { $data = $response_object; } $this->page_number = isset($response_object->page_number) ? $response_object->page_number : NULL; $this->page_size = isset($response_object->page_size) ? $response_object->page_size : NULL; $this->total_count = isset($response_object->total_count) ? $response_object->total_count : NULL; return $data; } } $this->timeout_current++; if (!isset($this->token_read)) { throw new BCMAPITokenError($this, self::ERROR_READ_TOKEN_NOT_PROVIDED); } $response = $this->curlRequest($url, TRUE); if ($response && $response != 'NULL') { $response_object = json_decode(preg_replace('/[[:cntrl:]]/u', '', $response)); if (isset($response_object->error)) { if ($this->timeout_retry && $response_object->code == 103 && $this->timeout_current < $this->timeout_attempts) { if ($this->timeout_delay > 0) { if ($this->timeout_delay < 1) { usleep($this->timeout_delay * 1000000); } else { sleep($this->timeout_delay); } } return $this->getData($url); } else { throw new BCMAPIApiError($this, self::ERROR_API_ERROR, $response_object); } } else { if (class_exists('BCMAPICache')) { $cache = BCMAPICache::set($url, $response_object); } if (isset($response_object->items)) { $data = $response_object->items; } else { $data = $response_object; } $this->page_number = isset($response_object->page_number) ? $response_object->page_number : NULL; $this->page_size = isset($response_object->page_size) ? $response_object->page_size : NULL; $this->total_count = isset($response_object->total_count) ? $response_object->total_count : NULL; return $data; } } else { throw new BCMAPIApiError($this, self::ERROR_API_ERROR); } }