api_cache 1.0.0 copy "api_cache: ^1.0.0" to clipboard
api_cache: ^1.0.0 copied to clipboard

A Flutter package for caching API responses with expiration support using Hive.

API Cache #

pub package License: MIT

codecov style: very good analysis

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 system
  • cacheData({required String cacheKey, required dynamic data, Duration? expiration}): Cache data with optional expiration
  • getCachedData<T>({required String cacheKey, required T defaultValue}): Retrieve cached data with type safety
  • hasCachedData(String cacheKey): Check if data exists in cache
  • isExpired(String cacheKey): Check if cached data is expired
  • clearCache(String cacheKey): Clear specific cache
  • clearAllCache(): Clear all cached data
  • getCacheSize(): Get total cache size

HiveRepositoryImpl #

Implementation of the cache storage using Hive.

Methods

  • init(): Initialize Hive storage
  • save(String key, dynamic value): Save data to Hive
  • get(String key): Retrieve data from Hive
  • delete(String key): Delete data from Hive
  • clear(): 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.

2
likes
150
points
14
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package for caching API responses with expiration support using Hive.

Documentation

API reference

License

MIT (license)

Dependencies

flutter, hive, path_provider

More

Packages that depend on api_cache