KScript docs
EN

cache fields and methods

A field is a plain fact about the value, read with a dot and no brackets: $value.name. A method takes arguments or does work, and is called with ->: $value->name(arguments), or with the value piped in: $value |> name(arguments).

Fields

FieldTypeDescription
.kindstringWhere the values are kept: memory, file or database.
.namestringThe store's name, which keeps its keys apart from other stores'.

From KScript.Cache

These need <import package="KScript.Cache" />import KScript.Cache. See KScript.Cache.

#cache->clear() → number KScript.Cache

Removes every key. Returns how many there were.
Example
<echo>{ $cache->clear() } keys removed</echo>
echo "{ $cache->clear() } keys removed"

#cache->clear_group(name: string) → number KScript.Cache

Clears this named cache group and returns how many entries were removed.
ParameterTakes
name requiredstring
Example
<do>$cache->group('users')->set('1', 'Joan')</do>
<echo>{ $cache->clear_group('users') } removed</echo>
$cache->group('users')->set('1', 'Joan')
echo "{ $cache->clear_group('users') } removed"

#cache->get(key, default?) → any KScript.Cache

The value kept under a key, or default (null) when it isn't there or has run out.

Arguments can be named: ->get(…, default: …).

Example
<do>$cache->set('greeting', 'hello')</do>
<echo>{ $cache->get('greeting') } { $cache->get('missing', default: 'none') }</echo>
$cache->set('greeting', 'hello')
echo "{ $cache->get('greeting') } { $cache->get('missing', default: 'none') }"

#cache->group(name: string) → cache KScript.Cache

A separate cache namespace inside this store.
ParameterTakes
name requiredstring
Example
<let name="users" value="$cache->group('users')" />
<do>$users->set('1', 'Joan')</do>
<echo>{ $users->get('1') } { $cache->get('1', default: 'not in the main cache') }</echo>
$users = $cache->group('users')
$users->set('1', 'Joan')
echo "{ $users->get('1') } { $cache->get('1', default: 'not in the main cache') }"

#cache->has(key) → bool KScript.Cache

True when a value is kept under the key and hasn't run out.
Example
<do>$cache->set('token', 'abc', ttl: 5m)</do>
<echo>{ $cache->has('token') }</echo>
$cache->set('token', 'abc', ttl: 5m)
echo $cache->has('token')

#cache->increment(key, by?: number, ttl?: duration) → number KScript.Cache

Adds to the number under a key (1 by default; 0 when it isn't there) and returns the new number.
ParameterTakes
key requiredany value
by optionalnumber
ttl optionalduration

Arguments can be named: ->increment(…, by: …, ttl: …).

Example
<do>$cache->increment('visits'); $cache->increment('visits', by: 2)</do>
<echo>{ $cache->get('visits') }</echo>
$cache->increment('visits')
$cache->increment('visits', by: 2)
echo $cache->get('visits')

#cache->keys() → list KScript.Cache

The keys that are kept and haven't run out.
Example
<do>$cache->set('a', 1); $cache->set('b', 2)</do>
<echo>{ $cache->keys() }</echo>
$cache->set('a', 1)
$cache->set('b', 2)
echo $cache->keys()

#cache->lock(key, ttl: duration, work: fn, wait?: duration) → any KScript.Cache

Runs work while holding a lock that no other request or worker using this store can hold, and returns what work returns. ttl is the longest the lock is kept, in case the process stops. When another holds it, the result is null at once, or after waiting up to wait.
ParameterTakes
key requiredany value
ttl requiredduration
work requiredfn
wait optionalduration

Arguments can be named: ->lock(…, ttl: …, work: …).

Example
<let name="report" value="$cache->lock('report', ttl: 1m, work: fn() => 'built once')" />
<echo>{ $report }</echo>
$report = $cache->lock('report', ttl: 1m, work: fn() => 'built once')
echo $report

#cache->pull(key, default?) → any KScript.Cache

The value kept under a key, which is then removed; default (null) when it isn't there.

Arguments can be named: ->pull(…, default: …).

Example
<do>$cache->set('code', 1234)</do>
<echo>{ $cache->pull('code') } { $cache->pull('code', default: 'gone') }</echo>
$cache->set('code', 1234)
echo "{ $cache->pull('code') } { $cache->pull('code', default: 'gone') }"

#cache->remember(key, compute: fn, ttl?: duration) → any KScript.Cache

The value under a key; when there isn't one, calls compute, keeps what it returns and returns that.
ParameterTakes
key requiredany value
compute requiredfn
ttl optionalduration

Arguments can be named: ->remember(…, compute: …, ttl: …).

Example
<let name="squares" value="$cache->remember('squares', fn() => [1, 2, 3]->map(fn(n) => $n * $n), ttl: 10m)" />
$squares = $cache->remember('squares', fn() => [1, 2, 3]->map(fn(n) => $n * $n), ttl: 10m)

#cache->remove(key) → bool KScript.Cache

Removes a key. True when it was there.
Example
<do>$cache->set('draft', 'hi'); $cache->remove('draft')</do>
<echo>{ $cache->has('draft') }</echo>
$cache->set('draft', 'hi')
$cache->remove('draft')
echo $cache->has('draft')

#cache->set(key, value, ttl?: duration) → any KScript.Cache

Keeps a value under a key. ttl is how long, such as 30s, 10m, 2h or 1d (a plain number is seconds). Without it the value is kept until it is removed.
ParameterTakes
key requiredany value
value requiredany value
ttl optionalduration

Arguments can be named: ->set(…, value: …, ttl: …).

Example
<do>$cache->set('user:1', { name: 'Joan' }, ttl: 600)</do>
$cache->set('user:1', { name: 'Joan' }, ttl: 600)

#cache->set_if_absent(key, value, ttl?: duration) → bool KScript.Cache

Stores a value only when the key is missing or expired. True if this call stored it.
ParameterTakes
key requiredany value
value requiredany value
ttl optionalduration

Arguments can be named: ->set_if_absent(…, value: …, ttl: …).

Example
<echo>{ $cache->set_if_absent('lock:report', 'me', ttl: 1m) } { $cache->set_if_absent('lock:report', 'you') }</echo>
echo "{ $cache->set_if_absent('lock:report', 'me', ttl: 1m) } { $cache->set_if_absent('lock:report', 'you') }"

#cache->ttl(key) → duration KScript.Cache

How long until a key runs out, as a duration, or null when it never does or isn't there.
Example
<do>$cache->set('code', 1234, ttl: 1m)</do>
<echo>{ $cache->ttl('code') } left</echo>
$cache->set('code', 1234, ttl: 1m)
echo "{ $cache->ttl('code') } left"

Methods from any