Caching: KScript.Cache
Introduction
Some work is slow: a web request, reading a big file, a long calculation. When its answer doesn’t change every time, you may keep the answer and reuse it. KScript.Cache keeps values by key, for as long as you say, so the slow work is done once.
To get started, import the package and use $cache:
import KScript.Cache
import KScript.Net
$repos = $cache->remember('repos', fn() => @api('https://api.example.com/repos')->convert(from: json), ttl: 10m)
echo "{ $repos->count() } repos"
remember returns the value kept under the key. When there isn’t one, or it has run out, it calls the function, keeps what it returns for ten minutes, and returns that.
Stores
A store is where cached values live. KScript.Cache has three.
$cache keeps values in memory, for as long as kscript runs. In kscript serve, every request shares it, so a value one request keeps is there for the next:
import KScript.Cache
$cache->set('greeting', 'Hello', ttl: 1h)
echo $cache->get('greeting')
@cache(file) keeps each value in a file in the app’s cache folder (see App folders), so values last between runs. Values are kept as JSON, so a file store holds maps, lists, text, numbers and true or false:
import KScript.Cache
$weather = @cache(file)
$weather->set('london', { high: 14, low: 6 }, ttl: 3h)
echo $weather->get('london').high
@cache(database) keeps values in the kscript_cache table of the project’s database, so every server that uses the database shares them: counts, locks and rate limits then hold across all of them. The table is made the first time it is needed:
import KScript.Cache
$shared = @cache(database, name: 'reports')
$shared->increment('built')
echo "{ $shared->get('built') } reports built"
Give a store a name to keep its keys apart from other stores’: @cache(file, name: 'weather'). Stores also belong to their app, so two apps never see each other’s keys. @cache(memory) is the same store as $cache.
Using the cache
Reading values
->get() returns the value under a key. When the key isn’t there, or its value has run out, you get null, or the default you give:
import KScript.Cache
$theme = $cache->get('theme', default: 'light')
echo $theme
->has() tells you whether a value is there without reading it, ->ttl() says how long it has left as a duration (null when it never runs out), and ->keys() lists every key, sorted:
import KScript.Cache
$cache->set('token', 'abc123', ttl: 15m)
if $cache->has('token') => echo "The token runs out in { $cache->ttl('token') }"
echo $cache->keys()
Storing values
->set() keeps a value and returns it. ->set_if_absent() keeps it only when the key isn’t there yet (or has run out), and returns true when this call stored it, which makes it a simple lock:
import KScript.Cache
$cache->set('last_run', @now())
if $cache->set_if_absent('report_running', true, ttl: 5m) {
echo 'Building the report'
}
Remembering
Most of the time you want ->remember(): read the value, or work it out and keep it when it isn’t there. The function only runs on a miss:
import KScript.Cache
fn slow_total() => [1, 2, 3, 4]->sum()
$total = $cache->remember('total', fn() => @slow_total(), ttl: 1h)
echo $total
Counting
->increment() adds to a number and returns the new one. A key that isn’t there starts at 0. Adding is one step, so requests and scripts that count at the same moment all count. A count keeps the time limit it started with unless you give a new one:
import KScript.Cache
$store = @cache(file, name: 'visits')
$count = $store->increment('today', ttl: 1d)
$store->increment('downloads', by: 5)
echo "Visit { $count } today"
Removing values
->remove() removes one key and returns true if it was there. ->pull() gets a value and removes it in one go. ->clear() removes every key and returns how many there were:
import KScript.Cache
$cache->set('draft', 'Dear Joan')
$cache->remove('draft')
$cache->set('code', 123456, ttl: 10m)
$code = $cache->pull('code')
echo "Cleared { $cache->clear() } values"
Locks
->lock() runs some work while holding a lock that no other request, worker or process using the same store can hold, and returns what the work returns. Use it so two workers don’t build the same report at once. ttl is the longest the lock is held, in case the process stops halfway. When another holds the lock, ->lock() returns null at once, or after waiting up to wait:
import KScript.Cache
$store = @cache(file)
$report = $store->lock('report:daily', ttl: 5m, wait: 10s, work: fn() => 'built')
if $report == null => echo "Another worker is building it"
Each lock is kept under lock: and its key, with a token only its holder knows, so only the holder releases it. A memory store locks within one process, the file store across processes on one machine, and the database store across every server that uses the database.
Rate limits
@rate_limit(key, limit:, per:) counts attempts under a key and refuses them past the limit, in any script, not just routes. It counts in the app’s memory cache, or the store: you give:
import KScript.Cache
$limit = @rate_limit('login:ann@example.com', limit: 5, per: 1m)
if $limit->is_limited() => echo "Try again in { $limit->available_in() }"
$limit->hit()
echo "{ $limit->remaining() } tries left"
$limit->clear()
| Method | What it does |
|---|---|
->attempt(work) |
Counts an attempt and runs work, returning its value. Past the limit it throws TooManyRequests with retry_after, which a route answers with 429. |
->hit() |
Counts an attempt, such as a failed sign-in, and returns how many are left. |
->is_limited() |
True when the attempts are used up for now. |
->remaining() |
How many attempts are left in this window. |
->available_in() |
How long until attempts are allowed again: 0s when some are left. |
->clear() |
Forgets the attempts, such as after a successful sign-in. |
The window starts with the first attempt and lasts per. To count across several servers, give a shared store: @rate_limit('login:' + $email, limit: 5, per: 1m, store: @cache(database)).
Groups
A group is a small cache of its own inside a store, with the same methods. Its keys are kept apart from the store’s own and from other groups’, and ->clear_group() removes all of them at once:
import KScript.Cache
$rates = $cache->group('rates')
$rates->set('GBP', 1.0)
$rates->set('EUR', 1.17)
echo "Removed { $cache->clear_group('rates') } rates"
Time limits
ttl is how long a value is kept, written as a duration: 500ms, 30s, 10m, 2h, 1d or 1w, joined for more (1h30m). A plain number is seconds, so ttl: 90 is a minute and a half. Without a ttl, a value is kept until you remove it or clear the store.
HTTP caching
KScript.Http uses KScript.Cache for HTTP caching. A route may tag its answers with an ETag, answer 304 Not Modified when the browser already has the latest answer, set Cache-Control, and keep whole answers in the memory cache:
import KScript.Http
import "controllers/people.kscript" as people
Routes {
Get(path: /people, to: @people.index, etag: true, cache: 60)
Get(path: /stats, to: @people.stats, middleware: 'cache_response:5m')
}
See Caching and ETags for the details.