CacheService class abstract

Abstract interface for cache storage implementations.

Implement this class to create custom cache backends such as SharedPreferences, Hive, SQLite, or any other storage solution.

Built-in Implementation

Shard provides MemoryCacheService as a ready-to-use in-memory implementation.

Custom Implementation Example

class SharedPrefsCacheService implements CacheService {
  final SharedPreferences _prefs;

  SharedPrefsCacheService(this._prefs);

  @override
  Future<void> write(String key, CacheEntry entry) async {
    await _prefs.setString(key, jsonEncode(entry.toJson()));
  }

  @override
  Future<CacheEntry?> read(String key) async {
    final json = _prefs.getString(key);
    if (json == null) return null;
    return CacheEntry.fromJson(jsonDecode(json));
  }

  @override
  Future<void> delete(String key) async {
    await _prefs.remove(key);
  }

  @override
  Future<void> clearAll() async {
    await _prefs.clear();
  }
}
Implementers

Constructors

CacheService()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

clearAll() Future<void>
Clears all cached entries.
delete(String key) Future<void>
Deletes a cache entry by key.
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.
toString() String
A string representation of this object.
inherited
write(String key, CacheEntry entry) Future<void>
Writes a cache entry with the given key.

Operators

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