{"id":362753,"date":"2024-05-21T01:41:30","date_gmt":"2024-05-21T01:41:30","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=362753"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-05-21T01:41:30","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=362753","title":{"rendered":"<span>\u0421\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u0432 Ktor \u0434\u043b\u044f \u0441\u0430\u043c\u044b\u0445 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0445<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h2>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h2>\n<figure class=\"float full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/13c\/e29\/f64\/13ce29f64fb16734f4450b9a67b452a9.jpg\" width=\"1024\" height=\"1024\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/13c\/e29\/f64\/13ce29f64fb16734f4450b9a67b452a9.jpg\" data-blurred=\"true\"\/><\/figure>\n<p>\u042d\u0442\u043e \u0431\u044b\u043b \u0442\u0435\u043f\u043b\u044b\u0439 \u043e\u0441\u0435\u043d\u043d\u0438\u0439 \u0432\u0435\u0447\u0435\u0440, \u043a\u043e\u0433\u0434\u0430 \u043f\u0435\u0440\u0435\u0434\u043e \u043c\u043d\u043e\u0439 \u0432\u0441\u0442\u0430\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 &#171;\u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0435 \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u0441\u0435\u0440\u0432\u0438\u0441\u0435&#187;. \u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b \u0447\u0435\u0433\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u043e\u0449\u0435, \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0432 <a href=\"https:\/\/ktor.io\/\" rel=\"noopener noreferrer nofollow\"><strong>Ktor<\/strong><\/a> \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u043f\u0430\u0440\u043e\u0439 \u0441\u0442\u0440\u043e\u0447\u0435\u043a \u043a\u043e\u0434\u0430.  \u041e\u0434\u043d\u0430\u043a\u043e \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u043a\u0430\u0437\u0430\u043b\u0430\u0441\u044c \u043d\u0435 \u0442\u0430\u043a\u043e\u0439 \u0440\u0430\u0434\u0443\u0436\u043d\u043e\u0439: <strong>Ktor<\/strong> \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e <a href=\"https:\/\/ktor.io\/docs\/caching.html\" rel=\"noopener noreferrer nofollow\"><strong>Cache Headers plugin<\/strong><\/a><strong>,<\/strong> \u043a\u043e\u0442\u043e\u0440\u044b\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435. \u0427\u0442\u043e \u0436, \u0432\u043e\u043e\u0440\u0443\u0436\u0438\u0432\u0448\u0438\u0441\u044c <strong>IDE<\/strong>, \u044f \u043d\u0430\u0447\u0430\u043b \u0432\u0430\u044f\u0442\u044c \u0441\u0432\u043e\u0439 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434 &#8212; <a href=\"https:\/\/github.com\/Scogun\/ktor-simple-cache\" rel=\"noopener noreferrer nofollow\"><strong>Ktor Simple Cache<\/strong><\/a>.<\/p>\n<h2>\u041f\u0435\u0440\u0432\u044b\u0435 \u0448\u0430\u0433\u0438<\/h2>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u044f \u0437\u0430\u0434\u0430\u043b\u0441\u044f \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c: &#171;<em>\u0410 \u043a\u0430\u043a \u0432\u043e\u043e\u0431\u0449\u0435 \u0447\u0442\u043e-\u0442\u043e \u043f\u0440\u0438\u043a\u0440\u0443\u0442\u0438\u0442\u044c \u043a <\/em><strong><em>Ktor<\/em><\/strong><em>?<\/em>&#171;. \u041e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c, \u0447\u0442\u043e <strong>Ktor<\/strong> \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u043c \u0447\u0435\u0440\u0435\u0437 <strong>plugin&#8217;\u044b<\/strong>, \u043e \u0447\u0435\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432 <a href=\"https:\/\/ktor.io\/docs\/custom-plugins.html\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>.<\/p>\n<p>\u0414\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0438\u0441\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0447\u0430\u0441\u0442\u0438 <strong>plugin&#8217;\u0430<\/strong>:<\/p>\n<details class=\"spoiler\">\n<summary>\u041f\u0435\u0440\u0432\u044b\u0439 \u043a\u043e\u0434<\/summary>\n<div class=\"spoiler__content\">\n<p>\u041f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440 \u043a\u044d\u0448\u0430:<\/p>\n<pre><code class=\"kotlin\">abstract class SimpleCacheProvider(config: Config) {      val invalidateAt = config.invalidateAt      abstract fun getCache(key: String): Any?      abstract fun setCache(key: String, content: Any, invalidateAt: Duration?)      open class Config protected constructor() {           var invalidateAt: Duration = 5.minutes     } }<\/code><\/pre>\n<p>\u0422\u0435\u043b\u043e plugin&#8217;\u0430 \u0441 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0439:<\/p>\n<pre><code class=\"kotlin\">class SimpleCacheConfig {      internal var provider: SimpleCacheProvider? = null }  class SimpleCache(internal var config: SimpleCacheConfig) {      companion object : BaseApplicationPlugin&lt;Application, SimpleCacheConfig, SimpleCache> {         override val key: AttributeKey&lt;SimpleCache> = AttributeKey(\"SimpleCacheHolder\")          override fun install(pipeline: Application, configure: SimpleCacheConfig.() -> Unit): SimpleCache {             return SimpleCache(SimpleCacheConfig().apply { configure })         }     } }<\/code><\/pre>\n<p>\u041d\u0443 \u0438 \u0441\u0430\u043c plugin \u0441 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0435\u0439:<\/p>\n<pre><code class=\"kotlin\">class SimpleCachePluginConfig {      var invalidateAt: Duration? = null }  val SimpleCachePlugin = createRouteScopedPlugin(name = \"SimpleCachePlugin\", ::SimpleCachePluginConfig) {     val provider = application.plugin(SimpleCache).config.provider       onCall {         val cache = provider.getCache(it.request.uri)         if (cache != null) {             it.respond(cache)         }     }     onCallRespond { call, body ->         provider.setCache(call.request.uri, body, pluginConfig.invalidateAt)     } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0418\u0434\u0435\u044f \u0434\u043e\u043d\u0435\u043b\u044c\u0437\u044f \u043f\u0440\u043e\u0441\u0442\u0430\u044f:<\/p>\n<ul>\n<li>\n<p>\u041f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 &#8212; \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <strong>OnCall<\/strong><\/p>\n<\/li>\n<li>\n<p>\u041b\u0435\u0437\u0435\u043c \u0432 \u043a\u044d\u0448 \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u043c\u0435\u0442\u043e\u0434\u0430<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043a\u044d\u0448 \u043d\u0430\u0439\u0434\u0435\u043d &#8212; \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0435\u0442 &#8212; \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c <strong>route<\/strong> \u043c\u0435\u0442\u043e\u0434, \u0430 \u043e\u0442\u0432\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0432 \u043a\u044d\u0448 \u0432\u043d\u0443\u0442\u0440\u0438 <strong>onCallRespond<\/strong>.<\/p>\n<\/li>\n<\/ul>\n<p>\u042f \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u043b, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 <strong>OnCall<\/strong> \u0441\u0434\u0435\u043b\u0430\u0442\u044c <strong>it.respond(cache)<\/strong> &#8212; <strong>onCallRespond<\/strong> \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0435 \u0431\u0443\u0434\u0435\u0442. \u041e\u0434\u043d\u0430\u043a\u043e \u043f\u0435\u0440\u0432\u044b\u0439 \u0436\u0435 unit test \u043f\u043e\u043a\u0430\u0437\u0430\u043b, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a. \u0418\u0442\u0430\u043a, \u043c\u043d\u0435 \u043d\u0430\u0434\u043e \u0431\u044b\u043b\u043e \u043a\u0430\u043a-\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 <strong>OnCallRespond<\/strong> \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u0437\u044f\u0442\u044b \u0438\u0437 \u043a\u044d\u0448\u0430 \u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438\u0445 \u043d\u0435 \u043d\u0430\u0434\u043e. \u0411\u043b\u0430\u0433\u043e, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 Ktor \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 <a href=\"https:\/\/ktor.io\/docs\/custom-plugins.html#call-state\" rel=\"noopener noreferrer nofollow\"><strong>Call State<\/strong><\/a>.<\/p>\n<p>\u0418\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e <strong>plugin<\/strong> \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u0442\u0430\u043a:<\/p>\n<pre><code class=\"kotlin\">val SimpleCachePlugin = createRouteScopedPlugin(name = \"SimpleCachePlugin\", ::SimpleCachePluginConfig) {     val provider = application.plugin(SimpleCache).config.provider     val isResponseFromCacheKey = AttributeKey&lt;Boolean>(\"isResponseFromCacheKey\")     onCall {         val cache = provider.getCache(it.request.uri)         if (cache != null) {             it.attributes.put(isResponseFromCacheKey, true)             it.respond(cache)         }     }     onCallRespond { call, body ->         if (!call.attributes.contains(isResponseFromCacheKey)) {             provider.setCache(call.request.uri, body, pluginConfig.invalidateAt)         }     } }<\/code><\/pre>\n<h2>\u041f\u0435\u0440\u0432\u0430\u044f \u0440\u0430\u0431\u043e\u0447\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f <\/h2>\n<p>\u0420\u0435\u0448\u0435\u043d\u043e \u0431\u044b\u043b\u043e \u0441\u0440\u0430\u0437\u0443 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438 \u0434\u0432\u0430 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430: <a href=\"https:\/\/github.com\/Scogun\/ktor-simple-cache\/tree\/main\/ktor-simple-memory-cache\" rel=\"noopener noreferrer nofollow\"><strong>Memory<\/strong><\/a> \u0438 <a href=\"https:\/\/github.com\/Scogun\/ktor-simple-cache\/tree\/main\/ktor-simple-redis-cache\" rel=\"noopener noreferrer nofollow\"><strong>Redis<\/strong><\/a>.<\/p>\n<p><strong>Memory<\/strong> \u043d\u0435 \u0432\u044b\u0437\u0432\u0430\u043b \u043e\u0441\u043e\u0431\u044b\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0438 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0435\u043d\u0438\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u0442\u0430\u043a:<\/p>\n<details class=\"spoiler\">\n<summary>SimpleMemoryCacheProvider.kt<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"kotlin\">class SimpleMemoryCacheProvider(config: Config) : SimpleCacheProvider(config) {      private val cache = mutableMapOf&lt;String, SimpleMemoryCacheObject>()      override fun getCache(key: String): Any? {         val `object` = cache[key]         if (`object` == null || `object`.isExpired) {             return null         }          return `object`.content     }      override fun setCache(key: String, content: Any, invalidateAt: Duration?) {         cache[key] = SimpleMemoryCacheObject(content, invalidateAt ?: this.invalidateAt)     }      class Config internal constructor() : SimpleCacheProvider.Config() }  private data class SimpleMemoryCacheObject(val content: Any, val duration: Duration, val start: LocalDateTime = LocalDateTime.now()) {      val isExpired: Boolean         get() = LocalDateTime.now().isAfter(start.plusSeconds(duration.inWholeSeconds)) }  fun SimpleCacheConfig.memoryCache(     configure : SimpleMemoryCacheProvider.Config.() -> Unit ){     provider = SimpleMemoryCacheProvider(SimpleMemoryCacheProvider.Config().apply(configure)) }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0431\u044b\u043b \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d <strong>SimpleMemoryCacheObject<\/strong> \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b \u0445\u0440\u0430\u043d\u0438\u043b \u043a\u0430\u043a \u0434\u0430\u043d\u043d\u044b\u0435, \u0442\u0430\u043a \u0438 \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438 \u043a\u044d\u0448\u0430.<\/p>\n<p>\u041d\u0443 \u0438, \u0441\u0430\u043c \u043a\u044d\u0448 &#8212; \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e <strong>mutableMapOf&lt;String, SimpleMemoryCacheObject>()<\/strong> \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u043b\u0435\u0436\u0430\u0442.<\/p>\n<\/div>\n<\/details>\n<p>\u0421 <strong>Redis<\/strong> \u0436\u0435 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0432\u043e\u0437\u0438\u0442\u044c\u0441\u044f. \u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0438\u043c \u044f \u0432\u044b\u0431\u0440\u0430\u043b <a href=\"https:\/\/github.com\/redis\/jedis\" rel=\"noopener noreferrer nofollow\"><strong>Jedis<\/strong><\/a> \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443. \u0422\u0430\u043a \u043a\u0430\u043a \u043e\u0442\u0432\u0435\u0442 \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439, \u043d\u043e \u0438 \u043d\u0435\u043a\u0438\u0435 <strong>DTO<\/strong> \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0432\u043e\u043e\u0440\u0443\u0436\u0438\u0442\u044c\u0441\u044f <a href=\"https:\/\/github.com\/google\/gson\" rel=\"noopener noreferrer nofollow\"><strong>Gson<\/strong><\/a>&#8216;\u043e\u043c. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, <strong>SimpleRedisCacheObject<\/strong> \u0441\u0442\u0430\u043b \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"kotlin\">private class SimpleRedisCacheObject(val type: String, val content: String) {      override fun toString() = \"$type%#%$content\"      companion object {          fun fromObject(`object`: Any) = SimpleRedisCacheObject(`object`::class.java.name, Gson().toJson(`object`))          fun fromCache(cache: String): Any {             val data = cache.split(\"%#%\")             return Gson().fromJson(data.last(), Class.forName(data.first()))         }     } }<\/code><\/pre>\n<p>\u041d\u0443 \u0438, \u0441\u0430\u043c \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440:<\/p>\n<details class=\"spoiler\">\n<summary>SimpleRedisCacheProvider.kt<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"kotlin\">class SimpleRedisCacheProvider(private val config: Config) : SimpleCacheProvider(config) {      private val jedis: JedisPooled = JedisPooled(config.host, config.port, config.ssl)      override fun getCache(key: String): Any? = if (jedis.exists(key)) SimpleRedisCacheObject.fromCache(jedis.get(key)) else null      override fun setCache(key: String, content: Any, invalidateAt: Duration?) {         val expired = (invalidateAt ?: config.invalidateAt).inWholeMilliseconds         jedis.psetex(key, expired, SimpleRedisCacheObject.fromObject(content).toString())     }      class Config internal constructor(): SimpleCacheProvider.Config() {          var host = \"localhost\"          var port = 6379          var ssl = false     } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0432\u0441\u0435 \u044d\u0442\u043e \u044f \u043f\u043e\u043a\u0440\u044b\u043b \u0442\u0435\u0441\u0442\u0430\u043c\u0438 (\u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <a href=\"https:\/\/github.com\/redis-developer\/testcontainers-redis\" rel=\"noopener noreferrer nofollow\"><strong>Redis TestContainer<\/strong><\/a>) \u0438 \u043d\u0430\u0447\u0430\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0440\u0430\u0431\u043e\u0442\u0435.<\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e <strong>endpoint<\/strong> \u0441 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c \u0442\u0435\u043a\u0441\u0442\u043e\u043c \u0434\u043b\u044f \u0432\u0441\u0435\u0445, \u0442\u043e \u0438\u043c\u0435\u043d\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043b\u044e\u0447\u0430 \u0445\u0432\u0430\u0442\u0430\u043b\u0430 \u043d\u0430 \u0443\u0440\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u043e\u0446\u0435\u043d\u0438\u0432 \u043f\u0435\u0440\u0441\u043f\u0435\u043a\u0442\u0438\u0432\u044b \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438, \u0431\u044b\u043b\u043e \u0440\u0435\u0448\u0435\u043d\u043e \u043f\u043e\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u0435\u0439 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 <strong>endpoint<\/strong>&#8216;\u044b. \u0410 \u0442\u0430\u043c \u0443\u0436\u0435 \u043a\u043b\u044e\u0447 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u043b \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c <strong>query parameters<\/strong>. \u041f\u043e \u0438\u0442\u043e\u0433\u0443 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u043f\u043e\u043b\u043d\u0438\u043b\u0430\u0441\u044c \u043f\u043e\u043b\u0435\u043c <strong>queryKeys: List&lt;String><\/strong> (\u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0438\u0445 \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0441\u043e\u0431\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f), \u043d\u0443 \u0438 \u043a\u043b\u044e\u0447 \u0442\u0435\u043f\u0435\u0440\u044c \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f \u0442\u0430\u043a:<\/p>\n<pre><code class=\"kotlin\">private fun buildKey(request: ApplicationRequest, queryKeys: List&lt;String>): String {     val keys =         if (queryKeys.isEmpty()) request.queryParameters else request.queryParameters.filter { key, _ -> key in queryKeys }     val key = \"${request.path()}?${         keys.entries().sortedBy { it.key }             .joinToString(\"&amp;\") { \"${it.key}=${it.value.joinToString(\",\")}\" }     }\"     return key.trimEnd('?') }<\/code><\/pre>\n<p>\u041d\u0430\u0432\u0435\u0440\u043d\u043e, \u043d\u0430\u0434\u043e \u0431\u044b \u0438 <strong>value<\/strong> \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c (\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0442\u043e\u0436\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b), \u043d\u043e \u044f \u043f\u043e\u043a\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b-\u0441\u043f\u0438\u0441\u043a\u0438 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e.<\/p>\n<h2>\u041d\u043e\u0432\u044b\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b<\/h2>\n<p>\u0412 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043c\u043e\u043c\u0435\u043d\u0442, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044f \u0442\u0435\u043b\u0435\u043c\u0435\u0442\u0440\u0438\u044e \u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u044f \u0437\u0430\u043c\u0435\u0442\u0438\u043b, \u0447\u0442\u043e \u0438\u043d\u043e\u0433\u0434\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0411\u0414 \u0438\u0434\u0443\u0442 \u043d\u0435 \u0447\u0435\u0440\u0435\u0437 \u0432\u0440\u0435\u043c\u044f \u043f\u0440\u043e\u0442\u0443\u0445\u0430\u043d\u0438\u044f \u043a\u044d\u0448\u0430, \u0430 \u0441\u0440\u0430\u0437\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0434\u0440\u044f\u0434. \u0421\u0442\u0430\u043b\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u044f \u0437\u0430\u0431\u044b\u043b \u043e \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438. \u041f\u0440\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u0445, \u043f\u043e\u043a\u0430 \u043a\u044d\u0448 \u0435\u0449\u0435 \u043d\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d, \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043f\u043e\u0434\u0440\u044f\u0434 \u0434\u0435\u043b\u0430\u044e\u0442 \u044d\u0442\u043e. \u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e, \u043f\u043e\u043a\u0440\u044b\u043b \u044d\u0442\u043e \u0442\u0435\u0441\u0442\u043e\u043c:<\/p>\n<details class=\"spoiler\">\n<summary>Check cache is cocurrency<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"kotlin\">    @Test     fun `check cache is concurrency`() {         testApplication {             val jsonClient = client.config {                 install(ContentNegotiation) {                     json()                 }             }              application(Application::testApplication)              runBlocking {                 val totalThreads = 1000                 val deferred = (1..totalThreads).map {                     async {                         jsonClient.get(\"long\")                     }                 }                  val result = deferred.awaitAll().map { it.body&lt;TestResponse>() }.groupBy { it.id }                     .map { it.key to it.value.size }                 result.shouldBeSingleton {                     it.second.shouldBe(totalThreads)                 }             }         }     }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e <strong>1000<\/strong> \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u0416\u0434\u0435\u043c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u0432\u0441\u0435 \u043e\u043d\u0438 \u0438\u043c\u0435\u044e\u0442 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 (<strong>TestResponse<\/strong> \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0432 <strong>id<\/strong> \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435).<\/p>\n<\/div>\n<\/details>\n<h2>\u041d\u043e\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u044b\u0442\u0430\u043b\u0441\u044f \u043e\u0431\u044b\u0433\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u0442\u043e \u0447\u0435\u0440\u0435\u0437 <strong>synchronized<\/strong>. \u041d\u0435 \u043f\u043e\u043c\u043e\u0433\u043b\u043e. \u041f\u043e\u0442\u043e\u043c \u043c\u0443\u0447\u0438\u043b <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/concurrent\/locks\/ReentrantReadWriteLock.html\" rel=\"noopener noreferrer nofollow\">ReentrantReadWriteLock<\/a> &#8212; \u0442\u043e\u0436\u0435 \u0431\u0435\u0437\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043d\u043e.<\/p>\n<p>\u041f\u0440\u0438\u0448\u0435\u043b \u043a \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043d\u0430\u0434\u043e \u0431\u044b \u0434\u0435\u043b\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b <strong>suspend<\/strong> \u0438 \u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u0441 <strong>coroutines<\/strong>. \u0412\u043d\u0430\u0447\u0430\u043b\u0435 \u043f\u044b\u0442\u0430\u043b\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines.sync\/-mutex\/\" rel=\"noopener noreferrer nofollow\"><strong>Mutex<\/strong><\/a> \u0441 <a href=\"https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines.sync\/-mutex\/#74969293%2FFunctions%2F1975948010\" rel=\"noopener noreferrer nofollow\"><strong>withLock<\/strong><\/a>. \u0417\u0430\u0442\u0435\u043c \u043f\u0435\u0440\u0435\u0448\u0435\u043b \u043d\u0430 <a href=\"https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines.channels\/-channel\/\" rel=\"noopener noreferrer nofollow\"><strong>Channel<\/strong><\/a>. \u0412\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0432\u0441\u0435 \u043f\u0435\u0440\u0432\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u0438\u0441\u0430\u043b\u0438 \u0432 \u043a\u044d\u0448.<\/p>\n<p>\u0420\u0435\u0448\u0438\u043b \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u043a\u0430\u043a \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0442 \u0432\u044b\u0437\u043e\u0432\u044b, \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u043a\u0443\u0447\u0443 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u0442\u0435\u0441\u0442. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u044d\u0442\u043e\u043c\u0443, \u0441\u043c\u043e\u0433 \u043f\u0440\u043e\u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0432\u0435\u0441\u044c \u0446\u0438\u043a\u043b \u0437\u0430\u043f\u0440\u043e\u0441\u0430-\u043e\u0442\u0432\u0435\u0442\u0430 \u0438 \u043d\u0430\u0448\u0435\u043b \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443: \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0430 \u043a\u044d\u0448 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0441 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0430 \u0440\u0430\u0437\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u043a\u044d\u0448. \u0410 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043a\u043e\u0433\u0434\u0430-\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u043b <strong>MS Certificate<\/strong>, \u0433\u0434\u0435 \u043e\u0434\u043d\u0438\u043c \u0438\u0437 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u0431\u044b\u043b \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 <strong>singleton<\/strong> \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0432\u0441\u043f\u043b\u044b\u043b\u043e \u0441\u0430\u043c\u0438\u043c \u0441\u043e\u0431\u043e\u0439 (<strong>Mutex<\/strong> \u0442\u0430\u043a\u0438 \u043f\u043e\u0434\u043e\u0448\u0435\u043b \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e):<\/p>\n<pre><code class=\"kotlin\">    private val mutex = Mutex()      override suspend fun getCache(key: String): Any? {         var `object` = cache[key]         if (`object`?.isExpired != false) {             mutex.lock()             `object` = cache[key]             if (`object`?.isExpired != false) {                 return null             } else {                 mutex.unlock()                 return `object`.content             }         }          return `object`.content     }      override suspend fun setCache(key: String, content: Any, invalidateAt: Duration?) {         cache[key] = SimpleMemoryCacheObject(content, invalidateAt ?: this.invalidateAt)         mutex.unlock()     }<\/code><\/pre>\n<p>\u041b\u043e\u0433\u0438\u043a\u0430 \u043f\u0440\u043e\u0441\u0442\u0430:<\/p>\n<ul>\n<li>\n<p>\u041f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u044d\u0448 &#8212; \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435\u0442 &#8212; \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u043c \u043f\u043e\u0442\u043e\u043a\u0438 \u0438 \u0435\u0449\u0435 \u0440\u0430\u0437 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u043d\u0435\u0442 \u043b\u0438 \u043a\u044d\u0448\u0430. \u042d\u0442\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0443\u0436\u043d\u0430, \u0435\u0441\u043b\u0438 \u043c\u044b \u043f\u0440\u043e\u0441\u043a\u043e\u0447\u0438\u043b\u0438 \u043f\u0435\u0440\u0432\u0443\u044e, \u043d\u043e \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043f\u043e\u0442\u043e\u043a \u0443\u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u043a\u044d\u0448.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043a\u044d\u0448\u0430 \u0432\u0441\u0435 \u0435\u0449\u0435 \u043d\u0435\u0442 &#8212; \u0437\u043d\u0430\u0447\u0438\u0442 \u044d\u0442\u043e\u0442 \u043f\u043e\u0442\u043e\u043a \u043f\u0435\u0440\u0432\u044b\u0439. \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c <strong>null<\/strong>, \u0447\u0442\u043e\u0431\u044b <strong>plugin<\/strong> \u0434\u0435\u0440\u043d\u0443\u043b \u043c\u0435\u0442\u043e\u0434 \u0438 \u0437\u0430\u0442\u0435\u043c \u0432\u044b\u0437\u0432\u0430\u043b <strong>setCache<\/strong>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438 \u043e\u0442\u043f\u0443\u0441\u0442\u0438\u0442 <strong>lock<\/strong>&#8216;\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c &#8212; \u0440\u0430\u0437\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u043c \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<\/li>\n<\/ul>\n<h2>\u0427\u0442\u043e \u0434\u0430\u043b\u044c\u0448\u0435?<\/h2>\n<p>\u0412\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c, \u043d\u0430\u0432\u0435\u0440\u043d\u043e, \u0441\u043f\u0440\u043e\u0441\u0438\u0442: &#171;<em>\u0410 \u043f\u043e\u0447\u0435\u043c\u0443 \u043b\u044e\u0431\u044b\u0435 \u043e\u0442\u0432\u0435\u0442\u044b \u043a\u044d\u0448\u0438\u0440\u0443\u044e\u0442\u0441\u044f? \u0418 \u0434\u0430\u0436\u0435 \u043e\u0442 <\/em><strong><em>400<\/em><\/strong><em> \u0438 \u0432\u044b\u0448\u0435!<\/em>&#171;, &#8212; \u0438 \u043f\u043e\u043f\u0430\u0434\u0435\u0442 \u0432 \u0442\u043e\u0447\u043a\u0443. \u0421\u0435\u0439\u0447\u0430\u0441, \u0435\u0441\u043b\u0438 \u0432\u0434\u0440\u0443\u0433 \u0441\u0435\u0440\u0432\u0438\u0441 \u043f\u0435\u0440\u0432\u044b\u043c \u0432\u0435\u0440\u043d\u0443\u043b \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u043e\u0448\u0438\u0431\u043a\u0443, \u043e\u043d\u0430 \u0437\u0430\u043a\u044d\u0448\u0438\u0440\u0443\u0435\u0442\u0441\u044f. \u0420\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u043b\u0435\u0433\u043a\u043e:<\/p>\n<pre><code class=\"kotlin\">if (!call.attributes.contains(isResponseFromCacheKey) &amp;&amp; (call.response.status() ?: HttpStatusCode.OK) &lt; HttpStatusCode.BadRequest) {     provider.setCache(buildKey(call.request, pluginConfig.queryKeys), body, pluginConfig.invalidateAt) }<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u043d\u0435\u0437\u0430\u0434\u0430\u0447\u0430! \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0435 \u0441\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0432 \u043c\u043e\u0451\u043c \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438, \u043f\u043e\u0442\u043e\u043a\u0438 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0440\u0430\u0437\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0442\u0441\u044f. \u041f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"kotlin\">    onCallRespond { call, body ->         if ((call.response.status() ?: HttpStatusCode.OK) >= HttpStatusCode.BadRequest) {             provider.badResponse()         }         else if (!call.attributes.contains(isResponseFromCacheKey)) {             provider.setCache(buildKey(call.request, pluginConfig.queryKeys), body, pluginConfig.invalidateAt)         }     }<\/code><\/pre>\n<pre><code class=\"kotlin\">    override suspend fun badResponse() {         mutex.unlock()     }<\/code><\/pre>\n<p>\u041d\u043e \u043c\u043d\u0435 \u0442\u0430\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043d\u0440\u0430\u0432\u0438\u0442\u044c\u0441\u044f, \u0442\u0430\u043a \u0447\u0442\u043e \u0431\u0443\u0434\u0443 \u043f\u0440\u0438\u0437\u043d\u0430\u0442\u0435\u043b\u0435\u043d \u0437\u0430 \u043b\u044e\u0431\u044b\u0435 \u0441\u043e\u0432\u0435\u0442\u044b, \u043a\u0430\u043a \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c <strong>concurrency<\/strong>.<\/p>\n<h2>\u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0441\u044c \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u0442\u044c \u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c. \u041f\u043e\u043a\u0430 \u043d\u0435 \u0443\u0432\u0435\u0440\u0435\u043d, \u043a\u0430\u043a\u0438\u0435 \u0435\u0449\u0435 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u044b \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f, \u0442\u0430\u043a \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0443 \u043a\u043e\u0433\u043e-\u0442\u043e \u0435\u0441\u0442\u044c \u0438\u0434\u0435\u0438 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0441\u0447\u0435\u0442 &#8212; \u0431\u0443\u0434\u0443 \u0440\u0430\u0434. \u0422\u0430\u043a \u0436\u0435 \u0434\u0443\u043c\u0430\u044e \u043d\u0430\u0434 \u0442\u0435\u043c, \u0447\u0442\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044c \u0432 <strong>cacheOutput<\/strong> \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e <strong>get<\/strong> \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u0438 \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e. \u0412 \u0446\u0435\u043b\u043e\u043c, \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434 \u0438 \u0440\u0443\u0433\u043d\u0443\u0442\u044c\u0441\u044f <strong>warning<\/strong>&#8216;\u043e\u043c, \u0432\u0440\u043e\u0434\u0435 \u0434\u0435\u043b\u043e \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u043e\u0435.<\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e, \u0447\u0442\u043e \u0434\u043e\u0447\u0438\u0442\u0430\u043b\u0438!<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/780164\/\"> https:\/\/habr.com\/ru\/articles\/780164\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h2>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h2>\n<figure class=\"float full-width\"><\/figure>\n<p>\u042d\u0442\u043e \u0431\u044b\u043b \u0442\u0435\u043f\u043b\u044b\u0439 \u043e\u0441\u0435\u043d\u043d\u0438\u0439 \u0432\u0435\u0447\u0435\u0440, \u043a\u043e\u0433\u0434\u0430 \u043f\u0435\u0440\u0435\u0434\u043e \u043c\u043d\u043e\u0439 \u0432\u0441\u0442\u0430\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 &#171;\u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0435 \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u0441\u0435\u0440\u0432\u0438\u0441\u0435&#187;. \u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b \u0447\u0435\u0433\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u043e\u0449\u0435, \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0432 <a href=\"https:\/\/ktor.io\/\" rel=\"noopener noreferrer nofollow\"><strong>Ktor<\/strong><\/a> \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u043f\u0430\u0440\u043e\u0439 \u0441\u0442\u0440\u043e\u0447\u0435\u043a \u043a\u043e\u0434\u0430.  \u041e\u0434\u043d\u0430\u043a\u043e \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u043a\u0430\u0437\u0430\u043b\u0430\u0441\u044c \u043d\u0435 \u0442\u0430\u043a\u043e\u0439 \u0440\u0430\u0434\u0443\u0436\u043d\u043e\u0439: <strong>Ktor<\/strong> \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e <a href=\"https:\/\/ktor.io\/docs\/caching.html\" rel=\"noopener noreferrer nofollow\"><strong>Cache Headers plugin<\/strong><\/a><strong>,<\/strong> \u043a\u043e\u0442\u043e\u0440\u044b\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435. \u0427\u0442\u043e \u0436, \u0432\u043e\u043e\u0440\u0443\u0436\u0438\u0432\u0448\u0438\u0441\u044c <strong>IDE<\/strong>, \u044f \u043d\u0430\u0447\u0430\u043b \u0432\u0430\u044f\u0442\u044c \u0441\u0432\u043e\u0439 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434 &#8212; <a href=\"https:\/\/github.com\/Scogun\/ktor-simple-cache\" rel=\"noopener noreferrer nofollow\"><strong>Ktor Simple Cache<\/strong><\/a>.<\/p>\n<h2>\u041f\u0435\u0440\u0432\u044b\u0435 \u0448\u0430\u0433\u0438<\/h2>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u044f \u0437\u0430\u0434\u0430\u043b\u0441\u044f \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c: &#171;<em>\u0410 \u043a\u0430\u043a \u0432\u043e\u043e\u0431\u0449\u0435 \u0447\u0442\u043e-\u0442\u043e \u043f\u0440\u0438\u043a\u0440\u0443\u0442\u0438\u0442\u044c \u043a <\/em><strong><em>Ktor<\/em><\/strong><em>?<\/em>&#171;. \u041e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c, \u0447\u0442\u043e <strong>Ktor<\/strong> \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u043c \u0447\u0435\u0440\u0435\u0437 <strong>plugin&#8217;\u044b<\/strong>, \u043e \u0447\u0435\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432 <a href=\"https:\/\/ktor.io\/docs\/custom-plugins.html\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>.<\/p>\n<p>\u0414\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0438\u0441\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0447\u0430\u0441\u0442\u0438 <strong>plugin&#8217;\u0430<\/strong>:<\/p>\n<details class=\"spoiler\">\n<summary>\u041f\u0435\u0440\u0432\u044b\u0439 \u043a\u043e\u0434<\/summary>\n<div class=\"spoiler__content\">\n<p>\u041f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440 \u043a\u044d\u0448\u0430:<\/p>\n<pre><code class=\"kotlin\">abstract class SimpleCacheProvider(config: Config) {      val invalidateAt = config.invalidateAt      abstract fun getCache(key: String): Any?      abstract fun setCache(key: String, content: Any, invalidateAt: Duration?)      open class Config protected constructor() {           var invalidateAt: Duration = 5.minutes     } }<\/code><\/pre>\n<p>\u0422\u0435\u043b\u043e plugin&#8217;\u0430 \u0441 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0439:<\/p>\n<pre><code class=\"kotlin\">class SimpleCacheConfig {      internal var provider: SimpleCacheProvider? = null }  class SimpleCache(internal var config: SimpleCacheConfig) {      companion object : BaseApplicationPlugin&lt;Application, SimpleCacheConfig, SimpleCache> {         override val key: AttributeKey&lt;SimpleCache> = AttributeKey(\"SimpleCacheHolder\")          override fun install(pipeline: Application, configure: SimpleCacheConfig.() -> Unit): SimpleCache {             return SimpleCache(SimpleCacheConfig().apply { configure })         }     } }<\/code><\/pre>\n<p>\u041d\u0443 \u0438 \u0441\u0430\u043c plugin \u0441 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0435\u0439:<\/p>\n<pre><code class=\"kotlin\">class SimpleCachePluginConfig {      var invalidateAt: Duration? = null }  val SimpleCachePlugin = createRouteScopedPlugin(name = \"SimpleCachePlugin\", ::SimpleCachePluginConfig) {     val provider = application.plugin(SimpleCache).config.provider       onCall {         val cache = provider.getCache(it.request.uri)         if (cache != null) {             it.respond(cache)         }     }     onCallRespond { call, body ->         provider.setCache(call.request.uri, body, pluginConfig.invalidateAt)     } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0418\u0434\u0435\u044f \u0434\u043e\u043d\u0435\u043b\u044c\u0437\u044f \u043f\u0440\u043e\u0441\u0442\u0430\u044f:<\/p>\n<ul>\n<li>\n<p>\u041f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 &#8212; \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <strong>OnCall<\/strong><\/p>\n<\/li>\n<li>\n<p>\u041b\u0435\u0437\u0435\u043c \u0432 \u043a\u044d\u0448 \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u043c\u0435\u0442\u043e\u0434\u0430<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043a\u044d\u0448 \u043d\u0430\u0439\u0434\u0435\u043d &#8212; \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0435\u0442 &#8212; \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c <strong>route<\/strong> \u043c\u0435\u0442\u043e\u0434, \u0430 \u043e\u0442\u0432\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0432 \u043a\u044d\u0448 \u0432\u043d\u0443\u0442\u0440\u0438 <strong>onCallRespond<\/strong>.<\/p>\n<\/li>\n<\/ul>\n<p>\u042f \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u043b, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 <strong>OnCall<\/strong> \u0441\u0434\u0435\u043b\u0430\u0442\u044c <strong>it.respond(cache)<\/strong> &#8212; <strong>onCallRespond<\/strong> \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0435 \u0431\u0443\u0434\u0435\u0442. \u041e\u0434\u043d\u0430\u043a\u043e \u043f\u0435\u0440\u0432\u044b\u0439 \u0436\u0435 unit test \u043f\u043e\u043a\u0430\u0437\u0430\u043b, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a. \u0418\u0442\u0430\u043a, \u043c\u043d\u0435 \u043d\u0430\u0434\u043e \u0431\u044b\u043b\u043e \u043a\u0430\u043a-\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 <strong>OnCallRespond<\/strong> \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u0437\u044f\u0442\u044b \u0438\u0437 \u043a\u044d\u0448\u0430 \u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438\u0445 \u043d\u0435 \u043d\u0430\u0434\u043e. \u0411\u043b\u0430\u0433\u043e, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 Ktor \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 <a href=\"https:\/\/ktor.io\/docs\/custom-plugins.html#call-state\" rel=\"noopener noreferrer nofollow\"><strong>Call State<\/strong><\/a>.<\/p>\n<p>\u0418\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e <strong>plugin<\/strong> \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u0442\u0430\u043a:<\/p>\n<pre><code class=\"kotlin\">val SimpleCachePlugin = createRouteScopedPlugin(name = \"SimpleCachePlugin\", ::SimpleCachePluginConfig) {     val provider = application.plugin(SimpleCache).config.provider     val isResponseFromCacheKey = AttributeKey&lt;Boolean>(\"isResponseFromCacheKey\")     onCall {         val cache = provider.getCache(it.request.uri)         if (cache != null) {             it.attributes.put(isResponseFromCacheKey, true)             it.respond(cache)         }     }     onCallRespond { call, body ->         if (!call.attributes.contains(isResponseFromCacheKey)) {             provider.setCache(call.request.uri, body, pluginConfig.invalidateAt)         }     } }<\/code><\/pre>\n<h2>\u041f\u0435\u0440\u0432\u0430\u044f \u0440\u0430\u0431\u043e\u0447\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f <\/h2>\n<p>\u0420\u0435\u0448\u0435\u043d\u043e \u0431\u044b\u043b\u043e \u0441\u0440\u0430\u0437\u0443 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438 \u0434\u0432\u0430 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430: <a href=\"https:\/\/github.com\/Scogun\/ktor-simple-cache\/tree\/main\/ktor-simple-memory-cache\" rel=\"noopener noreferrer nofollow\"><strong>Memory<\/strong><\/a> \u0438 <a href=\"https:\/\/github.com\/Scogun\/ktor-simple-cache\/tree\/main\/ktor-simple-redis-cache\" rel=\"noopener noreferrer nofollow\"><strong>Redis<\/strong><\/a>.<\/p>\n<p><strong>Memory<\/strong> \u043d\u0435 \u0432\u044b\u0437\u0432\u0430\u043b \u043e\u0441\u043e\u0431\u044b\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0438 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0435\u043d\u0438\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u0442\u0430\u043a:<\/p>\n<details class=\"spoiler\">\n<summary>SimpleMemoryCacheProvider.kt<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"kotlin\">class SimpleMemoryCacheProvider(config: Config) : SimpleCacheProvider(config) {      private val cache = mutableMapOf&lt;String, SimpleMemoryCacheObject>()      override fun getCache(key: String): Any? {         val `object` = cache[key]         if (`object` == null || `object`.isExpired) {             return null         }          return `object`.content     }      override fun setCache(key: String, content: Any, invalidateAt: Duration?) {         cache[key] = SimpleMemoryCacheObject(content, invalidateAt ?: this.invalidateAt)     }      class Config internal constructor() : SimpleCacheProvider.Config() }  private data class SimpleMemoryCacheObject(val content: Any, val duration: Duration, val start: LocalDateTime = LocalDateTime.now()) {      val isExpired: Boolean         get() = LocalDateTime.now().isAfter(start.plusSeconds(duration.inWholeSeconds)) }  fun SimpleCacheConfig.memoryCache(     configure : SimpleMemoryCacheProvider.Config.() -> Unit ){     provider = SimpleMemoryCacheProvider(SimpleMemoryCacheProvider.Config().apply(configure)) }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0431\u044b\u043b \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d <strong>SimpleMemoryCacheObject<\/strong> \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b \u0445\u0440\u0430\u043d\u0438\u043b \u043a\u0430\u043a \u0434\u0430\u043d\u043d\u044b\u0435, \u0442\u0430\u043a \u0438 \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438 \u043a\u044d\u0448\u0430.<\/p>\n<p>\u041d\u0443 \u0438, \u0441\u0430\u043c \u043a\u044d\u0448 &#8212; \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e <strong>mutableMapOf&lt;String, SimpleMemoryCacheObject>()<\/strong> \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u043b\u0435\u0436\u0430\u0442.<\/p>\n<\/div>\n<\/details>\n<p>\u0421 <strong>Redis<\/strong> \u0436\u0435 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0432\u043e\u0437\u0438\u0442\u044c\u0441\u044f. \u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0438\u043c \u044f \u0432\u044b\u0431\u0440\u0430\u043b <a href=\"https:\/\/github.com\/redis\/jedis\" rel=\"noopener noreferrer nofollow\"><strong>Jedis<\/strong><\/a> \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443. \u0422\u0430\u043a \u043a\u0430\u043a \u043e\u0442\u0432\u0435\u0442 \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439, \u043d\u043e \u0438 \u043d\u0435\u043a\u0438\u0435 <strong>DTO<\/strong> \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0432\u043e\u043e\u0440\u0443\u0436\u0438\u0442\u044c\u0441\u044f <a href=\"https:\/\/github.com\/google\/gson\" rel=\"noopener noreferrer nofollow\"><strong>Gson<\/strong><\/a>&#8216;\u043e\u043c. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, <strong>SimpleRedisCacheObject<\/strong> \u0441\u0442\u0430\u043b \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"kotlin\">private class SimpleRedisCacheObject(val type: String, val content: String) {      override fun toString() = \"$type%#%$content\"      companion object {          fun fromObject(`object`: Any) = SimpleRedisCacheObject(`object`::class.java.name, Gson().toJson(`object`))          fun fromCache(cache: String): Any {             val data = cache.split(\"%#%\")             return Gson().fromJson(data.last(), Class.forName(data.first()))         }     } }<\/code><\/pre>\n<p>\u041d\u0443 \u0438, \u0441\u0430\u043c \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440:<\/p>\n<details class=\"spoiler\">\n<summary>SimpleRedisCacheProvider.kt<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"kotlin\">class SimpleRedisCacheProvider(private val config: Config) : SimpleCacheProvider(config) {      private val jedis: JedisPooled = JedisPooled(config.host, config.port, config.ssl)      override fun getCache(key: String): Any? = if (jedis.exists(key)) SimpleRedisCacheObject.fromCache(jedis.get(key)) else null      override fun setCache(key: String, content: Any, invalidateAt: Duration?) {         val expired = (invalidateAt ?: config.invalidateAt).inWholeMilliseconds         jedis.psetex(key, expired, SimpleRedisCacheObject.fromObject(content).toString())     }      class Config internal constructor(): SimpleCacheProvider.Config() {          var host = \"localhost\"          var port = 6379          var ssl = false     } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0432\u0441\u0435 \u044d\u0442\u043e \u044f \u043f\u043e\u043a\u0440\u044b\u043b \u0442\u0435\u0441\u0442\u0430\u043c\u0438 (\u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <a href=\"https:\/\/github.com\/redis-developer\/testcontainers-redis\" rel=\"noopener noreferrer nofollow\"><strong>Redis TestContainer<\/strong><\/a>) \u0438 \u043d\u0430\u0447\u0430\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0440\u0430\u0431\u043e\u0442\u0435.<\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e <strong>endpoint<\/strong> \u0441 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c \u0442\u0435\u043a\u0441\u0442\u043e\u043c \u0434\u043b\u044f \u0432\u0441\u0435\u0445, \u0442\u043e \u0438\u043c\u0435\u043d\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043b\u044e\u0447\u0430 \u0445\u0432\u0430\u0442\u0430\u043b\u0430 \u043d\u0430 \u0443\u0440\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u043e\u0446\u0435\u043d\u0438\u0432 \u043f\u0435\u0440\u0441\u043f\u0435\u043a\u0442\u0438\u0432\u044b \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438, \u0431\u044b\u043b\u043e \u0440\u0435\u0448\u0435\u043d\u043e \u043f\u043e\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u0435\u0439 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 <strong>endpoint<\/strong>&#8216;\u044b. \u0410 \u0442\u0430\u043c \u0443\u0436\u0435 \u043a\u043b\u044e\u0447 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u043b \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c <strong>query parameters<\/strong>. \u041f\u043e \u0438\u0442\u043e\u0433\u0443 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u043f\u043e\u043b\u043d\u0438\u043b\u0430\u0441\u044c \u043f\u043e\u043b\u0435\u043c <strong>queryKeys: List&lt;String><\/strong> (\u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0438\u0445 \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0441\u043e\u0431\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f), \u043d\u0443 \u0438 \u043a\u043b\u044e\u0447 \u0442\u0435\u043f\u0435\u0440\u044c \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f \u0442\u0430\u043a:<\/p>\n<pre><code class=\"kotlin\">private fun buildKey(request: ApplicationRequest, queryKeys: List&lt;String>): String {     val keys =         if (queryKeys.isEmpty()) request.queryParameters else request.queryParameters.filter { key, _ -> key in queryKeys }     val key = \"${request.path()}?${         keys.entries().sortedBy { it.key }             .joinToString(\"&amp;\") { \"${it.key}=${it.value.joinToString(\",\")}\" }     }\"     return key.trimEnd('?') }<\/code><\/pre>\n<p>\u041d\u0430\u0432\u0435\u0440\u043d\u043e, \u043d\u0430\u0434\u043e \u0431\u044b \u0438 <strong>value<\/strong> \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c (\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0442\u043e\u0436\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b), \u043d\u043e \u044f \u043f\u043e\u043a\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b-\u0441\u043f\u0438\u0441\u043a\u0438 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e.<\/p>\n<h2>\u041d\u043e\u0432\u044b\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b<\/h2>\n<p>\u0412 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043c\u043e\u043c\u0435\u043d\u0442, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044f \u0442\u0435\u043b\u0435\u043c\u0435\u0442\u0440\u0438\u044e \u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u044f \u0437\u0430\u043c\u0435\u0442\u0438\u043b, \u0447\u0442\u043e \u0438\u043d\u043e\u0433\u0434\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0411\u0414 \u0438\u0434\u0443\u0442 \u043d\u0435 \u0447\u0435\u0440\u0435\u0437 \u0432\u0440\u0435\u043c\u044f \u043f\u0440\u043e\u0442\u0443\u0445\u0430\u043d\u0438\u044f \u043a\u044d\u0448\u0430, \u0430 \u0441\u0440\u0430\u0437\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0434\u0440\u044f\u0434. \u0421\u0442\u0430\u043b\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u044f \u0437\u0430\u0431\u044b\u043b \u043e \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438. \u041f\u0440\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u0445, \u043f\u043e\u043a\u0430 \u043a\u044d\u0448 \u0435\u0449\u0435 \u043d\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d, \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043f\u043e\u0434\u0440\u044f\u0434 \u0434\u0435\u043b\u0430\u044e\u0442 \u044d\u0442\u043e. \u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e, \u043f\u043e\u043a\u0440\u044b\u043b \u044d\u0442\u043e \u0442\u0435\u0441\u0442\u043e\u043c:<\/p>\n<details class=\"spoiler\">\n<summary>Check cache is cocurrency<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"kotlin\">    @Test     fun `check cache is concurrency`() {         testApplication {             val jsonClient = client.config {                 install(ContentNegotiation) {                     json()                 }             }              application(Application::testApplication)              runBlocking {                 val totalThreads = 1000                 val deferred = (1..totalThreads).map {                     async {                         jsonClient.get(\"long\")                     }                 }                  val result = deferred.awaitAll().map { it.body&lt;TestResponse>() }.groupBy { it.id }                     .map { it.key to it.value.size }                 result.shouldBeSingleton {                     it.second.shouldBe(totalThreads)                 }             }         }     }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e <strong>1000<\/strong> \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u0416\u0434\u0435\u043c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u0432\u0441\u0435 \u043e\u043d\u0438 \u0438\u043c\u0435\u044e\u0442 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 (<strong>TestResponse<\/strong> \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0432 <strong>id<\/strong> \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435).<\/p>\n<\/div>\n<\/details>\n<h2>\u041d\u043e\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u044b\u0442\u0430\u043b\u0441\u044f \u043e\u0431\u044b\u0433\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u0442\u043e \u0447\u0435\u0440\u0435\u0437 <strong>synchronized<\/strong>. \u041d\u0435 \u043f\u043e\u043c\u043e\u0433\u043b\u043e. \u041f\u043e\u0442\u043e\u043c \u043c\u0443\u0447\u0438\u043b <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/concurrent\/locks\/ReentrantReadWriteLock.html\" rel=\"noopener noreferrer nofollow\">ReentrantReadWriteLock<\/a> &#8212; \u0442\u043e\u0436\u0435 \u0431\u0435\u0437\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043d\u043e.<\/p>\n<p>\u041f\u0440\u0438\u0448\u0435\u043b \u043a \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043d\u0430\u0434\u043e \u0431\u044b \u0434\u0435\u043b\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b <strong>suspend<\/strong> \u0438 \u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u0441 <strong>coroutines<\/strong>. \u0412\u043d\u0430\u0447\u0430\u043b\u0435 \u043f\u044b\u0442\u0430\u043b\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines.sync\/-mutex\/\" rel=\"noopener noreferrer nofollow\"><strong>Mutex<\/strong><\/a> \u0441 <a href=\"https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines.sync\/-mutex\/#74969293%2FFunctions%2F1975948010\" rel=\"noopener noreferrer nofollow\"><strong>withLock<\/strong><\/a>. \u0417\u0430\u0442\u0435\u043c \u043f\u0435\u0440\u0435\u0448\u0435\u043b \u043d\u0430 <a href=\"https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines.channels\/-channel\/\" rel=\"noopener noreferrer nofollow\"><strong>Channel<\/strong><\/a>. \u0412\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0432\u0441\u0435 \u043f\u0435\u0440\u0432\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u0438\u0441\u0430\u043b\u0438 \u0432 \u043a\u044d\u0448.<\/p>\n<p>\u0420\u0435\u0448\u0438\u043b \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u043a\u0430\u043a \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0442 \u0432\u044b\u0437\u043e\u0432\u044b, \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u043a\u0443\u0447\u0443 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u0442\u0435\u0441\u0442. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u044d\u0442\u043e\u043c\u0443, \u0441\u043c\u043e\u0433 \u043f\u0440\u043e\u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0432\u0435\u0441\u044c \u0446\u0438\u043a\u043b \u0437\u0430\u043f\u0440\u043e\u0441\u0430-\u043e\u0442\u0432\u0435\u0442\u0430 \u0438 \u043d\u0430\u0448\u0435\u043b \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443: \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0430 \u043a\u044d\u0448 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0441 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0430 \u0440\u0430\u0437\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u043a\u044d\u0448. \u0410 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043a\u043e\u0433\u0434\u0430-\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u043b <strong>MS Certificate<\/strong>, \u0433\u0434\u0435 \u043e\u0434\u043d\u0438\u043c \u0438\u0437 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u0431\u044b\u043b \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 <strong>singleton<\/strong> \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0432\u0441\u043f\u043b\u044b\u043b\u043e \u0441\u0430\u043c\u0438\u043c \u0441\u043e\u0431\u043e\u0439 (<strong>Mutex<\/strong> \u0442\u0430\u043a\u0438 \u043f\u043e\u0434\u043e\u0448\u0435\u043b \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e):<\/p>\n<pre><code class=\"kotlin\">    private val mutex = Mutex()      override suspend fun getCache(key: String): Any? {         var `object` = cache[key]         if (`object`?.isExpired != false) {             mutex.lock()             `object` = cache[key]             if (`object`?.isExpired != false) {                 return null             } else {                 mutex.unlock()                 return `object`.content             }         }          return `object`.content     }      override suspend fun setCache(key: String, content: Any, invalidateAt: Duration?) {<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-362753","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/362753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=362753"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/362753\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=362753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=362753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=362753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}