DiskCache class

A cache that uses a bounded amount of space on a filesystem. Each cache entry has a string key and a fixed number of values. Each key must match the regex '^a-z0-9_-{1,120}$'. Values are byte sequences, accessible as streams or files.

The cache stores its data in a directory on the filesystem. This directory must be exclusive to the cache; the cache may delete or overwrite files from its directory. It is an error for multiple processes to use the same cache directory at the same time.

This cache limits the number of bytes that it will store on the filesystem. When the number of stored bytes exceeds the limit, the cache will remove entries in the background until the limit is satisfied. The limit is not strict: the cache may temporarily exceed it while waiting for files to be deleted. The limit does not include filesystem overhead or the cache journal so space-sensitive applications should set a conservative limit.

Clients call edit to create or update the values of an entry. An entry may have only one editor at one time; if a value is not available to be edited then edit will return null.

  • When an entry is being created it is necessary to supply a full set of values; the empty value should be used as a placeholder if necessary.
  • When an entry is being edited, it is not necessary to supply data for every value; values default to their previous value.

Every edit call must be matched by a call to Editor.commit or Editor.abort. Committing is atomic: a read observes the full set of values as they were before or after the commit, but never a mix of values.

Clients call get to read a snapshot of an entry. The read will observe the value at the time that get was called. Updates and removals after the call do not impact ongoing reads.

This class is tolerant of some I/O errors. If files are missing from the filesystem, the corresponding entries will be dropped from the cache. If an error occurs while writing a cache value, the edit will fail silently.

Constructors

DiskCache(FileSystem fileSystem, String directory, {int appVersion = 1, int valueCount = 1, int? maxSize})

Properties

appVersion int
final
directory String
Returns the directory where this cache stores its data.
final
fileSystem → FileSystem
final
hashCode int
The hash code for this object.
no setterinherited
journalFile String
final
journalFileBackup String
final
journalFileTmp String
final
maxSize Future<int?>
The maximum number of bytes that this cache should use to store its data.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
size Future<int>
Returns the number of bytes currently being used to store the values in this cache. This may be greater than the max size if a background deletion is pending.
no setter
valueCount int
final

Methods

close() Future<void>
Closes this cache. Stored values will remain on the filesystem.
delete() Future<void>
Closes the cache and deletes all of its stored values. This will delete all files in the cache directory including files that weren't created by the cache.
edit(String key, [int? expectedSequenceNumber]) Future<Editor?>
Returns an editor for the entry named key, or null if another edit is in progress.
emulateWindows() Future<void>
evictAll() Future<void>
Deletes all stored values from the cache. In-flight edits will complete normally but their values will not be stored.
exists(String key) Future<bool>
flush() Future<void>
Force buffered operations to the filesystem.
get(String key) Future<Snapshot?>
Returns a snapshot of the entry named key, or null if it doesn't exist is not currently readable. If a value is returned, it is moved to the head of the LRU queue.
hasJournalErrors(bool hasErrors) Future<void>
initialize() Future<void>
isClosed() Future<bool>
Returns true if this cache has been closed.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
redundantOpCount() Future<int>
remove(String key) Future<bool>
Drops the entry for key if it exists and can be removed. If the entry for key is currently being edited, that edit will complete normally but its value will not be stored.
setMaxSize(int? maxSize) Future<void>
Changes the maximum number of bytes the cache can store and queues a job to trim the existing store, if necessary.
toString() String
A string representation of this object.
inherited

Operators

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