Vyuh Cache
A lightweight, flexible caching solution for Vyuh-framework applications. Provides time-based caching with pluggable storage backends. Perfect for reducing API calls, improving app performance, and managing temporary data.
Features โจ
- Time-based Caching: Automatic expiration with TTL (Time To Live) โฑ๏ธ
- Type Safety: Generic implementation for type-safe caching ๐ก๏ธ
- Pluggable Storage: Built-in memory storage with extensible storage interface ๐พ
- Async API: Full async support for all operations โก
- Value Generation: Built-in support for generating missing cache values ๐
- Expiration Handling: Automatic cleanup of expired entries ๐งน
Installation ๐ฆ
Add this to your package's pubspec.yaml
:
dependencies:
vyuh_cache: any
Usage ๐ก
Basic Usage
// Create a cache instance with memory storage
final cache = Cache(
CacheConfig(
storage: MemoryCacheStorage<String>(),
ttl: Duration(minutes: 5),
),
);
// Store a value
await cache.set('greeting', 'Hello, World!');
// Check if key exists
if (await cache.has('greeting')) {
// Retrieve the value
final value = await cache.get('greeting');
print(value); // Hello, World!
}
Value Generation
// Get value with automatic generation if missing
final value = await cache.build(
'user-123',
generateValue: () async {
// Expensive operation (e.g., API call)
return await fetchUserFromApi('123');
},
);
Cache Management
// Remove a specific entry
await cache.remove('old-key');
// Clear all entries
await cache.clear();
Custom Storage
Implement CacheStorage
interface for custom storage backends:
class RedisStorage<T> implements CacheStorage<T> {
@override
Future<CacheEntry<T>?> get(String key) async {
// Implement Redis get
}
@override
Future<void> set(String key, CacheEntry<T> value) async {
// Implement Redis set
}
// ... implement other methods
}
Best Practices ๐ฏ
- Choose Appropriate TTL: Set TTL based on data volatility
- Handle Errors: Always handle potential cache misses
- Type Safety: Use specific types instead of dynamic
- Memory Management: Clear unused cache entries
- Storage Selection: Use appropriate storage for your use case
Learn More ๐
- Visit docs.vyuh.tech for detailed documentation
- Check out the GitHub repository
- Report issues on the issue tracker
License ๐
This project is licensed under the terms specified in the LICENSE file.