Skip to content

FileCacheArea

final class FileCacheArea
Package: dev.joid.lib.cache · Module: core · Source: FileCacheArea.java

One folder of the cache, whose entries are written once by key, read many times, and deleted after a period without use.

Get one with of(name), and keep it in a constant. Each entry is written into a temporary file and moved into place in one step, so a reader never sees a half-written entry, and two threads that compute the same key write it once. Reading an entry marks it as used; the retention counts from the last use. The methods are thread-safe; call them off the render thread, since they touch the disk.

ReturnsSignatureSummary
FileCacheAreaof(String name) factoryReturns the area name, the same instance for the same name, in the folder of that name under the cache folder.

PropertyTypeDefaultSummary
retentionDurationDuration.ofDays(30L)How long an entry stays in the cache without being read before JOID deletes it.

ReturnsSignatureSummary
FilegetDirectory()Returns the folder of the area under the cache folder; it is created by the first write.
Fileentry(String key)Returns the file of key and marks it as used, or null when the area has no such entry.
Filecompute(String key, FileCacheWriter writer)Returns the file of key, writing it with writer first when the area does not have it.
Filestore(FileCacheWriter writer)Writes a content with writer and returns its file, named after the SHA-1 of the content: the same content is stored once.
booleanremove(String key)Deletes the entry key and returns true when there was one.
longclean(long now, AtomicInteger files)Deletes the entries unused since now minus the retention, and the temporary files older than one day, adds the number of deleted files to files and returns the bytes freed.
StringgetName()Returns the name of the area, the name of its folder.
TypeNameValueSummary
StringMARKER".area"The name of the file, .area, that marks a folder of the cache folder as an area and holds its retention.

FileCacheArea of(String name) factory

Returns the area name, the same instance for the same name, in the folder of that name under the cache folder. Throws IllegalArgumentException when name is not a plain file name: a letter or a digit, then letters, digits, ., - and _.

  • this retention(Duration retention) fluent
  • Duration getRetention()

Default: Duration.ofDays(30L)

Throws:

  • IllegalArgumentException: The retention … of the cache area … is not positive, pass a duration above 0
  • IllegalStateException: The cache area … is already declared with a retention of …, it cannot be declared again with …: give one of them another name

How long an entry stays in the cache without being read before JOID deletes it. retention(Duration) declares it and writes it into the folder, so the cleaning at the next start reads it. Throws IllegalArgumentException for a duration of zero or less, and IllegalStateException when the area was already declared with another retention.

File getDirectory()

Returns the folder of the area under the cache folder; it is created by the first write.

File entry(String key)

Returns the file of key and marks it as used, or null when the area has no such entry. Throws IllegalArgumentException when key is not a plain file name.

File compute(String key, FileCacheWriter writer)

Returns the file of key, writing it with writer first when the area does not have it. Throws IOException when the folder cannot be created, when writer fails, or when it writes nothing.

File store(FileCacheWriter writer)

Throws:

  • IOException: The content written into the cache area … cannot be hashed at …, the writer of store must write a file

Writes a content with writer and returns its file, named after the SHA-1 of the content: the same content is stored once. Throws IOException when the folder cannot be created, when writer fails, or when it writes nothing.

boolean remove(String key)

Deletes the entry key and returns true when there was one. An entry still open elsewhere is left to the next cleaning, with a dev-mode warning.

long clean(long now, AtomicInteger files)

Deletes the entries unused since now minus the retention, and the temporary files older than one day, adds the number of deleted files to files and returns the bytes freed. FileCache.clean calls it for every area.

String getName()

Returns the name of the area, the name of its folder.

String MARKER

The name of the file, .area, that marks a folder of the cache folder as an area and holds its retention.


fluent returns the instance, typed for chaining · factory creates an instance · signal also accepts a value that follows signals, as a signal, an expression or a lambda (When does JOID compute this value again?) · hook called by JOID, override it · abstract you implement it · default interface method with a body · static called on the class · protected visible to subclasses

Report a problem with this page