diff --git a/Classes/Middleware/RequestCacheMiddleware.php b/Classes/Middleware/RequestCacheMiddleware.php index dd9c6dc..02c6028 100644 --- a/Classes/Middleware/RequestCacheMiddleware.php +++ b/Classes/Middleware/RequestCacheMiddleware.php @@ -46,12 +46,24 @@ class RequestCacheMiddleware implements MiddlewareInterface */ protected $ignoredQueryParams; + /** + * @var array + * @Flow\InjectConfiguration(path="request.queryParams.respect") + */ + protected $respectedQueryParams; + /** * @var array * @Flow\InjectConfiguration(path="request.cookieParams.ignore") */ protected $ignoredCookieParams; + /** + * @var array + * @Flow\InjectConfiguration(path="request.cookieParams.respect") + */ + protected $respectedCookieParams; + /** * @var boolean * @Flow\InjectConfiguration(path="maxPublicCacheTime") @@ -127,13 +139,17 @@ protected function getCacheIdentifierForRequestIfCacheable(ServerRequestInterfac $requestQueryParams = $request->getQueryParams(); $allowedQueryParams = []; $ignoredQueryParams = []; + $respectedQueryParams = []; $disallowedQueryParams = []; foreach ($requestQueryParams as $key => $value) { switch (true) { case (in_array($key, $this->allowedQueryParams)): $allowedQueryParams[$key] = $value; break; - case (in_array($key, $this->ignoredQueryParams)): + case (in_array($key, $this->ignoredQueryParams) && empty($this->respectedQueryParams)): + $ignoredQueryParams[$key] = $value; + break; + case (!in_array($key, $this->respectedQueryParams) && empty($this->ignoredQueryParams)): $ignoredQueryParams[$key] = $value; break; default: @@ -148,7 +164,9 @@ protected function getCacheIdentifierForRequestIfCacheable(ServerRequestInterfac $requestCookieParams = $request->getCookieParams(); $disallowedCookieParams = []; foreach ($requestCookieParams as $key => $value) { - if (!in_array($key, $this->ignoredCookieParams)) { + if (!in_array($key, $this->ignoredCookieParams) && !empty($this->ignoredCookieParams) && empty($this->respectedCookieParams)) { + $disallowedCookieParams[$key] = $value; + } else if (in_array($key, $this->respectedCookieParams) && empty($this->ignoredCookieParams)) { $disallowedCookieParams[$key] = $value; } } diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index 641dcfe..e35f258 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -16,9 +16,22 @@ Flowpack: cookieParams: # ignored cookie params exclude cookies that are handled by the frontend # and are not relevant for the backend. A usecase would be gdpr consent cookies - # if they are only used on the client side + # if they are only used on the client side. + # Example: + # - 'consent_settings' + # => If the cookie "consent_settings" is present, it will be cached. + # But if a "_ga" cookie is present, the request will not be cached. + # Important: Only evaluated if "respect" is empty! ignore: [] + # respected cookie params are relevant for the backend and will skip the caching. + # Example: + # - 'Neos_Session' + # => If the "Neos_Session" cookie is present in the request, it will not be cached. + # But if a "_ga" cookie is present, the request will be cached. + # Important: Only evaluated if "ignore" is empty! + respect: [] + # a request will only qualify for caching if it only contains queryParams that # are allowed or ignored. All other arguments will prevent caching. queryParams: @@ -29,8 +42,22 @@ Flowpack: # ignored arguments are not part of the cache identifier but do not # prevent caching either. Use this for arguments that are meaningless for # the backend like utm_campaign + # Example: + # - 'utm_campaign' + # => If the "utm_campaign" argument is present in the request, it will be cached. + # But if a "utm_source" argument is present, the request will not be cached. + # Important: Only evaluated if "respect" is empty! ignore: [] + # respected arguments are relevant for the backend and will skip the caching. + # All other arguments will be ignored and cached as if they weren't there. + # Example: + # - 'search' + # => If the "search" argument is present in the request, it will not be cached. + # But if a "utm_source" argument is present, the request will be cached as if the argument wouldn't be there. + # Important: Only evaluated if "ignore" is empty! + respect: [] + Neos: Flow: http: diff --git a/README.md b/README.md index f30a118..2ea1ac8 100644 --- a/README.md +++ b/README.md @@ -29,9 +29,22 @@ Flowpack: cookieParams: # ignored cookie params exclude cookies that are handled by the frontend # and are not relevant for the backend. A usecase would be gdpr consent cookies - # if they are only used on the client side + # if they are only used on the client side. + # Example: + # - 'consent_settings' + # => If the cookie "consent_settings" is present, it will be cached. + # But if a "_ga" cookie is present, the request will not be cached. + # Important: Only evaluated if "respect" is empty! ignore: [] + # respected cookie params are relevant for the backend and will skip the caching. + # Example: + # - 'Neos_Session' + # => If the "Neos_Session" cookie is present in the request, it will not be cached. + # But if a "_ga" cookie is present, the request will be cached. + # Important: Only evaluated if "ignore" is empty! + respect: [] + # a request will only qualify for caching if it only contains queryParams that # are allowed or ignored. All other arguments will prevent caching. queryParams: @@ -42,7 +55,21 @@ Flowpack: # ignored arguments are not part of the cache identifier but do not # prevent caching either. Use this for arguments that are meaningless for # the backend like utm_campaign + # Example: + # - 'utm_campaign' + # => If the "utm_campaign" argument is present in the request, it will be cached. + # But if a "utm_source" argument is present, the request will not be cached. + # Important: Only evaluated if "respect" is empty! ignore: [] + + # respected arguments are relevant for the backend and will skip the caching. + # All other arguments will be ignored and cached as if they weren't there. + # Example: + # - 'search' + # => If the "search" argument is present in the request, it will not be cached. + # But if a "utm_source" argument is present, the request will be cached as if the argument wouldn't be there. + # Important: Only evaluated if "ignore" is empty! + respect: [] ``` You can also move the cache backend to something faster if available, to improve performance even more.