king_cache 0.0.54
king_cache: ^0.0.54 copied to clipboard
A Flutter package for caching api data and markdown content to disk and improving app performance. It uses the http package to make api calls and use cache directory to cache data with specialized sup [...]
🌟 King Cache 🌟
This package is used to cache API results so the next time you call the same API, it will return the cached result instead of calling the API again. This will help reduce the number of api calls and improve your app's user experience.
This package uses a file-based caching system on mobile/desktop and IndexedDB on web platforms.
It gives you a couple of functions to manage the cache. It also has a log function so you can add, remove, clear and share logs. It also gives you the ability to set the cache expiry time.
Features #
- Cache API results.
- Set cache expiry time.
- Manage cache.
- Log cache.
- Clear cache.
- Share cache.
- Web Support - Full IndexedDB support for web platforms.
- Markdown Caching - Specialized caching for markdown content and tech books.
Getting started #
- Add this package to your pubspec.yaml file.
- Import the package.
- Call the functions.
Usage #
Basic API Caching #
import 'package:king_cache/king_cache.dart';
// Cache API responses
await CacheViaRestService.call(
'https://api.example.com/data',
onSuccess: (data) => print('Data: $data'),
isCacheHit: (isHit) => print('Cache hit: $isHit'),
shouldUpdate: false,
expiryTime: DateTime.now().add(Duration(hours: 1)),
);
Markdown Content Caching #
King Cache now supports specialized caching for markdown content, perfect for tech books, documentation, and educational content:
// Cache markdown content
await KingCache().cacheMarkdown(
'chapter-1',
'''# Chapter 1: Introduction
This is the first chapter of our guide.
## Section 1.1: Getting Started
Welcome to the tutorial!
''',
expiryDate: DateTime.now().add(Duration(days: 7)),
);
// Retrieve markdown content
final content = await KingCache().getMarkdownContent('chapter-1');
if (content != null && !content.isExpired) {
print('Title: ${content.title}');
print('Headers: ${content.headers}');
print('Content: ${content.content}');
}
Cache Management #
// Check if content exists
final exists = await KingCache().hasMarkdownContent('chapter-1');
// Get all markdown cache keys
final keys = await KingCache().getMarkdownKeys();
// Remove specific content
await KingCache().removeMarkdownContent('chapter-1');
// Clear all markdown cache
await KingCache().clearAllMarkdownCache();
Traditional Cache Operations #
// Basic cache operations
await KingCache().setCache('key', 'value');
final value = await KingCache().getCache('key');
await KingCache().removeCache('key');
final exists = await KingCache().hasCache('key');
// Log operations
await KingCache().storeLog('Application started');
final logs = await KingCache().getLogs;
await KingCache().clearLog;
Original Usage Examples #
For backward compatibility, here are the traditional API caching examples:
KingCache.setBaseUrl('https://jsonplaceholder.typicode.com/');
KingCache.setHeaders({'Content-Type': 'application/json'});
TextButton(
onPressed: () async {
KingCache.storeLog('Call Json Place Holder API');
await CacheViaRestService.call(
'https://jsonplaceholder.typicode.com/todos/1',
method: HttpMethod.get,
onSuccess: (data) {
// This will execute 2 times when you have data in data
debugPrint(data);
KingCache.storeLog('Response: $data');
},
onError: (data) => debugPrint(data.message),
apiResponse: (data) => debugPrint(data.message),
isCacheHit: (isHit) => debugPrint('Is Cache Hit: $isHit'),
shouldUpdate: false,
expiryTime: DateTime.now().add(const Duration(hours: 1)),
);
KingCache.storeLog('Call Json Place Holder API');
},
child: const Text('Json Place Holder API'),
),
TextButton(
onPressed: () async {
debugPrint(await KingCache.getLog);
},
child: const Text('Get Logs'),
)
TextButton(
onPressed: () => KingCache.shareLogs,
child: const Text('Share Logs'),
)
TextButton(
onPressed: () => KingCache.clearLog,
child: const Text('Clear Logs'),
)
TextButton(
onPressed: () => KingCache.clearAllCache,
child: const Text('Clear All Cache'),
)
🧑🏻 Author #
Rohit Jain
🤝 Support
You can expect responsive replies and fast fixes to any issues that appear.
🎀 Contributions (GitHub Flow), 🔥 issues, and 🥮 feature requests are most welcome!
💙 If you like this project, Give it a ⭐ and Share it with friends!
💰 Donations Links
Web Support #
King Cache now supports web platforms with IndexedDB storage! The same API works seamlessly across all platforms:
- Mobile/Desktop: File-based storage
- Web: IndexedDB storage
- API: Identical across all platforms
See WEB_SUPPORT.md for detailed information about web support features.
Markdown Caching #
King Cache includes specialized support for markdown content and tech book organization:
- Markdown Content: Cache markdown with automatic title/header extraction
- Tech Books: Organize content by books, chapters, and sections
- Expiry Management: Automatic content expiration
- Platform Support: Works on all platforms (mobile, desktop, web)
See MARKDOWN_CACHING.md for detailed documentation and examples.
Made with Flutter & ❤️ in India