flutter_smart_cache_manager 0.0.6
flutter_smart_cache_manager: ^0.0.6 copied to clipboard
A lightweight Flutter caching library with TTL, cache policies, offline fallback, disk persistence, and background refresh for optimized API performance.
import 'package:flutter/material.dart';
import 'package:flutter_smart_cache_manager/flutter_smart_cache_manager.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(home: CacheDemo());
}
}
/// Shows both storage backends side by side so the difference is visible:
///
/// * `MemoryStorage` — RAM only. "API calls" resets to 0 every time the app
/// is fully closed and reopened, because the cache is gone with it.
/// * `PersistentStorage` — written to disk. Fully close the app and reopen
/// it: this card still shows the same cached text, and "API calls" stays
/// at 1 (won't refetch until the 5-minute TTL expires).
///
/// Tap "Fetch" on either card to trigger a (simulated) network call.
class CacheDemo extends StatelessWidget {
const CacheDemo({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Smart Cache Manager Demo')),
body: const Padding(
padding: EdgeInsets.all(16),
child: Column(
children: [
_CacheCard(
title: 'MemoryStorage',
subtitle: 'RAM only — cleared on every app restart',
cacheKey: 'memory_user',
storageBuilder: _memoryStorage,
),
SizedBox(height: 16),
_CacheCard(
title: 'PersistentStorage',
subtitle: 'Written to disk — survives app restarts',
cacheKey: 'persistent_user',
storageBuilder: _persistentStorage,
),
],
),
),
);
}
}
Future<CacheStorage> _memoryStorage() async => MemoryStorage();
Future<CacheStorage> _persistentStorage() => PersistentStorage.create();
class _CacheCard extends StatefulWidget {
const _CacheCard({
required this.title,
required this.subtitle,
required this.cacheKey,
required this.storageBuilder,
});
final String title;
final String subtitle;
final String cacheKey;
final Future<CacheStorage> Function() storageBuilder;
@override
State<_CacheCard> createState() => _CacheCardState();
}
class _CacheCardState extends State<_CacheCard> {
CacheManager? _cache;
String _text = 'Not loaded yet';
int _apiCalls = 0;
bool _loading = false;
@override
void initState() {
super.initState();
_load();
}
Future<void> _load() async {
setState(() => _loading = true);
// Built once per card and reused — a fresh CacheManager still finds
// whatever PersistentStorage already has on disk from a previous run.
_cache ??= CacheManager(await widget.storageBuilder());
final result = await _cache!.get<String>(
key: widget.cacheKey,
ttl: const Duration(minutes: 5),
fetcher: () async {
await Future.delayed(const Duration(seconds: 1));
_apiCalls++;
print('[${widget.title}] API CALLED (call #$_apiCalls)');
return 'Hello Vishal @ ${DateTime.now().toIso8601String()}';
},
);
if (!mounted) return;
setState(() {
_text = result;
_loading = false;
});
}
@override
Widget build(BuildContext context) {
return Card(
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.title,
style: Theme.of(context).textTheme.titleMedium,
),
Text(widget.subtitle, style: Theme.of(context).textTheme.bodySmall),
const SizedBox(height: 8),
Text(_text),
const SizedBox(height: 4),
Text('API calls this session: $_apiCalls'),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _loading ? null : _load,
child: Text(_loading ? 'Loading...' : 'Fetch'),
),
],
),
),
);
}
}