platform/packages/cache
2021-05-31 09:05:34 +08:00
..
.idea Add 'packages/cache/' from commit 'aea408b2f7bda86f28b7ea8c8f8d2b43f47915e7' 2020-02-15 18:29:01 -05:00
example Published oauth2, cache, cors, auth_oauth2 2021-05-30 08:46:13 +08:00
lib Published oauth2, cache, cors, auth_oauth2 2021-05-30 08:46:13 +08:00
test Published oauth2, cache, cors, auth_oauth2 2021-05-30 08:46:13 +08:00
.gitignore Add 'packages/cache/' from commit 'aea408b2f7bda86f28b7ea8c8f8d2b43f47915e7' 2020-02-15 18:29:01 -05:00
.travis.yml Add 'packages/cache/' from commit 'aea408b2f7bda86f28b7ea8c8f8d2b43f47915e7' 2020-02-15 18:29:01 -05:00
analysis_options.yaml Add 'packages/cache/' from commit 'aea408b2f7bda86f28b7ea8c8f8d2b43f47915e7' 2020-02-15 18:29:01 -05:00
AUTHORS.md Published oauth2, cache, cors, auth_oauth2 2021-05-30 08:46:13 +08:00
CHANGELOG.md Published oauth2, cache, cors, auth_oauth2 2021-05-30 08:46:13 +08:00
LICENSE Add 'packages/cache/' from commit 'aea408b2f7bda86f28b7ea8c8f8d2b43f47915e7' 2020-02-15 18:29:01 -05:00
pubspec.yaml Fixed auth returning 500 instead of 403 2021-05-31 09:05:34 +08:00
README.md Published oauth2, cache, cors, auth_oauth2 2021-05-30 08:46:13 +08:00

angel3_cache

version Null Safety Gitter

License

Support for server-side caching in Angel.

CacheService

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.

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.

void main() async {
    var app = Angel()..lazyParseBodies = true;
    
    app.use(
      '/api/todos',
      CacheService(
        database: AnonymousService(
          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()}'};
          }
        ),
      ),
    );
}

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:

Future configureServer(Angel app) async {
  // Simple instance.
  var cache = ResponseCache();
  
  // You can also pass an invalidation timeout.
  var cache = ResponseCache(timeout: const Duration(days: 2));
  
  // Close the cache when the application closes.
  app.shutdownHooks.add((_) => cache.close());
  
  // Use `patterns` to specify which resources should be cached.
  cache.patterns.addAll([
    'robots.txt',
    RegExp(r'\.(png|jpg|gif|txt)$'),
    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:

Future configureServer(Angel app) async {
  app.addRoute('PURGE', '*', (req, res) {
    if (req.ip != '127.0.0.1')
      throw AngelHttpException.forbidden();
    return cache.purge(req.uri.path);
  });
}