#cache->clear() → number KScript.Cache
删除所有键。返回删除的数量。
示例
<echo>{ $cache->clear() } keys removed</echo>echo "{ $cache->clear() } keys removed"字段是关于值的一个普通事实,用点号读取,不需要方括号:$value.name。方法会接收参数或执行操作,调用方式为 ->:$value->name(arguments),也可以把值通过管道传入:$value |> name(arguments)。
这些需要 <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参数可以命名:->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<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参数可以命名:->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参数可以命名:->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 $reportcache->pull(key, default?) → any KScript.Cache参数可以命名:->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参数可以命名:->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<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| 参数 | 接受 |
|---|---|
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| 参数 | 接受 |
|---|---|
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<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"->convert(from?: 'json'|yaml|'csv'|'markdown'|'latex', to?: 'json'|yaml|'csv'|html|'markdown'|'latex', header?: bool, as?: type, headers?: list, indented?: bool)->from(type: 'json'|yaml|'csv'|'markdown'|'latex', header?: bool, as?: type)->is_a(type: word)->is_null()->or(fallback)->to(type: 'json'|yaml|'csv'|html|'markdown'|'latex', headers?: list, indented?: bool)->to_string()->type()