FileStorage class
A Storage that keeps each key in its own file and nothing in memory until it is read, for per-id state (one store per document, chat or note) that should cost no RAM while its store does not exist.
A HydratedStorage box holds every value in memory for as long as it is
open, so its memory grows with what is on disk. Here, opening lists the
directory and reads no file (evicting at build, with maxAge or
maxEntries, also reads their modification times), and memory holds the
values that were read or written lately, up to cacheBytes.
read stays synchronous, as stores need: a value that is not cached is read from its file right there, which blocks for as long as the read takes. Call preload first on paths where that matters, e.g. while navigating to the screen that creates the store:
final notes = await FileStorage.build(
storageDirectory: HydratedStorageDirectory('${support.path}/notes'),
);
await notes.preload('note_$id');
final store = NoteStore(id, storage: notes); // hydrates from the cache
A write goes to a temporary file that is then renamed over the old one,
so a crash leaves the old value or the new one, never a torn file.
deleteAll and clear delete one file at a time, state keys before
their version keys, so a crash midway leaves some keys behind but never
state without its version key. Values are JSON (or bytes), as
HydratedEncoding.json or HydratedEncoding.gzipJson, and are not
encrypted. One storage at a time may use a directory: a second one, from
this isolate or another process, fails to build. Isolates of one process
share its file lock and do not share statics, so the app must keep a
directory to one isolate. The lock is held until close; iOS terminates
a suspended app that holds a file lock in an app-group container, so
close a storage kept there when the app goes to the background: call
HydratedMobX.flushAll for it first, and when the app returns, build it
again without maxAge or maxEntries, move live stores over with
HydratedMobX.rebindAll, then evict. Not available on the web.
- Implemented types
Properties
- cachedBytes → int
-
The encoded bytes held in memory now.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
-
keys
→ Iterable<
String> -
Every key currently stored, including the
.__versionkeys HydratedMobX keeps beside versioned state (see HydratedMobX.versionKeySuffix).no setteroverride - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
clear(
) → Future< void> -
Clears all key value pairs from storage.
override
-
close(
) → Future< void> -
Closes the storage instance, freeing any allocated resources.
A storage instance can no longer be used once it is closed.
override
-
delete(
String key) → Future< void> -
Deletes the value for
key.override -
deleteAll(
Iterable< String> keys) → Future<void> -
Deletes the values for every key in
keys: in one operation where the storage can do that (asHydratedStoragedoes), otherwise one key at a time with every state key before its version key, so an interruption never leaves state without its version.override -
evict(
{Duration? olderThan, int? maxEntries, bool where(String key)?, bool keepLive = false}) → Future< Set< String> > - Deletes stale state, with its version key, and returns a new set of the state keys it deleted. A key's use time is when its store last hydrated from it or wrote it (kept as the file's modification time).
-
markUsed(
String key) → void -
Records that
keywas used now.override -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
preload(
String key) → Future< void> -
Reads
key(and its version key) into memory, so that a store constructed next hydrates without touching the disk. A value larger thancacheBytesis not kept in memory, so it is not read here either: the store reads it from its file. -
read(
String key) → dynamic -
Returns the value for
key, ornullwhen there is none.override -
setUsedAt(
String key, DateTime time) → Future< void> -
Records
time, or now if it is later, askey's use time, when this storage holdskey.override -
toString(
) → String -
A string representation of this object.
inherited
-
usedAt(
String key) → DateTime? -
When
keywas last used, ornullwhen no use time is known.override -
write(
String key, dynamic value) → Future< void> -
Persists the
key/valuepair.override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
build(
{required HydratedStorageDirectory storageDirectory, HydratedEncoding encoding = HydratedEncoding.gzipJson, int cacheBytes = defaultCacheBytes, Duration? maxAge, int? maxEntries, bool evictable(String key)?, Duration useResolution = HydratedStorage.defaultUseResolution, DateTime clock() = DateTime.now}) → Future< FileStorage> -
Opens the storage kept in
storageDirectory, creating it if needed.
Constants
- defaultCacheBytes → const int
-
The
cacheBytesused when none is given to build.