Five explicit cache policies
cacheFirst, networkFirst, cacheOnly, networkOnly and staleWhileRevalidate make the trade-off between freshness and speed a per-call decision instead of hidden behaviour.
Case study · Open source
An offline-first data layer for Flutter: one call reads through a two-tier cache with a policy, keeps typed data across restarts, encrypts it, scopes it per user and syncs offline writes.
01Problem
Apps that must work on weak or no connectivity end up hand-writing the same pieces: memory and disk caches, expiry, "use the cache if the network fails", per-user data on shared devices, and a queue for writes made offline. cache_nexus puts all of that behind one API.
02My role
I designed and published the package (1.0.0 in June 2026) and maintain it; 1.1.0, released on Oct 31, 2026, focused on correctness and security without breaking the 1.0 API.
03How it works
cache.get() takes a key, a fetcher, a TTL and one of five policies. It checks memory, then Hive CE on disk, and only calls the fetcher when the policy says so. Values pass through optional encryption and compression, keys are prefixed by the current user context, and watchers are notified when a value changes.
$ flutter pub add cache_nexus # Dart final posts = await cache.get<List<Post>>( key: 'posts', fetcher: () => api.getPosts(), ttl: const Duration(minutes: 10), policy: CachePolicy.staleWhileRevalidate, );
04Key technical decisions
cacheFirst, networkFirst, cacheOnly, networkOnly and staleWhileRevalidate make the trade-off between freshness and speed a per-call decision instead of hidden behaviour.
Request deduplication means ten widgets asking for the same key trigger one network call.
Encryption and compression wrap any storage through SecureCacheStorage; AesGcmEncryptor uses an app-supplied key and a random nonce per value.
CacheContext scopes keys by user and role, so logging out can invalidate exactly one user's data.
05Hard parts solved
Hive returns List<dynamic> and Map<dynamic, dynamic>, which 1.0 treated as misses — re-fetching and even overwriting offline data. 1.1 normalises maps, decodes common collections and adds CacheAdapter for app classes.
Keys over Hive's 255-byte limit (long URLs, long Arabic keys) are hashed transparently, and prefix deletes still find them.
User ali with role admin could collide with user ali_admin; 1.1 fixes the prefix format and migrates 1.0 entries on first read.
SyncEngine retries with exponential backoff; an executor that throws now counts as a failed attempt instead of blocking the queue.
06Outcome
1.1.0 shipped with no breaking API changes: every 1.0 call keeps working, storage moved to the maintained Hive CE (which reads existing Hive boxes), and a dev overlay shows cache hits, misses and network events live.
Entries in the 1.1.0 CHANGELOG.
Source: git history of the main branch (Oct 31, 2026).
07Stack & links
© 2026 Abd Alftah Salem · Back to portfolio