KScript docs
EN

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 package="KScript.Cache" />
<import package="KScript.Net" />

<let name="repos" value="$cache->remember('repos', fn() => @api('https://api.example.com/repos')->convert(from: json), ttl: 10m)" />
<echo>{ $repos->count() } repos</echo>
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.