cache_guard
Simple async caching for Flutter. Run once, reuse forever.
No more boilerplate. No more duplicate calls. Just cache it.
What is this?
cache_guard is a lightweight utility that caches any async result with production-ready protections â in a single line:
- ð Cache async results â execute once, reuse on every subsequent call
- ð Prevent duplicate calls â concurrent requests for the same key share a single execution
- âąïļ TTL support â set expiration durations for automatic cache invalidation
- ð Force refresh â bypass cache when you need fresh data
- ð§Đ Fluent extension â chain
.cache("key")directly on anyFuture - ðŠķ Lightweight â zero external dependencies, pure Dart
- ðŊ Minimal API â 4 functions, 1 extension, that's it
Every Flutter developer writes the same caching boilerplate.
cache_guardreplaces all of it with one function call.
Getting Started
Installation
dependencies:
cache_guard: ^0.0.1
flutter pub get
Minimal Example
import 'package:cache_guard/cache_guard.dart';
// That's it. Cached, deduplicated, zero config.
final user = await cacheGuard('user', () => api.getUser());
Usage
Basic
final user = await cacheGuard('user', () => api.getUser());
First call executes the API and caches the result. Second call returns the cached value instantly.
With TTL
final user = await cacheGuard(
'user',
() => api.getUser(),
ttl: Duration(minutes: 5),
);
Cache expires after 5 minutes. Next call after expiration re-executes the task.
Force Refresh
final user = await cacheGuard(
'user',
() => api.getUser(),
forceRefresh: true,
);
Ignores cache and always fetches fresh data.
Extension Syntax
final user = await api.getUser().cache('user');
// With TTL
final user = await api.getUser().cache('user', ttl: Duration(minutes: 5));
Cache Utilities
// Check if a key is cached
if (hasCache('user')) {
print('User is cached!');
}
// Get cached value synchronously
final user = getCache<User>('user');
// Clear a specific key
clearCache('user');
// Clear all cache
clearCache();
API Reference
cacheGuard<T>()
| Parameter | Type | Default | Description |
|---|---|---|---|
key |
String |
â | Unique cache key |
task |
Future<T> Function() |
â | The async function to execute and cache |
ttl |
Duration? |
null |
Time-to-live before cache expires |
forceRefresh |
bool |
false |
Bypass cache and re-execute the task |
clearCache()
| Parameter | Type | Default | Description |
|---|---|---|---|
key |
String? |
null |
Specific key to clear, or all if null |
hasCache()
| Parameter | Type | Description |
|---|---|---|
key |
String |
Key to check |
getCache<T>()
| Parameter | Type | Description |
|---|---|---|
key |
String |
Key to retrieve |
Extensions
| Extension | On | Method | Description |
|---|---|---|---|
CacheGuardExtension<T> |
Future<T> |
.cache() |
Fluent syntax for cacheGuard() |
Why This Package?
Flutter async code often leads to repetitive caching patterns:
// â What you write today â every single time
Map<String, dynamic> _cache = {};
Future<User> getUser() async {
if (_cache.containsKey('user')) {
return _cache['user'];
}
final user = await api.getUser();
_cache['user'] = user;
return user;
}
// â
With cache_guard
final user = await cacheGuard('user', () => api.getUser());
10 lines â 1 line. No bugs, no race conditions, no duplicate calls.
Architecture
lib/
âââ cache_guard.dart â Barrel export
âââ src/
âââ cache_guard_impl.dart â Core cacheGuard() + utilities
âââ cache_store.dart â In-memory storage & TTL logic
âââ extensions.dart â Future<T>.cache() extension
Zero dependencies beyond Flutter SDK. Lightweight, tree-shakeable, and fully tested.
Requirements
| Requirement | Version |
|---|---|
| Dart SDK | >=3.0.0 <4.0.0 |
| Flutter | >=3.10.0 |
| Null safety | â |
| Dependencies | None (Flutter SDK only) |
ð Support
If this package helps you build better Flutter apps, consider supporting the development:
Your support helps keep this package maintained and up-to-date. Every contribution is greatly appreciated! ð
License
MIT â see LICENSE for details.
Libraries
- cache_guard
- A lightweight Flutter package that caches async results with zero configuration â run once, reuse forever.