キャッシュヘッダー
必要な依存関係: io.ktor:ktor-server-caching-headers
コード例: caching-headers
NativeサーバーKtorはKotlin/Nativeをサポートしており、追加のランタイムや仮想マシンなしでサーバーを実行できます。のサポート: ✅
CachingHeaders プラグインは、HTTPキャッシュに使用される Cache-Control および Expires ヘッダーを設定する機能を追加します。以下の方法でキャッシュを設定できます:
- 画像、CSS、JavaScriptファイルなどの特定のコンテンツタイプに対して異なるキャッシュ戦略を設定する。
- アプリケーションレベルのグローバル、ルートレベル、または特定の呼び出し(コール)など、異なるレベルでキャッシュオプションを指定する。
依存関係の追加
CachingHeaders を使用するには、ビルドスクリプトに ktor-server-caching-headers アーティファクトを追加します:
implementation(ktorLibs.server.cachingHeaders)implementation ktorLibs.server.cachingHeaders<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-caching-headers-jvm</artifactId>
<version>${ktor_version}</version>
</dependency>CachingHeaders のインストール
CachingHeaders プラグインをアプリケーションにインストールするには、指定されたモジュールモジュールを使用すると、ルートをグループ化してアプリケーションを構造化できます。内の install 関数に渡します。 以下の例は、CachingHeaders をインストールする方法を示しています:
embeddedServer()関数の呼び出し内。Applicationクラスに明示的に定義されたmodule()拡張関数内。
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.application.*
import io.ktor.server.plugins.cachingheaders.*
fun main() {
embeddedServer(Netty, port = 8080) {
install(CachingHeaders)
// ...
}.start(wait = true)
}import io.ktor.server.application.*
import io.ktor.server.plugins.cachingheaders.*
// ...
fun Application.module() {
install(CachingHeaders)
// ...
}CachingHeaders プラグインは、特定のルートにインストールすることもできます。 これは、アプリケーションの異なるリソースに対して異なる CachingHeaders 設定が必要な場合に便利です。
CachingHeaders をインストールした後、さまざまなコンテンツタイプのキャッシュ設定を構成できます。
キャッシュの設定
CachingHeaders プラグインを設定するには、特定の ApplicationCall およびコンテンツタイプに対して指定のキャッシュオプションを提供する options 関数を定義する必要があります。caching-headers の例にある以下のコードスニペットは、プレーンテキストとHTMLに対して max-age オプション付きの Cache-Control ヘッダーを追加する方法を示しています:
fun Application.module() {
routing {
install(CachingHeaders) {
options { call, content ->
when (content.contentType?.withoutParameters()) {
ContentType.Text.Plain -> CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 3600))
ContentType.Text.Html -> CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 60))
else -> null
}
}
}
}
}CachingOptions オブジェクトは、パラメータとして Cache-Control および Expires ヘッダーの値を受け取ります:
cacheControlパラメータは CacheControl の値を受け取ります。CacheControl.MaxAgeを使用して、max-ageパラメータや可視性、再検証オプションなどの関連設定を指定できます。キャッシュを無効にするにはCacheControl.NoCache/CacheControl.NoStoreを使用できます。expiresパラメータを使用すると、ExpiresヘッダーをGMTDateまたはZonedDateTimeの値として指定できます。
ルートレベル
プラグインはグローバルだけでなく、特定のルートにインストールすることもできます。例えば、以下の例は /index ルートに対して指定したキャッシュヘッダーを追加する方法を示しています:
route("/index") {
install(CachingHeaders) {
options { call, content -> CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 1800)) }
}
get {
call.respondText("Index page")
}
}呼び出しレベル
よりきめ細かなキャッシュ設定が必要な場合は、ApplicationCall.caching プロパティを使用して呼び出しレベルでキャッシュオプションを設定できます。以下の例は、ユーザーがログインしているかどうかに応じてキャッシュオプションを設定する方法を示しています:
route("/profile") {
get {
val userLoggedIn = true
if(userLoggedIn) {
call.caching = CachingOptions(CacheControl.NoStore(CacheControl.Visibility.Private))
call.respondText("Profile page")
} else {
call.caching = CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 900))
call.respondText("Login page")
}
}
}ユーザーのログインには、Authentication および Sessions プラグインを使用できます。
