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 package="KScript.Cache" />
<do>$cache->set('greeting', 'Hello', ttl: 1h)</do>
<echo value="$cache->get('greeting')" />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 package="KScript.Cache" />
<let name="weather" value="@cache(file)" />
<do>$weather->set('london', { high: 14, low: 6 }, ttl: 3h)</do>
<echo value="$weather->get('london').high" />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 package="KScript.Cache" />
<let name="shared" value="@cache(database, name: 'reports')" />
<do>$shared->increment('built')</do>
<echo>{ $shared->get('built') } reports built</echo>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.