2024-10-12 10:35:14 +00:00
# Protevus HTTP Cache
2021-06-22 10:42:26 +00:00
2021-12-19 02:23:27 +00:00
![Pub Version (including pre-releases) ](https://img.shields.io/pub/v/angel3_cache?include_prereleases )
2021-05-30 00:46:13 +00:00
[![Null Safety ](https://img.shields.io/badge/null-safety-brightgreen )](https://dart.dev/null-safety)
2024-07-07 15:02:49 +00:00
[![Discord ](https://img.shields.io/discord/1060322353214660698 )](https://discord.gg/3X6bxTUdCM)
2024-10-12 10:35:14 +00:00
[![License ](https://img.shields.io/github/license/dart-backend/protevus )](https://github.com/dart-backend/protevus/tree/master/packages/cache/LICENSE)
2018-01-30 15:27:25 +00:00
2024-10-12 10:35:14 +00:00
A service that provides HTTP caching to the response data for [Protevus framework ](https://pub.dev/packages/angel3 ).
2018-01-30 15:27:25 +00:00
## `CacheService`
2021-12-19 02:23:27 +00:00
A `Service` class that caches data from one service, storing it in another. An imaginable use case is storing results from MongoDB or another database in Memcache/Redis.
2018-04-02 01:05:35 +00:00
2018-04-02 02:50:58 +00:00
## `cacheSerializationResults`
2021-06-22 10:42:26 +00:00
2018-04-02 02:50:58 +00:00
A middleware that enables the caching of response serialization.
2021-06-26 10:02:41 +00:00
This can improve the performance of sending objects that are complex to serialize. You can pass a [shouldCache] callback to determine which values should be cached.
2018-04-02 02:50:58 +00:00
2018-05-16 03:54:34 +00:00
```dart
2021-05-30 00:46:13 +00:00
void main() async {
var app = Angel()..lazyParseBodies = true;
2018-05-16 03:54:34 +00:00
app.use(
'/api/todos',
2021-05-30 00:46:13 +00:00
CacheService(
database: AnonymousService(
2018-05-16 03:54:34 +00:00
index: ([params]) {
2022-01-23 05:10:50 +00:00
print('Fetched directly from the underlying service at ${DateTime.now()}!');
2018-05-16 03:54:34 +00:00
return ['foo', 'bar', 'baz'];
},
read: (id, [params]) {
2022-01-23 05:10:50 +00:00
return {id: '$id at ${DateTime.now()}'};
2018-05-16 03:54:34 +00:00
}
),
),
);
}
```
2018-04-02 01:59:46 +00:00
## `ResponseCache`
2024-10-12 10:35:14 +00:00
A flexible response cache for Protevus.
2021-06-22 10:42:26 +00:00
2022-01-23 05:10:50 +00:00
Use this to improve real and perceived response of Web applications, as well as to memorize expensive responses.
2018-04-02 01:59:46 +00:00
2021-06-22 10:42:26 +00:00
Supports the `If-Modified-Since` header, as well as storing the contents of response buffers in memory.
2018-04-02 01:59:46 +00:00
To initialize a simple cache:
```dart
Future configureServer(Angel app) async {
// Simple instance.
2021-05-30 00:46:13 +00:00
var cache = ResponseCache();
2018-04-02 01:59:46 +00:00
// You can also pass an invalidation timeout.
2021-05-30 00:46:13 +00:00
var cache = ResponseCache(timeout: const Duration(days: 2));
2018-04-02 01:59:46 +00:00
2018-04-02 02:02:56 +00:00
// Close the cache when the application closes.
app.shutdownHooks.add((_) => cache.close());
2018-04-02 01:59:46 +00:00
// Use `patterns` to specify which resources should be cached.
cache.patterns.addAll([
'robots.txt',
2021-05-30 00:46:13 +00:00
RegExp(r'\.(png|jpg|gif|txt)$'),
Glob('public/**/*'),
2018-04-02 01:59:46 +00:00
]);
// REQUIRED: The middleware that serves cached responses
app.use(cache.handleRequest);
// REQUIRED: The response finalizer that saves responses to the cache
app.responseFinalizers.add(cache.responseFinalizer);
}
```
### Purging the Cache
2021-06-22 10:42:26 +00:00
2018-04-02 01:59:46 +00:00
Call `invalidate` to remove a resource from a `ResponseCache` .
2021-06-22 10:42:26 +00:00
Some servers expect a reverse proxy or caching layer to support `PURGE` requests. If this is your case, make sure to include some sort of validation (maybe IP-based) to ensure no arbitrary attacker can hack your cache:
2018-04-02 01:59:46 +00:00
```dart
Future configureServer(Angel app) async {
app.addRoute('PURGE', '*', (req, res) {
if (req.ip != '127.0.0.1')
2021-05-30 00:46:13 +00:00
throw AngelHttpException.forbidden();
2018-04-02 01:59:46 +00:00
return cache.purge(req.uri.path);
});
}
2021-06-22 10:42:26 +00:00
```