Skip to content

Commit

Permalink
Improve controller matching and include privateCache support
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesDPC committed Mar 16, 2022
1 parent 70680eb commit ef48b94
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
1 change: 1 addition & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ NSWDPC\Utilities\Cache\CacheHeaderConfiguration:
- 'SilverStripe\Security\Security'
- 'SilverStripe\ShareDraftContent\Controllers\ShareDraftController'
- 'SilverStripe\Assets\Storage\ProtectedFileController'
privateCache: []
45 changes: 35 additions & 10 deletions src/Extensions/CacheStateModificationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,26 @@ class CacheStateModificationExtension extends Extension {

public function onBeforeInit() {
$configuration = CacheHeaderConfiguration::config()->get('controllers');
if(!empty($configuration['privateCache'])) {
$this->setPrivateState($configuration['privateCache']);
}
if(!empty($configuration['disableCache'])) {
$this->setDisableState($configuration['disableCache']);
}
}

/**
* Match current controller against an array of controller names
* @param array controllers to check current controller against
*/
protected function matchController(array $controllers) : bool {
$controllerCheck = function($className, $k) use ($controllers) {
return ($this->owner instanceof $className);
};
$matches = array_filter($controllers, $controllerCheck, ARRAY_FILTER_USE_BOTH);
return !empty($matches);
}

/**
* Based on the controller and configuration, trigger a disabled cache state
* in the application and tell the {@link CacheHeaderProxyMiddleware} to honour that
Expand All @@ -28,19 +43,29 @@ protected function setDisableState(array $controllers) {
// none configured
return;
}
$cacheMiddleware = HTTPCacheControlMiddleware::singleton();
$controllerClass = get_class($this->owner);
// Handle exact match on the controller class
if(in_array($controllerClass, $controllers)) {

if($match = $this->matchController($controllers)) {
$cacheMiddleware = HTTPCacheControlMiddleware::singleton();
$cacheMiddleware->disableCache(true)->useAppliedState();
}
}

/**
* Based on the controller and configuration, trigger a private cache state with
* a max-age of 0 in the application
* and tell the {@link CacheHeaderProxyMiddleware} to honour that
* See config.yml for the configured list of controllers
* @param array $controllers controllers that should have a private cache
*/
protected function setPrivateState(array $controllers) {
if(empty($controllers)) {
// none configured
return;
}
// Include subclasses of the controller class
foreach($controllers as $className) {
if(is_subclass_of($controllerClass, $className, true)) {
$cacheMiddleware->disableCache(true)->useAppliedState();
return;
}

if($match = $this->matchController($controllers)) {
$cacheMiddleware = HTTPCacheControlMiddleware::singleton();
$cacheMiddleware->privateCache(true)->useAppliedState();
}
}
}
2 changes: 2 additions & 0 deletions src/Models/CacheHeaderConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ class CacheHeaderConfiguration {
private static $no_store = null;
private static $no_cache = null;

private static $controllers = [];

}

0 comments on commit ef48b94

Please sign in to comment.