#cache->clear() → number KScript.Cache
すべてのキーを削除します。削除された件数を返します。
例
<echo>{ $cache->clear() } keys removed</echo>echo "{ $cache->clear() } keys removed"フィールドは、値についての単純な事実で、ドットを使い角括弧なしで読み取ります: $value.name。メソッドは引数を取るか処理を行い、-> で呼び出します: $value->name(arguments)。または、値をパイプで渡して呼び出します: $value |> name(arguments)。
| フィールド | 型 | 説明 |
|---|---|---|
.kind | string | 値の保存先です。memory、file、database のいずれかになります。 |
.name | string | ストアの名前です。他のストアとキーを区別するために使います。 |
これらには <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 必須 | 任意の値 |
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 必須 | 任意の値 |
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()