Better cache key generation

All of the arguments supplied to the request function can impact what is
returned by a restful service.

This takes account of that and makes the cache key more specific,
including basic auth details, so we don't rely on *just* the absolute
URL for caching.
This commit is contained in:
Daniel Hensby 2013-01-30 16:39:18 +00:00
parent 3457f43839
commit 7c189731e3

View File

@ -115,7 +115,16 @@ class RestfulService extends ViewableData {
assert(in_array($method, array('GET','POST','PUT','DELETE','HEAD','OPTIONS')));
$cachedir = TEMP_FOLDER; // Default silverstripe cache
$cache_file = md5($url); // Encoded name of cache file
//use var export on potentially nested arrays
$cache_file_items = array(
$subURL,
$method,
var_export($data, true),
var_export(array_merge((array)$this->customHeaders, (array)$headers), true),
var_export($curlOptions, true),
"$this->authUsername:$this->authPassword"
);
$cache_file = md5(implode('-', $cache_file_items)); // Encoded name of cache file
$cache_path = $cachedir."/xmlresponse_$cache_file";
// Check for unexpired cached feed (unless flush is set)
@ -212,13 +221,6 @@ class RestfulService extends ViewableData {
$responseBody = curl_exec($ch);
$curlError = curl_error($ch);
// Problem verifying the server SSL certificate; just ignore it as it's not mandatory
if(strpos($curlError,'14090086') !== false) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$responseBody = curl_exec($ch);
$curlError = curl_error($ch);
}
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($curlError !== '' || $statusCode == 0) $statusCode = 500;