83 lines
2.6 KiB
Dart
83 lines
2.6 KiB
Dart
/*
|
|
* This file is part of the Protevus Platform.
|
|
*
|
|
* (C) Protevus <developers@protevus.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
import 'package:protevus_http/http.dart';
|
|
|
|
/// Instances of this type provide configuration for the 'Cache-Control' header.
|
|
///
|
|
/// Typically used by [FileController]. See [FileController.addCachePolicy].
|
|
class CachePolicy {
|
|
/// Creates a new cache policy.
|
|
///
|
|
/// Policies applied to [Response.cachePolicy] will add the appropriate
|
|
/// headers to that response. See properties for definitions of arguments
|
|
/// to this constructor.
|
|
///
|
|
/// [preventIntermediateProxyCaching] - If true, prevents caching by intermediate proxies.
|
|
/// [preventCaching] - If true, prevents any caching of the response.
|
|
/// [requireConditionalRequest] - If true, requires a conditional GET for cached responses.
|
|
/// [expirationFromNow] - Sets the duration for which the resource is valid.
|
|
const CachePolicy({
|
|
this.preventIntermediateProxyCaching = false,
|
|
this.preventCaching = false,
|
|
this.requireConditionalRequest = false,
|
|
this.expirationFromNow,
|
|
});
|
|
|
|
/// Prevents a response from being cached by an intermediate proxy.
|
|
///
|
|
/// This sets 'Cache-Control: private' if true. Otherwise, 'Cache-Control: public' is used.
|
|
final bool preventIntermediateProxyCaching;
|
|
|
|
/// Prevents any caching of a response by a proxy or client.
|
|
///
|
|
/// If true, sets 'Cache-Control: no-cache, no-store'. If this property is true,
|
|
/// no other properties are evaluated.
|
|
final bool preventCaching;
|
|
|
|
/// Requires a client to send a conditional GET to use a cached response.
|
|
///
|
|
/// If true, sets 'Cache-Control: no-cache'.
|
|
final bool requireConditionalRequest;
|
|
|
|
/// Sets how long a resource is valid for.
|
|
///
|
|
/// Sets 'Cache-Control: max-age=x', where 'x' is [expirationFromNow] in seconds.
|
|
final Duration? expirationFromNow;
|
|
|
|
/// Constructs a header value configured from this instance.
|
|
///
|
|
/// This value is used for the 'Cache-Control' header.
|
|
///
|
|
/// Returns a string representation of the cache control header based on the
|
|
/// configuration of this CachePolicy instance.
|
|
String get headerValue {
|
|
if (preventCaching) {
|
|
return "no-cache, no-store";
|
|
}
|
|
|
|
final items = [];
|
|
|
|
if (preventIntermediateProxyCaching) {
|
|
items.add("private");
|
|
} else {
|
|
items.add("public");
|
|
}
|
|
|
|
if (expirationFromNow != null) {
|
|
items.add("max-age=${expirationFromNow!.inSeconds}");
|
|
}
|
|
|
|
if (requireConditionalRequest) {
|
|
items.add("no-cache");
|
|
}
|
|
|
|
return items.join(", ");
|
|
}
|
|
}
|