KScript 문서
KO

언어

영어에서 자동 번역되었습니다. 원문은 영어입니다.

캐싱: KScript.Cache

저장소

저장소는 캐시된 값이 보관되는 곳입니다. KScript.Cache에는 세 가지 저장소가 있습니다.

$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'). 저장소는 해당 앱에도 속하므로, 두 앱은 서로의 키를 볼 수 없습니다. @cache(memory)는 $cache와 같은 저장소입니다.

Edit this page on GitHub