KScript 文档
ZH

cache 字段和方法

字段是关于值的一个普通事实,用点号读取,不需要方括号:$value.name。方法会接收参数或执行操作,调用方式为 ->:$value->name(arguments),也可以把值通过管道传入:$value |> name(arguments)。

字段

字段类型说明
.kindstring值的存放位置:内存、文件或数据库。
.namestring存储的名称,用于将其键与其他存储的键区分开。

来自 KScript.Cache

这些需要 <import package="KScript.Cache" />import KScript.Cache。参见 KScript.Cache。

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

删除所有键。返回删除的数量。
示例
<echo>{ $cache->clear() } keys removed</echo>
echo "{ $cache->clear() } keys removed"

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

清空此名称的缓存组,并返回已删除的条目数。
参数接受
name 必填string
示例
<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

保存在某个键下的值;若该键不存在或已过期,则返回默认值 (null)。

参数可以命名:->get(…, default: …)。

示例
<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

此存储中一个独立的缓存命名空间。
参数接受
name 必填string
示例
<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。
示例
<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

对某个键下的数字加上一个值(默认为 1;若键不存在则从 0 开始),并返回新的数字。
参数接受
key 必填任意值
by 可选number
ttl 可选duration

参数可以命名:->increment(…, by: …, ttl: …)。

示例
<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

当前保存且未过期的键。
示例
<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

在持有锁的情况下运行工作,该锁不能被使用此存储的其他请求或工作进程持有,并返回工作的结果。ttl 是锁的最长保持时间,以防进程意外中止。若锁已被他人持有,则立即返回 null,或等待最多 wait 时长后再返回。
参数接受
key 必填任意值
ttl 必填duration
work 必填fn
wait 可选duration

参数可以命名:->lock(…, ttl: …, work: …)。

示例
<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

取出某个键下保存的值,随后将其删除;若该键不存在,则返回默认值 (null)。

参数可以命名:->pull(…, default: …)。

示例
<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

返回某个键下的值;若不存在,则调用 compute,保存其返回值并将其返回。
参数接受
key 必填任意值
compute 必填fn
ttl 可选duration

参数可以命名:->remember(…, compute: …, ttl: …)。

示例
<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

删除一个键。如果该键存在则返回 True。
示例
<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

在某个键下保存一个值。ttl 为保存时长,例如 30s、10m、2h 或 1d(纯数字则以秒计)。若不指定,则该值会一直保存直到被删除。
参数接受
key 必填任意值
value required任意值
ttl 可选duration

参数可以命名:->set(…, value: …, ttl: …)。

示例
<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

仅当键不存在或已过期时才保存值。若本次调用保存了该值则返回 True。
参数接受
key 必填任意值
value required任意值
ttl 可选duration

参数可以命名:->set_if_absent(…, value: …, ttl: …)。

示例
<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

某个键距离过期的剩余时长(duration 类型);若该键永不过期或不存在,则返回 null。
示例
<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"

来自 any 的方法