KScript docs
EN

Caching: KScript.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 package="KScript.Cache" />

<let name="theme" value="$cache->get('theme', default: 'light')" />
<echo value="$theme" />
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 package="KScript.Cache" />

<do>$cache->set('token', 'abc123', ttl: 15m)</do>
<if $cache->has('token')>
  <echo>The token runs out in { $cache->ttl('token') }</echo>
</if>
<echo value="$cache->keys()" />
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 package="KScript.Cache" />

<do>$cache->set('last_run', @now())</do>
<if $cache->set_if_absent('report_running', true, ttl: 5m)>
  <echo>Building the report</echo>
</if>
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 package="KScript.Cache" />

<fn name="slow_total" value="[1, 2, 3, 4]->sum()" />

<let name="total" value="$cache->remember('total', fn() => @slow_total(), ttl: 1h)" />
<echo value="$total" />
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 package="KScript.Cache" />

<let name="store" value="@cache(file, name: 'visits')" />
<let name="count" value="$store->increment('today', ttl: 1d)" />
<do>$store->increment('downloads', by: 5)</do>
<echo>Visit { $count } today</echo>
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 package="KScript.Cache" />

<do>$cache->set('draft', 'Dear Joan')</do>
<do>$cache->remove('draft')</do>
<do>$cache->set('code', 123456, ttl: 10m)</do>
<let name="code" value="$cache->pull('code')" />
<echo>Cleared { $cache->clear() } values</echo>
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 package="KScript.Cache" />

<let name="store" value="@cache(file)" />
<let name="report" value="$store->lock('report:daily', ttl: 5m, wait: 10s, work: fn() => 'built')" />
<if $report == null>
  <echo>Another worker is building it</echo>
</if>
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.