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.
Description
Section titled “Description”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.
Learn more
Section titled “Learn more”Creation
Section titled “Creation”| Returns | Signature | Summary |
|---|---|---|
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. |
Properties
Section titled “Properties”| Property | Type | Default | Summary |
|---|---|---|---|
| retention | Duration | Duration.ofDays(30L) | How long an entry stays in the cache without being read before JOID deletes it. |
Methods
Section titled “Methods”| Returns | Signature | Summary |
|---|---|---|
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. |
File | compute(String key, FileCacheWriter writer) | Returns the file of key, writing it with writer first when the area does not have it. |
File | store(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. |
boolean | remove(String key) | Deletes the entry key and returns true when there was one. |
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. |
String | getName() | Returns the name of the area, the name of its folder. |
Constants and fields
Section titled “Constants and fields”| Type | Name | Value | Summary |
|---|---|---|---|
String | MARKER | ".area" | The name of the file, .area, that marks a folder of the cache folder as an area and holds its retention. |
Creation details
Section titled “Creation details”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 _.
Property details
Section titled “Property details”retention
Section titled “retention”this retention(Duration retention)fluentDuration getRetention()
Default: Duration.ofDays(30L)
Throws:
IllegalArgumentException: The retention … of the cache area … is not positive, pass a duration above 0IllegalStateException: 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.
Method details
Section titled “Method details”getDirectory
Section titled “getDirectory”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.
compute
Section titled “compute”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.
remove
Section titled “remove”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.
getName
Section titled “getName”String getName()
Returns the name of the area, the name of its folder.
Constant and field details
Section titled “Constant and field details”MARKER
Section titled “MARKER”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
