A powerful Flutter package for caching content with tagging and metadata capabilities, utilizing flutter_cache_manager and shared_preferences under the hood.
- Content Caching: Store and retrieve
Map<String, dynamic>content against unique keys. - Metadata Storage: Associate an
updatedOndate with each cached content item. - Tagging System: Assign a single tag to content keys for grouped cache management.
- Cache Invalidation: Clear cache by key, tag, or entirely.
- Configuration: Customize global cache duration, maximum number of cache objects, and cache key.
- Platform Support: Works seamlessly on Android, iOS, and Web platforms.
- Consistent API: Provides a uniform interface across platforms, abstracting away platform-specific differences.
Add super_content_cache to your project's pubspec.yaml file:
dependencies:
super_content_cache: 0.0.1Then, run:
flutter pub getimport 'package:super_content_cache/super_content_cache.dart';Before using the cache, you can configure it with optional parameters:
cacheKey: A unique identifier for your cache (useful if you have multiple caches or to avoid conflicts on the web).globalCacheDuration: The default duration to keep content in the cache (default is 30 days).maxObjects: The maximum number of objects to store in the cache (default is 200).
SuperContentCache.configure(
cacheKey: 'superContentCache_myApp',
globalCacheDuration: Duration(days: 7),
maxObjects: 500,
);Store a Map<String, dynamic> against a key, with an optional tag and updatedOn date:
await SuperContentCache.storeContent(
key: 'user_profile_123',
content: {
'name': 'John Doe',
'age': 30,
},
updatedOn: DateTime.now(),
tag: 'user_profiles', // Optional
);Retrieve the content associated with a key:
final content = await SuperContentCache.getContent('user_profile_123');
print('Content: $content');Retrieve the updatedOn date for a key:
final updatedOn = await SuperContentCache.getUpdatedOn('user_profile_123');
print('Updated On: $updatedOn');Clear the cache for a specific key:
await SuperContentCache.clearKey('user_profile_123');Clear the cache for all keys associated with a tag:
await SuperContentCache.clearTag('user_profiles');Clear all cached content and associated metadata:
await SuperContentCache.clearAll();An example application demonstrating how to use the super_content_cache package is available in the example directory. It showcases:
- Configuring the cache with a custom
cacheKey. - Storing, updating, and retrieving content.
- Clearing cache by key, tag, and entirely.
You can find the complete example in the example directory of the package.
- Storage Mechanism: On the web, content is stored using
SharedPreferences, which utilizes the browser'slocalStorage. - Data Persistence: Data persists across page reloads and browser sessions.
- Storage Limitations: Browsers typically limit
localStorageto around 5MB per origin. Be mindful of the data size when storing content. - Performance: Suitable for small to medium-sized data. Not ideal for large binary files or extensive data storage.
- Storage Mechanism: Uses
flutter_cache_managerto store content in the app's local file system. - Data Persistence: Data persists as long as the app's cache is not cleared.
- Capacity: Capable of handling larger amounts of data compared to
SharedPreferences. - Performance: Efficient for storing and retrieving larger datasets.
- Web Storage Quotas: Be aware of storage limitations on the web and handle potential
QuotaExceededErrorexceptions. - Data Size: Ensure that the content size is appropriate for the storage mechanism on each platform.
- Single Tag per Key: Each key can have only one associated tag.
Contributions are welcome! If you find any issues or have suggestions for improvements, please submit issues and pull requests on GitHub.
This project is licensed under the MIT License - see the LICENSE file for details.