使用缓存
读取值
->get()返回某个键下的值。当键不存在,或其值已过期时,返回null或你指定的默认值:
<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()可在不读取值的情况下判断值是否存在,->ttl()以时长形式给出剩余时间(永不过期时为null),->keys()按排序列出所有键:
<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()存储值
->set()保存一个值并返回它。->set_if_absent()仅当键尚不存在(或已过期)时才保存,并且在本次调用确实保存了值时返回 true,因此可用作简单的锁:
<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'
}记忆结果
大多数情况下你需要的是->remember():读取值;若值不存在,则计算并保存它。该函数只在未命中时运行:
<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计数
->increment()对数字进行累加并返回新值。不存在的键从 0 开始。累加是单步完成的,因此同时发生的请求和脚本都能被正确计数。计数会保留其启动时设定的时间限制,除非你给出新的限制:
<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"删除值
->remove()删除一个键,若该键存在则返回 true。->pull()一步完成取值并删除。->clear()删除所有键,并返回删除的数量:
<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"锁
->lock()在持有锁的情况下执行一些工作,使用同一存储的其他请求、工作进程或进程都无法同时持有该锁,并返回工作的结果。用它可以避免两个工作进程同时生成同一份报告。ttl是锁被持有的最长时间,以防进程中途停止。当锁已被其他方持有时,->lock()会立即返回null,或在等待最多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"每个锁保存在lock:之下及其键名下,并附带一个只有持有者知道的令牌,因此只有持有者才能释放它。内存存储在单个进程内加锁,文件存储在同一台机器的多个进程间加锁,数据库存储在使用该数据库的所有服务器间加锁。