Skip to content

Commit

Permalink
Add when config to component
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Apr 11, 2020
1 parent edb7d6c commit 2654c0e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
25 changes: 13 additions & 12 deletions docs/Full-page.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,7 @@ public function middleware($middlewareQueue) {
```

It by default only gets invoked for GET requests, as per HTTP spec for caching (POST requests should never be cached).
If you need to further specify it, you can the `'when'` config and pass a callable:

```php
->add(new CacheMiddleware([
'when' => function (ServerRequest $request) {
return !$request->is('ajax');
},
]))
}
```
In this case we also exlude AJAX requests.
If you need to further specify it, see then `'when'` config and pass a callable to the component and the middleware.

## Usage
Once the Middleware is loaded, you need to add the component to the controllers you want to make cache-able:
Expand All @@ -58,6 +48,17 @@ The component creates the cache file, the dispatcher on the next request will di
as the file modification date is within the allowed range.
Once the file gets too old it will be cleaned out, a real action will be called and a fresh cache file will be created.

```php
$this->loadComponent('Cache.Cache', [
'when' => function (ServerRequest $request) {
return !$request->is('ajax');
},
])
}
```
In this case we also exlude AJAX requests.


### Global Configuration
The `CACHE` constant can be adjusted in your bootstrap and defaults to `tmp/cache/views/`.

Expand Down Expand Up @@ -118,7 +119,7 @@ The dispatcher cannot know if cache files of some non-public actions are request
## TODOS
- Limit filename length to around 200 (as it includes query strings) and add md5 hashsum instead as suffix.
- Extract the common file name part into a trait for both component and filter to use.
- Allow other caching approaches than just file cache?
- Allow other caching approaches than just file cache? => Cache Engine(s)
- Allow usage of subfolders for File cache to avoid the folder to have millions of files as a flat list?
- What happens with custom headers set in the original request? How can we pass those to the final cached response?
- Re-implement the removed CacheHelper with its nocache parts?
7 changes: 6 additions & 1 deletion src/Controller/Component/CacheComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CacheComponent extends Component {
'actions' => [],
'compress' => false,
'force' => false,
'when' => null,
];

/**
Expand All @@ -30,6 +31,11 @@ public function shutdown(EventInterface $event): void {
if (Configure::read('debug') && !$this->getConfig('force')) {
return;
}
/** @var callable $when */
$when = $this->getConfig('when');
if ($when !== null && $when($request) !== true) {
return;
}

/** @var \Cake\Http\Response $response */
$response = $event->getSubject()->getResponse();
Expand Down Expand Up @@ -81,7 +87,6 @@ protected function _isActionCachable() {
* @return bool Success of caching view.
*/
protected function _writeFile($content, $duration) {
//$cacheTime = date('Y-m-d H:i:s', $timestamp);
$now = time();
if (!$duration) {
$cacheTime = 0;
Expand Down

0 comments on commit 2654c0e

Please sign in to comment.