BlobCache class

LRU cache for blob storage.

On native platforms blobs are stored as files in a directory, using SHA-256 hashes of blob IDs as filenames. On the web — where dart:io is unavailable at runtime — the cache transparently falls back to an in-memory LRU store with the same API. Evicts the least recently accessed entry when maxSize is exceeded. Operations on one cache instance run in call order, including reads and cleanup. Sharing a native cache directory between instances is unsupported.

Example

final cache = BlobCache(
  cacheDirectory: Directory('/path/to/cache'), // ignored on web
  maxSize: 100,
);

// Store blob
await cache.put('blob-id-123', blobData);

// Retrieve blob
final data = await cache.get('blob-id-123');

// Clean up
await cache.dispose();

When no cacheDirectory is provided on native, a temporary directory is created and automatically cleaned up on dispose.

Constructors

BlobCache({Directory? cacheDirectory, int maxSize = 100})
Creates a cache rooted at cacheDirectory or a temporary directory.

Properties

directory → Directory
The native cache directory.
no setter
hashCode → int
The hash code for this object.
no setterinherited
maxSize → int
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

cleanup() → Future<void>
Deletes every cached entry and clears in-memory tracking. The cache can be used again after cleanup completes.
dispose() → Future<void>
Cleans up temporary caches when the cache lifecycle ends. Waits for pending operations. Later operations fail with StateError. Calling dispose more than once is safe. User-provided directories remain.
get(String blobId) → Future<Uint8List?>
Returns cached bytes for blobId and refreshes its access time.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
put(String blobId, List<int> data) → Future<File?>
Persists data for blobId, evicting the oldest entry when full.
remove(String blobId) → Future<void>
Removes any stored bytes for blobId.
toString() → String
A string representation of this object.
inherited

Operators

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