api_cache 1.0.0
api_cache: ^1.0.0 copied to clipboard
A Flutter package for caching API responses with expiration support using Hive.
API Cache #
A Flutter package for caching API responses with expiration support using Hive.
Features #
- Cache API responses with optional expiration
- Automatic data normalization
- Type-safe data retrieval
- Easy to use API
- Built on top of Hive for efficient storage
Installation #
Add this to your package's pubspec.yaml file:
dependencies:
api_cache: ^1.0.0
Usage #
1. Initialize the Cache #
import 'package:api_cache/api_cache.dart';
// Initialize the cache
final apiCache = ApiCacheRepository(HiveRepositoryImpl());
await apiCache.init();
2. Cache Data #
// Cache data with expiration
await apiCache.cacheData(
cacheKey: 'user_profile',
data: {
'id': 1,
'name': 'John Doe',
'email': 'john@example.com',
},
expiration: const Duration(hours: 1),
);
// Cache data without expiration (stays until cleared)
await apiCache.cacheData(
cacheKey: 'app_settings',
data: {
'theme': 'dark',
'notifications': true,
},
);
3. Retrieve Cached Data #
// Get cached data with default value
final userData = await apiCache.getCachedData(
cacheKey: 'user_profile',
defaultValue: null,
);
// Get cached data with type safety
final settings = await apiCache.getCachedData<Map<String, dynamic>>(
cacheKey: 'app_settings',
defaultValue: {'theme': 'light', 'notifications': false},
);
4. Check Cache Status #
// Check if data exists in cache
final exists = await apiCache.hasCachedData('user_profile');
// Check if data is expired
final isExpired = await apiCache.isExpired('user_profile');
5. Cache Management #
// Clear specific cache
await apiCache.clearCache('user_profile');
// Clear all cache
await apiCache.clearAllCache();
// Get cache size
final size = await apiCache.getCacheSize();
6. Error Handling #
try {
await apiCache.cacheData(
cacheKey: 'user_profile',
data: userData,
expiration: const Duration(hours: 1),
);
} catch (e) {
print('Error caching data: $e');
}
API Reference #
ApiCacheRepository #
The main class for managing API cache operations.
Methods
init(): Initialize the cache systemcacheData({required String cacheKey, required dynamic data, Duration? expiration}): Cache data with optional expirationgetCachedData<T>({required String cacheKey, required T defaultValue}): Retrieve cached data with type safetyhasCachedData(String cacheKey): Check if data exists in cacheisExpired(String cacheKey): Check if cached data is expiredclearCache(String cacheKey): Clear specific cacheclearAllCache(): Clear all cached datagetCacheSize(): Get total cache size
HiveRepositoryImpl #
Implementation of the cache storage using Hive.
Methods
init(): Initialize Hive storagesave(String key, dynamic value): Save data to Hiveget(String key): Retrieve data from Hivedelete(String key): Delete data from Hiveclear(): Clear all data from Hive
Example #
Check out the example for a complete usage example.
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
Features and bugs #
Please file feature requests and bugs at the issue tracker.