mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
#1730 - Caching in RestfulService (merged from branches/2.2.0, r44881)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@44915 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
b5098645fc
commit
85d6566e32
@ -9,9 +9,12 @@ class RestfulService extends ViewableData {
|
|||||||
protected $queryString;
|
protected $queryString;
|
||||||
protected $rawXML;
|
protected $rawXML;
|
||||||
protected $errorTag;
|
protected $errorTag;
|
||||||
|
protected $checkErrors;
|
||||||
|
protected $cache_expire;
|
||||||
|
|
||||||
function __construct($base){
|
function __construct($base, $expiry=3600){
|
||||||
$this->baseURL = $base;
|
$this->baseURL = $base;
|
||||||
|
$this->cache_expire = $expiry;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -32,27 +35,46 @@ class RestfulService extends ViewableData {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function connect(){
|
function connect(){
|
||||||
$url = $this->constructURL();
|
$url = $this->constructURL(); // url for the request
|
||||||
|
|
||||||
$ch = curl_init();
|
// check for file exists in cache
|
||||||
$timeout = 5;
|
// set the cache directory
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
$cachedir = TEMP_FOLDER; // default silverstripe-cache
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
||||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
|
$cache_file = md5($url); // encoded name of cache file
|
||||||
$this->rawXML = curl_exec($ch);
|
$cache_path = $cachedir . "/$cache_file";
|
||||||
curl_close($ch);
|
|
||||||
|
if((@file_exists("$cache_path") && ((@filemtime($cache_path) + $this->cache_expire) > (time())))){
|
||||||
//Debug::show($url);
|
$this->rawXML = file_get_contents($cache_path);
|
||||||
|
} else {
|
||||||
//Try using file_get_contents if cURL is not installed in your system.
|
// not available in cache fetch from server
|
||||||
//$this->rawXML = file_get_contents($url);
|
$ch = curl_init();
|
||||||
if($this->rawXML != "")
|
$timeout = 5;
|
||||||
if($this->checkErrors == true)
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
return $this->errorCatch($this->rawXML);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||||
else
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
|
||||||
return $this->rawXML;
|
$this->rawXML = curl_exec($ch);
|
||||||
else
|
curl_close($ch);
|
||||||
user_error("Invalid Response (maybe your calling to wrong URL or server unavailable)", E_USER_ERROR);
|
|
||||||
|
// save the response in cache
|
||||||
|
$fp = @fopen($cache_path,"w+");
|
||||||
|
@fwrite($fp,$this->rawXML);
|
||||||
|
@fclose($fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try using file_get_contents if cURL is not installed in your system.
|
||||||
|
// $this->rawXML = file_get_contents($url);
|
||||||
|
|
||||||
|
// results returned - from cache / live
|
||||||
|
if($this->rawXML != ""){
|
||||||
|
if($this->checkErrors == true) {
|
||||||
|
return $this->errorCatch($this->rawXML);
|
||||||
|
} else {
|
||||||
|
return $this->rawXML;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
user_error("Invalid Response (maybe your calling to wrong URL or server unavailable)", E_USER_ERROR);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -202,4 +224,4 @@ class RestfulService extends ViewableData {
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
Loading…
Reference in New Issue
Block a user