2018-01-30 15:27:25 +00:00
|
|
|
# cache
|
|
|
|
[![Pub](https://img.shields.io/pub/v/angel_cache.svg)](https://pub.dartlang.org/packages/angel_cache)
|
|
|
|
[![build status](https://travis-ci.org/angel-dart/cache.svg)](https://travis-ci.org/angel-dart/cache)
|
|
|
|
|
|
|
|
Support for server-side caching in [Angel](https://angel-dart.github.io).
|
|
|
|
|
|
|
|
## `CacheService`
|
|
|
|
*TODO: Documentation*
|
|
|
|
|
|
|
|
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 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.
|
|
|
|
var cache = new ResponseCache();
|
|
|
|
|
|
|
|
// You can also pass an invalidation timeout.
|
|
|
|
var cache = new ResponseCache(timeout: const Duration(days: 2));
|
|
|
|
|
|
|
|
// Use `patterns` to specify which resources should be cached.
|
|
|
|
cache.patterns.addAll([
|
|
|
|
'robots.txt',
|
|
|
|
new RegExp(r'\.(png|jpg|gif|txt)$'),
|
|
|
|
new Glob('/public/**/*'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// 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')
|
|
|
|
throw new AngelHttpException.forbidden();
|
|
|
|
return cache.purge(req.uri.path);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
```
|