Cache Manager Plus 📚
A Dart package for managing cache with support for multiple storage backends. The CacheManager
provides an easy-to-use
API for caching data, with features like ephemeral and persistent caching, cache invalidation, and support for custom
storage implementations.
Table of content
Features 🧪
- Ephemeral and Persistent Caching: Cache data with a specific expiration or as temporary data.
- Multiple Storage Backends: Use in-memory storage, Hive-based storage, or implement your own custom
CacheStore
. - Cache Invalidation: Invalidate specific cache items or clear the entire cache.
- Singleton Access: Manage cache through a singleton
CacheManager
instance. - Customizable: Extend the
CacheStore
interface to create your own storage backends. - Utils: Find CacheManagerPlus utilities as a pub package here. Includes different CacheStore implementations and other utility functions
Installation 🏗️
To use this package, add it to your pubspec.yaml
file:
dependencies:
cache_manager_plus: ^1.0.0
Install by running:
dart pub add cache_manager_plus
Usage 🔧
Initializing the CacheManager
You can initialize the CacheManager with a single store or multiple stores:
import 'package:cache_manager_plus/cache_manager_plus.dart';
void main() async {
// Single store mode
await CacheManager.init(store: InMemoryCacheStore());
// Multiple stores mode
await CacheManager.init(
stores: [InMemoryCacheStore()],
);
}
Adding and Retrieving Cache Items
import 'package:cache_manager/cache_manager.dart';
void main() async {
final cacheManager = CacheManager.instance;
// Add a persistent cache item
await cacheManager.set(
CacheItem.persistent(
key: 'user_profile',
data: {'name': 'John Doe', 'age': 30},
duration: Duration(days: 7),
),
);
// Retrieve the cache item
final item = await cacheManager.get('user_profile');
if (item != null && item.isValid) {
print('Cached data: ${item.data}');
}
}
Invalidating Cache
import 'package:cache_manager/cache_manager.dart';
void main() async {
final cacheManager = CacheManager.instance;
// Invalidate a specific cache item
await cacheManager.invalidateCacheItem('user_profile');
// Invalidate all cache items
await cacheManager.invalidateCache();
}
License
This project is licensed under the MIT License—see the LICENSE file for details
Additional information
For more details, visit the repository and example tests. Contributions, issues, and feature requests are welcome. See the issue tracker for more information.