Skip to content

Commit

Permalink
Feature/update (#23)
Browse files Browse the repository at this point in the history
* Update libraries

* Update Plugins

* Update Kotlin

* Refactoring

* Increase version

* Update READMEs
  • Loading branch information
Scogun authored Dec 14, 2024
1 parent 9539af0 commit f7a51d8
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 32 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ allprojects {

group = "com.ucasoft.ktor"

version = "0.50.7"
version = "0.51.2"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Dependensies.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import org.gradle.api.Project

const val ktorVersion = "3.0.1"
const val ktorVersion = "3.0.2"
const val kotestVersion = "5.9.1"

fun Project.ktor(module: String) = "io.ktor:ktor-$module:$ktorVersion"
Expand Down
4 changes: 2 additions & 2 deletions ktor-simple-cache/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Ktor Simple Cache
Base solution which provides the plugin implementation and abstract class for cache providers.

[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-cache/0.50.7?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-cache/0.50.7/jar)
[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-cache/0.51.2?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-cache/0.51.2/jar)
## Setup
### Gradle
```kotlin
repositories {
mavenCentral()
}

implementation("com.ucasoft.ktor:ktor-simple-cache:0.50.7")
implementation("com.ucasoft.ktor:ktor-simple-cache:0.51.2")
```
## Usage
```kotlin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ abstract class SimpleCacheProvider(config: Config) {

val invalidateAt = config.invalidateAt

fun badResponse() {
private val mutex = Mutex()

abstract suspend fun getCache(key: String): Any?
abstract suspend fun setCache(key: String, content: Any, invalidateAt: Duration?)

fun handleBadResponse() {
mutex.unlock()
}

private val mutex = Mutex()

suspend fun loadCache(key: String): Any? {
var cache = getCache(key)
Expand All @@ -53,10 +56,6 @@ abstract class SimpleCacheProvider(config: Config) {
mutex.unlock()
}

abstract suspend fun getCache(key: String): Any?

abstract suspend fun setCache(key: String, content: Any, invalidateAt: Duration?)

open class Config protected constructor() {

var invalidateAt: Duration = 5.minutes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ val SimpleCachePlugin = createRouteScopedPlugin(name = "SimpleCachePlugin", ::Si
}
}
on(CallFailed) { _, e ->
provider.badResponse()
provider.handleBadResponse()
throw e
}
onCallRespond { call, body ->
if ((call.response.status() ?: HttpStatusCode.OK) >= HttpStatusCode.BadRequest) {
provider.badResponse()
provider.handleBadResponse()
}
else if (!call.attributes.contains(isResponseFromCacheKey)) {
provider.saveCache(buildKey(call.request, pluginConfig.queryKeys), body, pluginConfig.invalidateAt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal class SimpleCacheTests {
application(Application::badTestApplication)

val exception = shouldThrow<IllegalStateException> {
client.get("/check")
client.get(CHECK_ENDPOINT)
}

exception.message.shouldBe("Add one cache provider!")
Expand All @@ -47,18 +47,20 @@ internal class SimpleCacheTests {
@Test
fun `check cache call no set`() {
testApplication {
val provider = buildProvider(mutableMapOf("/check" to TextContent("Test", ContentType.Any)))
val provider = buildProvider(mutableMapOf(CHECK_ENDPOINT to TextContent("Test", ContentType.Any)))

install(SimpleCache) {
testCache(provider)
}

application(Application::testApplication)
application {
testApplication()
}

val response = client.get("/check")
val response = client.get(CHECK_ENDPOINT)
response.readRawBytes().toString(Charsets.UTF_8).shouldBe("Test")

verify(provider, times(1)).getCache(eq("/check"))
verify(provider, times(1)).getCache(eq(CHECK_ENDPOINT))
verify(provider, never()).setCache(anyString(), any(), anyOrNull())
}
}
Expand All @@ -79,7 +81,7 @@ internal class SimpleCacheTests {
val response = client.get("/bad")
response.shouldHaveStatus(HttpStatusCode.BadRequest)
verify(provider, times(2 * i)).getCache(eq("/bad"))
verify(provider, times(1 * i)).badResponse()
verify(provider, times(1 * i)).handleBadResponse()
verify(provider, times(0)).setCache(eq("/bad"), any(), anyOrNull())
cache.shouldBeEmpty()
}
Expand All @@ -99,10 +101,10 @@ internal class SimpleCacheTests {
application(Application::testApplication)

for (i in 0..1) {
val response = client.get("/check")
val response = client.get(CHECK_ENDPOINT)
response.readRawBytes().toString(Charsets.UTF_8).toIntOrNull().shouldNotBeNull()
verify(provider, times(2 + i)).getCache(eq("/check"))
verify(provider, times(1)).setCache(eq("/check"), any(), anyOrNull())
verify(provider, times(2 + i)).getCache(eq(CHECK_ENDPOINT))
verify(provider, times(1)).setCache(eq(CHECK_ENDPOINT), any(), anyOrNull())
cache.shouldNotBeEmpty()
}
}
Expand All @@ -126,7 +128,7 @@ internal class SimpleCacheTests {
delay(500)
}
async {
client.get("/check")
client.get(CHECK_ENDPOINT)
}
}

Expand All @@ -136,8 +138,8 @@ internal class SimpleCacheTests {
it.second.shouldBe(totalThreads)
}

verify(provider, atMost(1100)).getCache(eq("/check"))
verify(provider, times(1)).setCache(eq("/check"), any(), anyOrNull())
verify(provider, atMost(1100)).getCache(eq(CHECK_ENDPOINT))
verify(provider, times(1)).setCache(eq(CHECK_ENDPOINT), any(), anyOrNull())
}
}
}
Expand Down Expand Up @@ -311,4 +313,8 @@ internal class SimpleCacheTests {

return provider
}

companion object {
private const val CHECK_ENDPOINT = "/check"
}
}
4 changes: 2 additions & 2 deletions ktor-simple-memory-cache/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Ktor Simple Memory Cache
Memory cache provider for Ktor Simple Cache plugin

[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-memory-cache/0.50.7?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-memory-cache/0.50.7/jar)
[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-memory-cache/0.51.2?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-memory-cache/0.51.2/jar)
## Setup
### Gradle
```kotlin
repositories {
mavenCentral()
}

implementation("com.ucasoft.ktor:ktor-simple-memory-cache:0.50.7")
implementation("com.ucasoft.ktor:ktor-simple-memory-cache:0.51.2")
```
## Usage
```kotlin
Expand Down
4 changes: 2 additions & 2 deletions ktor-simple-redis-cache/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Ktor Simple Redis Cache
Redis cache provider for Ktor Simple Cache plugin

[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-redis-cache/0.50.7?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-redis-cache/0.50.7/jar)
[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-redis-cache/0.51.2?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-redis-cache/0.51.2/jar)
## Setup
### Gradle
```kotlin
repositories {
mavenCentral()
}

implementation("com.ucasoft.ktor:ktor-simple-redis-cache:0.50.7")
implementation("com.ucasoft.ktor:ktor-simple-redis-cache:0.51.2")
```
## Usage
```kotlin
Expand Down
6 changes: 3 additions & 3 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
pluginManagement {
resolutionStrategy {
plugins {
val kotlinVersion = "2.0.21"
val kotlinVersion = "2.1.0"
kotlin("multiplatform") version kotlinVersion apply false
kotlin("plugin.serialization") version kotlinVersion apply false
id("org.jetbrains.kotlinx.kover") version "0.8.3" apply false
id("org.jetbrains.kotlinx.kover") version "0.9.0" apply false
}
}
}

plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
id("org.gradle.toolchains.foojay-resolver-convention") version "0.9.0"
}

rootProject.name = "simple-cache"
Expand Down

0 comments on commit f7a51d8

Please sign in to comment.