angel_cache 1.0.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 27

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

1.0.0 #

  • First version

example/main.dart

import 'package:angel_cache/angel_cache.dart';
import 'package:angel_framework/angel_framework.dart';
import 'package:glob/glob.dart';

main() async {
  var app = new Angel()..lazyParseBodies = true;

  // Cache a glob
  var cache = new ResponseCache()
    ..patterns.addAll([
      new Glob('/*.txt'),
    ]);

  // Handle `if-modified-since` header, and also send cached content
  app.use(cache.handleRequest);

  // A simple handler that returns a different result every time.
  app.get('/date.txt',
      (ResponseContext res) => res.write(new DateTime.now().toIso8601String()));

  // Support purging the cache.
  app.addRoute('PURGE', '*', (RequestContext req) {
    if (req.ip != '127.0.0.1') throw new AngelHttpException.forbidden();

    cache.purge(req.uri.path);
    print('Purged ${req.uri.path}');
  });

  // The response finalizer that actually saves the content
  app.responseFinalizers.add(cache.responseFinalizer);

  var http = new AngelHttp(app);
  var server = await http.startServer('127.0.0.1', 3000);
  print('Listening at http://${server.address.address}:${server.port}');
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  angel_cache: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:angel_cache/angel_cache.dart';
  
Version Uploaded Documentation Archive
2.0.1 Nov 10, 2018 Go to the documentation of angel_cache 2.0.1 Download angel_cache 2.0.1 archive
2.0.0 Oct 21, 2018 Go to the documentation of angel_cache 2.0.0 Download angel_cache 2.0.0 archive
1.0.0 May 16, 2018 Go to the documentation of angel_cache 1.0.0 Download angel_cache 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
54
Health:
Code health derived from static analysis. [more]
--
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
--
Overall:
Weighted score of the above. [more]
27
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Fix dependencies in pubspec.yaml.

Running pub upgrade failed with the following output:

ERR: The current Dart SDK version is 2.3.1.
 
 Because angel_framework >=0.0.0-dev.1 <1.1.3 requires SDK version <2.0.0 and angel_framework >=1.1.3 <2.0.0-alpha depends on container ^0.1.2, angel_framework >=0.0.0-dev.1 <2.0.0-alpha requires container ^0.1.2.
 So, because container >=0.1.1 requires SDK version >=1.8.0 <2.0.0 and angel_cache depends on angel_framework ^1.0.0-dev, version solving failed.

Health suggestions

Format lib/angel_cache.dart.

Run dartfmt to format lib/angel_cache.dart.

Format lib/src/cache.dart.

Run dartfmt to format lib/src/cache.dart.

Format lib/src/serializer.dart.

Run dartfmt to format lib/src/serializer.dart.

Format lib/src/util.dart.

Run dartfmt to format lib/src/util.dart.

Maintenance issues and suggestions

Fix platform conflicts. (-20 points)

Error(s) prevent platform classification:

Fix dependencies in pubspec.yaml.

Make sure dartdoc successfully runs on your package's source files. (-10 points)

Dependencies were not resolved.

The package description is too short. (-19 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Package is getting outdated. (-1.64 points)

The package was last published 53 weeks ago.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.19.0 <3.0.0