cached
1.8.1, released a year ago by iteo.com
Simplifies and speeds up creation of in-memory and persistent caching mechanisms using code generation.
What it does
The package generates caching code for Dart classes and methods. It supports LRU eviction, time-to-live (TTL), conditional caching, persistent storage integration, and stream updates.
AI-written summary of version 1.8.1, based on the package's README and metadata. Report an inaccuracy
Use it when
- You want to automatically cache API responses or expensive method calls in memory or persistent storage.
- You need LRU cache eviction or TTL-based expiration without writing manual cache boilerplate.
Skip it when
- You do not want to use code generation or build_runner in your project.
- You need a general-purpose key-value store without class-level annotations.
Strengths
- Reduces boilerplate for caching method results.
- Supports LRU cache algorithm, TTL, and conditional caching via 'where'.
- Supports persistent storage integrations and cache streams.
Weaknesses
- Requires running build_runner for code generation.
- Deprecated persistentStorage parameter requires migrating to newer annotations.
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- Yes (build_runner)
- Direct dependencies
- 7
Add cached and build_runner to dev_dependencies, add cached_annotation to dependencies, and run build_runner.
@WithCache()
abstract mixin class RemoteRepository implements Repository, _$RemoteRepository {
factory RemoteRepository({required SomeApiDataSource dataSource}) = _RemoteRepository;
@Cached()
Future<SomeResponseType> getSthData() {
return dataSource.getData();
}
}Alternatives
Packages that do the same job and could replace cached.
- flutter_cache_manager
If you specifically need to cache web files, images, or downloads instead of arbitrary method results.
91Compare - shared_preferences
If you just need simple key-value persistence without method-level caching annotations.
97Compare
Works with
Packages commonly used together with cached: ones it builds on, ones built on it, and its own family.
- analyzercached depends on itPerforms static analysis of Dart code for tool integration and embedding.
- buildcached depends on itPackage for authoring build_runner compatible code generators.
- source_gencached depends on itSource code generation builders and utilities for the Dart build system.
- source_helpercached depends on itUtilities to help with Dart source code generation and string literal formatting.
- cached_annotationcached depends on itProvides annotations for the Cached code‑generator to define caching behavior.
- build_runnerUsed togetherA build system for Dart code generation and modular compilation.