dox_cache

Cache for Dart with memory, file and Redis drivers.

pub package license: MIT

Part of Dox, a web backend framework for Dart. Source code: github.com/dartondox/dox.

Install

dart pub add dox_cache

To see what is ready and what may still change, read Status and roadmap.

Use it

import 'package:dox_cache/dox_cache.dart';

void main() async {
  CacheManager cache = CacheManager(CacheConfig(
    defaultStore: 'memory',
    stores: <String, Map<String, dynamic>>{
      'memory': <String, dynamic>{'driver': 'memory', 'max_size': 1000},
      'file': <String, dynamic>{'driver': 'file', 'path': 'storage/cache'},
      'redis': <String, dynamic>{'driver': 'redis', 'host': '127.0.0.1', 'prefix': 'app:'},
    },
  ));

  CacheStore store = cache.store(); // the default store
  await store.put('a', 1, Duration(minutes: 5));
  print(await store.get('a')); // 1

  List<dynamic> posts = await store.remember('posts', Duration(minutes: 10), () async {
    return <dynamic>['first post'];
  });
  print(posts);
}

In a Dox app you do not set this up yourself. Set the stores in config/cache.dart and use Cache().

Encrypt values

Add 'encrypt': true to a file or redis store, and pass encrypter: to CacheConfig. In a Dox app the Dox().cache encrypter uses your APP_KEY. On Redis, plain ints stay plain, so increment still works.

Features

  • Drivers: memory (with an optional LRU size limit), file, redis and null.
  • get, put, add, forget, many, putMany, increment, pull, remember.
  • Tags: store.tags(['users']).flush() removes only those items.
  • Atomic locks with lock(name, ttl): get(), release(), block(timeout). Memory, file and Redis.
  • A key prefix per store. Redis flush deletes only keys with the prefix.
  • Your own driver with cache.extend(name, factory).

Values must be JSON types: Map, List, String, num, bool, null. The memory store lives in one isolate. Use redis when isolates or servers must share data.

Documentation

License

MIT. See LICENSE.

Libraries

commands
The dox commands of dox_cache. The dox CLI finds this library from the dox: commands: key in pubspec.yaml.
dox_cache
Cache for Dart and Dox: memory, file and Redis drivers.