Blobs / Caching
Caching
A set of files can be a cache of derived files, such as converted PDFs or
thumbnails: idle counts a file's term from its last read, and maxSize
bounds what the set holds together, removing the files used least recently
when a write would go past it. The Blobs engine keeps the bound in the set
itself, so every program that opens the set follows the same one.
const cache = await store.database('cache', { backup: false })
const pages = cache.files('pages', { idle: '30d', maxSize: '512MiB' })
await pages.put(`${hash}.pdf`, pdf, { contentType: 'application/pdf' })
const page = await pages.get(`${hash}.pdf`) // renews its 30 days and moves it up the ordercache = await store.database("cache", backup=False)
pages = cache.files("pages", idle="30d", max_size="512MiB")
await pages.put(f"{hash}.pdf", pdf, content_type="application/pdf")
page = await pages.get(f"{hash}.pdf") # renews its 30 days and moves it up the ordercache, err := sqldb.Open(ctx, store, "cache", sqldb.Backup(false))
pages, err := blobs.OpenFiles(cache, "pages", blobs.Idle(30*24*time.Hour), blobs.MaxSize(512<<20))
_, err = pages.Put(ctx, hash+".pdf", bytes.NewReader(pdf), blobs.ContentType("application/pdf"))
page, found, err := pages.Get(ctx, hash+".pdf") // renews its 30 days and moves it up the orderlet cache = store.database("cache").backup(false).open()?;
let pages = cache.files("pages").idle(Duration::from_hours(24 * 30)).max_size(512 << 20).open()?;
pages.key(&format!("{hash}.pdf")).content_type("application/pdf").put(&pdf)?;
let page = pages.get(&format!("{hash}.pdf"))?; // renews its 30 days and moves it up the orderA database opened with backup: false keeps its files out of every backup,
which suits files the program can make again. A key names what made the
file: after a new converter, write ${hash}.v2.pdf, or the cache serves the
old pages until their term ends.
A term from the last use
idle works as it does for a bucket (Expiry):
getandheadrenew a file's term.headcounts because a conditional request answered304used the file.list,usageand the scrub renew nothing, so a page that lists the cache does not keep it alive.- A renewal is written once a thirtieth of the term has passed since the last one, about once a day for 30 days, together with other writes. A read that finds less than a minute left renews before it answers.
ttlandidleexclude each other on one set; a write's ownttlgives that file a fixed term.- A connection with a
readtoken cannot open a set withidle: the open failspermission. Its reads renew nothing.
A ceiling that evicts
A write that takes the set past maxSize removes files in its own commit
until the set fits, so a write never fails because the set is full:
const thumbs = store.files('thumbs', { maxSize: '1GiB' })
await thumbs.put(`${id}.webp`, webp) // may remove the thumbnails used least recentlythumbs = store.files("thumbs", max_size="1GiB")
await thumbs.put(f"{id}.webp", webp) # may remove the thumbnails used least recentlyFiles written and never read since go first, then the files read least recently, to within an hour: a file's read gives it a day's head start over files only written. So a burst of misses, each written once and never asked for again, does not push out the pages people read every day. The head start is a day and not for good: a file read a year ago goes before one written this morning.
- A file larger than
maxSizecannot fit: its write failslimit, naming the ceiling.maxFileSizestill bounds one file below it. - The size counted is what
usagecounts, the files' listed sizes. A copy shares its bytes with its source and still counts twice, and removing one copy frees no disk until the other goes too. - One write removes at most 10,000 files. A write that 10,000 removals
cannot fit fails
limitand removes nothing. That happens only when a set holds a great many small files over a ceiling just lowered; the store's maintenance then removes the rest a batch at a time. - An upload counts at its commit, not while its pieces arrive.
- A use is written at most once an hour, or once a thirtieth of
idle. Reads through areadtoken, the command line and the MCP tools record no use, so looking at a cache from outside does not change what it keeps.
A quota that refuses
whenFull: 'refuse' turns the ceiling into a quota for files that must not
go, such as a user's uploads:
const uploads = db.files('uploads', { maxSize: '500MiB', whenFull: 'refuse' })
await uploads.put(path, data) // past 500 MiB: LimitError, and nothing is removeduploads = db.files("uploads", max_size="500MiB", when_full="refuse")
await uploads.put(path, data) # past 500 MiB: LimitError, and nothing is removeduploads, err := blobs.OpenFiles(db, "uploads", blobs.MaxSize(500<<20), blobs.WhenFull(blobs.Refuse))
_, err = uploads.Put(ctx, path, r.Body) // past 500 MiB: tinystore.ErrLimit, and nothing is removedlet uploads = db.files("uploads").max_size(500 << 20).when_full(WhenFull::Refuse).open()?;
uploads.put(&path, &data)?; // past 500 MiB: ErrorKind::Limit, and nothing is removedA replace counts the difference, so a file smaller than the one it replaces always lands; a copy counts its size; each write of a transaction counts on its own. Expiry and deletes free room as usual. A ceiling lowered below the set's size refuses what grows the set and takes what shrinks it.
The ceiling is the set's
The set keeps maxSize and whenFull in its file, so one program with
512 MiB and another with none never share a set without a bound:
- The first open with
maxSizewrites it, and from then on it holds for every handle, in this process and every other. - An open without
maxSizetakes the one the set keeps. - An open with another value fails
conflict, naming both. Opening a handle never changes what data already stored lives by, so a rolling deploy with old and new values does not move the ceiling with every open.
A ceiling changes by one call, which checks what it replaces:
await pages.setMaxSize('1GiB', { ifCurrent: '512MiB' }) // 'none' removes itawait pages.set_max_size("1GiB", if_current="512MiB") # "none" removes iterr = pages.SetMaxSize(ctx, 1<<30, blobs.IfCurrent(512<<20)) // blobs.NoMaxSize removes itpages.set_max_size(1 << 30).if_current(512 << 20).apply()?; // remove_max_size() removes itA ceiling the set keeps already is a success that writes nothing, so every
instance of a deploy may call it at its start, opening the set without
maxSize. A set that keeps another than ifCurrent fails conflict, so two
deploys never undo each other. A lower ceiling removes files at once, a batch
at a time, unless the set refuses. whenFull can be given beside it; left
out, the set keeps what it does. A read token cannot change a ceiling, nor
open a set with one it does not keep: both fail permission.
| What | Where it lives | Default |
|---|---|---|
ttl, idle | the handle that opens it | none: files never expire |
maxFileSize | the handle that opens it | none |
maxSize, whenFull | the set, in its file | none; 'evict' once given |
Free room now
trim removes files in the same order until the set holds at most to,
and leaves the ceiling where it was:
const { evictions, freed } = await pages.trim({ to: '300MiB' }) // { evictions: 184, freed: 220_000_000 }trimmed = await pages.trim(to="300MiB") # Trim(evictions=184, freed=220000000)trimmed, err := pages.Trim(ctx, 300<<20) // blobs.Trim{Evictions, Freed}let trimmed = pages.trim(300 << 20)?; // Trim { evictions, freed }It runs in batches of 10,000 files, each its own commit, so a trim of a
million files never holds the writer long; freed is the files' listed
sizes. A set without a ceiling trims too. A set whose ceiling refuses fails
invalid: those files are a user's. From a shell, tinystore files ./data trim pages --to 300MiB does the same (Files).
Seeing it work
const usage = await pages.usage() // { count: 12418, size: 522_000_000, maxSize: 536_870_912, evictions: 37 }usage = await pages.usage() # Usage(count=12418, size=522000000, max_size=536870912, evictions=37)usage, err := pages.Usage(ctx) // blobs.Usage{Count: 12418, Size: 522000000, MaxSize: 536870912, Evictions: 37}let usage = pages.usage()?; // Usage { count: 12418, size: 522000000, max_size: Some(536870912), evictions: 37 }evictions counts the files the ceiling removed since the store opened, in
this process; a trim counts in its own answer. A full set with few
evictions has a ceiling that fits; a count that climbs fast says it is low.
The store's own metrics have tinystore_files_evicted_total and
tinystore_files_bytes, by set (Self-metrics),
and tinystore status --names prints each set's files, size and ceiling:
pages 12418 files 498 MiB / 512 MiBtinystore verify ./data checks that each set's count and size, which its
writes keep, match its files.
Limits and defaults
| What | Value |
|---|---|
| files one write evicts at most | 10,000 |
| files one trim's batch removes | 10,000, each batch its own commit |
| a use written at most | once an hour, or a thirtieth of idle |
| an idle file renewed before reads | with less than a minute left |
| a file read ranks ahead by | a day |
maxSize, whenFull | none; 'evict' once a ceiling is given |
See also
- Get or create: a missing file made once while every other call of its path waits for it, the miss of a cache.
- Copies and expiry:
ttl,expire, and copies that share their bytes. - Files in a database:
db.filesand a database left out of backups. - Expiry:
idleas a bucket has it.