The Protevus Platform: Unified Full-Stack Development https://protevus.com
Find a file
2018-05-15 23:54:34 -04:00
.idea CacheService example 2018-04-01 23:41:43 -04:00
example CacheService example 2018-04-01 23:41:43 -04:00
lib 1.0.0 2018-05-15 23:54:34 -04:00
test Send if-modified-since in the future 2018-04-02 11:28:22 -04:00
.gitignore cacheSerializationResults 2018-04-01 21:11:40 -04:00
.travis.yml Update cache_test 2018-04-02 11:23:17 -04:00
analysis_options.yaml Serve content from cache 2018-04-01 21:45:45 -04:00
CHANGELOG.md 1.0.0 2018-05-15 23:54:34 -04:00
LICENSE Initial commit 2017-05-25 08:28:39 -04:00
pubspec.yaml 1.0.0 2018-05-15 23:54:34 -04:00
README.md 1.0.0 2018-05-15 23:54:34 -04:00

cache

Pub build status

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.

main() async {
    var app = new Angel()..lazyParseBodies = true;
    
    app.use(
      '/api/todos',
      new CacheService(
        database: new 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 = new ResponseCache();
  
  // You can also pass an invalidation timeout.
  var cache = new 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',
    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:

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);
  });
}