Skip to content

Commit

Permalink
Merge pull request #30 from jonom/fixes
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
Damian Mooyman committed Nov 22, 2015
2 parents 4da9338 + 819bd03 commit 1970e00
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
17 changes: 14 additions & 3 deletions _config/dynamiccache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,21 @@ DynamicCache:
optInHeader: null
# If a header should be used to explicitly disable caching for a cache, set the
# regular expression here which will be used to match the specified header
optOutHeader: '/(^X\-DynamicCache\-OptOut)|(^Location)/'
optOutHeader: '/(^X\-DynamicCache\-OptOut)/'
# Header to use by the module attempting to opt out
optOutHeaderString: 'X-DynamicCache-OptOut: true'
# Status codes that should be cached
optInResponseCodes:
- 200
- 203
- 204
- 205
- 300
- 301
- 404
- 410
# Status codes that should not be cached (overrides optInResponseCodes)
optOutResponseCodes: null
# Header prefix to use for reporting cache results
responseHeader: 'X-DynamicCache'
# If caching should be limited only to specified urls set the regular expression
Expand All @@ -31,11 +43,10 @@ DynamicCache:
# Determines which headers should also be cached. X-Include-CSS and other relevant
# headers can be essential in instructing the front end to include specific
# resource files
cacheHeaders: '/^(X\-)|(Cache\-Control)|(Etag)|(Expires)|(Last\-Modified)/i'
cacheHeaders: '/^(X\-)|(Cache\-Control)|(Etag)|(Expires)|(Last\-Modified)|(Location)/i'
# If you wish to override the cache configuration, then change this to another
# backend, and initialise a new SS_Cache backend in your _config file
cacheBackend: 'DynamicCache'
# Specify page types to skip caching for
ignoredPages:
- ErrorPage
- UserDefinedForm
24 changes: 17 additions & 7 deletions code/DynamicCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ protected function getCacheKey($url) {
}

// Segment by url
$url = trim($url, '/');
$fragments[] = $url ? $url : 'home';
$fragments[] = trim($url, '/');

// Extend
$this->extend('updateCacheKeyFragments', $fragments);
Expand All @@ -188,7 +187,9 @@ protected function presentCachedResult($cachedValue) {
// Check for empty cache
if(empty($cachedValue)) return false;
$deserialisedValue = unserialize($cachedValue);
if(empty($deserialisedValue['content'])) return false;

// Set response code
http_response_code($deserialisedValue['response_code']);

// Present cached headers
foreach($deserialisedValue['headers'] as $header) {
Expand Down Expand Up @@ -220,9 +221,10 @@ protected function presentCachedResult($cachedValue) {
* @param array $headers Headers to cache
* @param string $cacheKey Key to cache this page under
*/
protected function cacheResult($cache, $result, $headers, $cacheKey) {
protected function cacheResult($cache, $result, $headers, $cacheKey, $responseCode) {
$cache->save(serialize(array(
'headers' => $headers,
'response_code' => $responseCode,
'content' => $result
)), $cacheKey);
}
Expand Down Expand Up @@ -302,9 +304,17 @@ public function run($url) {
$this->yieldControl();
$headers = headers_list();
$result = ob_get_flush();
$responseCode = http_response_code();

// Skip blank copy
if(empty($result)) return;
// Skip blank copy unless redirecting
$locationHeaderMatches = preg_grep('/^Location/i', $headers);
if(empty($result) && empty($locationHeaderMatches)) return;

// Skip excluded status codes
$optInResponseCodes = self::config()->optInResponseCodes;
$optOutResponseCodes = self::config()->optOutResponseCodes;
if (is_array($optInResponseCodes) && !in_array($responseCode, $optInResponseCodes)) return;
if (is_array($optOutResponseCodes) && in_array($responseCode, $optInResponseCodes)) return;

// Check if any headers match the specified rules forbidding caching
if(!$this->headersAllowCaching($headers)) return;
Expand All @@ -314,6 +324,6 @@ public function run($url) {
$saveHeaders = $this->getCacheableHeaders($headers);

// Save data along with sent headers
$this->cacheResult($cache, $result, $saveHeaders, $cacheKey);
$this->cacheResult($cache, $result, $saveHeaders, $cacheKey, $responseCode);
}
}

0 comments on commit 1970e00

Please sign in to comment.