Abd Alftah Salem
Hire me
←All case studies

Case study · Open source

cache_nexus

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

Every app rewrites the same caching code

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

Creator and maintainer

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

A read-through cache with a policy per call

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.

Inside one cache.get()

Inside one cache.get()cache.get(key, fetcher)policy · TTL · CacheContextMemory tierfast hitsHive CE tiersurvives restartsFetchernetwork (Dio…)SecureCacheStorageAES-GCM + GZipwatch() streamsreactive widgetsSyncEngineoffline write queue
$ 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

The choices that shaped it

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.

Concurrent calls share one fetch

Request deduplication means ten widgets asking for the same key trigger one network call.

Security as a decorator

Encryption and compression wrap any storage through SecureCacheStorage; AesGcmEncryptor uses an app-supplied key and a random nonce per value.

Per-user isolation

CacheContext scopes keys by user and role, so logging out can invalidate exactly one user's data.

05Hard parts solved

What made it more than a script

Typed data across restarts

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.

Long and Arabic keys

Keys over Hive's 255-byte limit (long URLs, long Arabic keys) are hashed transparently, and prefix deletes still find them.

Unambiguous user prefixes

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.

An offline queue that never jams

SyncEngine retries with exponential backoff; an executor that throws now counts as a failed attempt instead of blocking the queue.

06Outcome

A tested foundation for offline-first apps

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.

188tests
96%test coverage
160/160pana score (v1.1)
5cache policies

What changed in 1.1.0

Fixed17
Added10
Changed5
Deprecated2

Entries in the 1.1.0 CHANGELOG.

Commits per month · 51 total

4011
Jun 26Jul 26Aug 26Sep 26Oct 26

Source: git history of the main branch (Oct 31, 2026).

07Stack & links

Built with

© 2026 Abd Alftah Salem · Back to portfolio