FakeCacheService class

An in-memory CacheService implementation for use in tests.

Supports seeded initial entries (fresh or expired), failure injection for read/write/delete/clearAll, latency simulation, and call inspection.

final cache = FakeCacheService()
  ..seed('user_1', User(id: 1, name: 'Alice'))
  ..readError = Exception('cache offline');
Implemented types

Constructors

FakeCacheService({Map<String, CacheEntry>? initialData})
Creates a FakeCacheService, optionally pre-populated with initialData.

Properties

clearError ↔ Object?
If non-null, clearAll throws and entries are NOT cleared.
getter/setter pair
deleteCount ↔ int
Number of delete calls since construction (or last reset).
getter/setter pair
deleteError ↔ Object?
If non-null, delete throws.
getter/setter pair
entries → Map<String, CacheEntry>
Unmodifiable view of the underlying key→CacheEntry map.
no setter
hashCode → int
The hash code for this object.
no setterinherited
readCount ↔ int
Number of read calls since construction (or last reset).
getter/setter pair
readDelay ↔ Duration?
If non-null, read awaits this duration before completing.
getter/setter pair
readError ↔ Object?
If non-null, read throws this object instead of returning.
getter/setter pair
readKeys → List<String>
Keys passed to read, in order, duplicates included.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
writeCount ↔ int
Number of write calls since construction (or last reset).
getter/setter pair
writeDelay ↔ Duration?
If non-null, write awaits this duration before completing.
getter/setter pair
writeError ↔ Object?
If non-null, write throws.
getter/setter pair
writeKeys → List<String>
Keys passed to write, in order, duplicates included.
no setter

Methods

clear() → void
Wipes stored entries only.
clearAll() → Future<void>
Clears all cached entries.
override
delete(String key) → Future<void>
Deletes a cache entry by key.
override
hasKey(String key) → bool
Whether key is currently present in the cache.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
read(String key) → Future<CacheEntry?>
Reads a cache entry by key. Returns null if not found.
override
reset() → void
Wipes everything — data, counters, key lists, errors, delays.
seed(String key, Object? data, {Duration ttl = const Duration(hours: 1)}) → void
Pre-populates a fresh entry that expires in ttl (default 1 hour).
seedExpired(String key, Object? data) → void
Pre-populates an entry already expired (expiry at epoch 0).
toString() → String
A string representation of this object.
inherited
write(String key, CacheEntry entry) → Future<void>
Writes a cache entry with the given key.
override

Operators

operator ==(Object other) → bool
The equality operator.
inherited