0 purchases
cache controller
cache_controller #
cache_controller is a Dart package for managing cached data with expiration control.
Usage #
To use this package, add cache_controller as a dependency in your pubspec.yaml file.
dependencies:
cache_controller: ^0.9.7-pre
copied to clipboard
or use dart cli
dart pub add cache_controller
copied to clipboard
Then import the package into your Dart code:
import 'package:cache_controller/cache_controller.dart';
copied to clipboard
CacheController #
CacheController is the main class provided by this package. It allows you to manage cached data with expiration control.
Here's a simple example of how to use CacheController:
import 'package:cache_controller/cache_controller.dart';
void main() async {
// Create a cache controller with a TTL of 1 hour
final cacheController = CacheController<int>(
storage:
MemoryCacheStorage(), // Example storage, use your own implementation
ttl: const Duration(hours: 1),
onCacheRequest: () async {
// Simulate fetching data from a remote source
return 42;
},
);
// Retrieve the cached value, or fetch it if it's expired or not available
final value = await cacheController.value;
print('Cached value: $value');
// Update the cache value
await cacheController.update(100);
print('Updated cache value');
// Dispose of the cache controller when no longer needed
cacheController.dispose();
}
copied to clipboard
In this example, CacheController is used to manage an integer value in the cache. It fetches the value from a remote source using the onCacheRequest callback if it's expired or not available. The TTL (time-to-live) determines how long the cached value remains valid.
CacheStorage #
CacheStorage is an abstract class representing the storage mechanism for cached data. You can implement your own storage mechanism by extending this class. This package provides some built-in implementations:
MemoryCacheStorage: Stores cached data in memory.
Features and bugs #
Please file feature requests and bugs at the issue tracker.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.