2021-05-30 00:46:13 +00:00
|
|
|
# angel3_cache
|
|
|
|
[![version](https://img.shields.io/badge/pub-v4.0.0-brightgreen)](https://pub.dartlang.org/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
|
|
|
|
|
|
|
Support for server-side caching in [Angel](https://angel-dart.github.io).
|
|
|
|
|
|
|
|
## `CacheService`
|
|
|
|
|
|
|
|
A `Service` class that caches data from one service, storing it in another.
|
2018-04-02 01:05:35 +00:00
|
|
|
An imaginable use case is storing results from MongoDB or another database in
|
|
|
|
MemcacheD/Redis.
|
|
|
|
|
2018-04-02 02:50:58 +00:00
|
|
|
## `cacheSerializationResults`
|
|
|
|
A middleware that enables the caching of response serialization.
|
|
|
|
|
|
|
|
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-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]) {
|
|
|
|
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`
|
|
|
|
A flexible response cache for Angel.
|
|
|
|
|
|
|
|
Use this to improve real and perceived response of Web applications,
|
|
|
|
as well as to memoize expensive responses.
|
|
|
|
|
|
|
|
Supports the `If-Modified-Since` header, as well as storing the contents of
|
|
|
|
response buffers in memory.
|
|
|
|
|
|
|
|
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
|
|
|
|
Call `invalidate` to remove a resource from a `ResponseCache`.
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
```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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
```
|