cache_guard library

A lightweight Flutter package that caches async results with zero configuration — run once, reuse forever.

Quick Start

import 'package:cache_guard/cache_guard.dart';

// Basic usage
final user = await cacheGuard('user', () => api.getUser());

// With TTL
await cacheGuard(
  'user',
  () => api.getUser(),
  ttl: Duration(minutes: 5),
);

// Extension syntax
await api.getUser().cache('user');

Extensions

CacheGuardExtension on Future<T>
Adds a cache method to any Future<T> for inline caching.

Functions

cacheGuard<T>(String key, Future<T> task(), {Duration? ttl, bool forceRefresh = false}) Future<T>
Executes task and caches the result under key.
clearCache([String? key]) → void
Clears cached values.
getCache<T>(String key) → T?
Synchronously retrieves the cached value for key, or null if no cached value exists.
hasCache(String key) bool
Returns true if a cached value exists for the given key.