KScript ドキュメント
JA

言語

英語から自動翻訳されています。原文は英語です。

キャッシュ: KScript.Cache

ストア

ストアはキャッシュされた値が保存される場所です。KScript.Cache には 3 種類あります。

$cache は kscript が動いている間、値をメモリに保持します。kscript serve では、すべてのリクエストがそれを共有するため、あるリクエストが保持した値は次のリクエストでも利用できます:

<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) は、アプリの cache フォルダ内のファイルに各値を保存します(アプリフォルダを参照)。そのため値は実行をまたいで残ります。値は JSON として保存されるため、ファイルストアはマップ、リスト、テキスト、数値、true や 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) はプロジェクトのデータベース内の kscript_cache テーブルに値を保持するため、そのデータベースを使うすべてのサーバーがそれを共有します。カウント、ロック、レート制限はすべてのサーバーをまたいで機能します。テーブルは初めて必要になったときに作成されます:

<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"

ストアに名前を付けると、そのキーを他のストアのキーと分けられます: @cache(file, name: 'weather')。ストアはそれぞれのアプリにも属するため、2 つのアプリが互いのキーを見ることはありません。@cache(memory) は $cache と同じストアです。

Edit this page on GitHub