platform/packages/cache/README.md

93 lines
3 KiB
Markdown
Raw Normal View History

2021-06-26 10:02:41 +00:00
# HTTP Caching for Angel3
2021-06-22 10:42:26 +00:00
2021-07-08 05:23:02 +00:00
[![version](https://img.shields.io/badge/pub-v4.0.1-brightgreen)](https://pub.dev/packages/angel3_cache)
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)
[![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/packages/cache/LICENSE)
2018-01-30 15:27:25 +00:00
2021-07-08 05:23:02 +00:00
A service that provides HTTP caching to the response data for [Angel3 framework](https://pub.dev/packages/angel3).
2018-01-30 15:27:25 +00:00
## `CacheService`
2021-06-22 10:42:26 +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 MemcacheD/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
void main() async {
var app = Angel()..lazyParseBodies = true;
2018-05-16 03:54:34 +00:00
app.use(
'/api/todos',
CacheService(
database: AnonymousService(
2018-05-16 03:54:34 +00:00
index: ([params]) {
print('Fetched directly from the underlying service at ${new DateTime.now()}!');
return ['foo', 'bar', 'baz'];
},
read: (id, [params]) {
return {id: '$id at ${new DateTime.now()}'};
}
),
),
);
}
```
2018-04-02 01:59:46 +00:00
## `ResponseCache`
2021-06-22 10:42:26 +00:00
A flexible response cache for Angel3.
Use this to improve real and perceived response of Web applications, as well as to memoize 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.
var cache = ResponseCache();
2018-04-02 01:59:46 +00:00
// You can also pass an invalidation timeout.
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',
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')
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
```