RemoteCaching class
A Flutter package for caching remote API calls with configurable duration.
This package provides a simple yet powerful way to cache remote API responses locally using SQLite, with support for automatic expiration, custom serialization, and intelligent cache management.
Key Features
- Automatic caching with configurable expiration
- SQLite persistence for reliable data storage
- Generic support for any serializable data type
- Custom deserialization with
fromJson
functions - Cache statistics and monitoring
- Error handling with graceful fallbacks
- Cross-platform support (iOS, Android, Web, Desktop)
Basic Usage
// Initialize the cache system
await RemoteCaching.instance.init(
defaultCacheDuration: Duration(hours: 1),
verboseMode: true,
);
// Cache a remote API call
final user = await RemoteCaching.instance.call<User>(
'user_profile_123',
cacheDuration: Duration(minutes: 30),
remote: () async => await apiService.getUser(123),
fromJson: (json) => User.fromJson(json as Map<String, dynamic>),
);
Advanced Usage
// Use exact expiration time
final data = await RemoteCaching.instance.call<Data>(
'cache_key',
cacheExpiring: DateTime.now().add(Duration(hours: 2)),
remote: () async => await fetchData(),
fromJson: (json) => Data.fromJson(json as Map<String, dynamic>),
);
// Force refresh (bypass cache)
final freshData = await RemoteCaching.instance.call<Data>(
'cache_key',
forceRefresh: true,
remote: () async => await fetchData(),
fromJson: (json) => Data.fromJson(json as Map<String, dynamic>),
);
// Cache lists and complex data
final users = await RemoteCaching.instance.call<List<User>>(
'all_users',
remote: () async => await apiService.getAllUsers(),
fromJson: (json) => (json as List)
.map((item) => User.fromJson(item as Map<String, dynamic>))
.toList(),
);
Cache Management
// Clear specific cache entry
await RemoteCaching.instance.clearCacheForKey('user_profile_123');
// Clear all cache
await RemoteCaching.instance.clearCache();
// Get cache statistics
final stats = await RemoteCaching.instance.getCacheStats();
print('Total entries: ${stats.totalEntries}');
Error Handling
The package handles serialization errors gracefully. If fromJson
fails,
the error is logged and the remote call is used instead. Your app will
never crash due to cache-related errors.
Thread Safety
This class is thread-safe and can be used from multiple isolates. All database operations are properly synchronized.
Constructors
- RemoteCaching.new()
-
factory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
call<
T> (String key, {required Future< T> remote(), required T fromJson(Object? json), Duration? cacheDuration, DateTime? cacheExpiring, bool forceRefresh = false}) → Future<T> - Execute a remote call with caching.
-
clearCache(
) → Future< void> - Clear the entire cache.
-
clearCacheForKey(
String key) → Future< void> - Clear a specific cache entry.
-
dispose(
) → Future< void> - Dispose of the cache system.
-
getCacheStats(
) → Future< CachingStats> - Get cache statistics.
-
init(
{Duration? defaultCacheDuration, bool verboseMode = kDebugMode, String? databasePath}) → Future< void> - Initialize the caching system.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- instance → RemoteCaching
-
no setter