Skip to content

Commit

Permalink
doing some linter
Browse files Browse the repository at this point in the history
  • Loading branch information
frankois944 committed Jan 16, 2025
1 parent ca574b0 commit a70cbe6
Show file tree
Hide file tree
Showing 13 changed files with 464 additions and 398 deletions.
9 changes: 5 additions & 4 deletions KtorKMPFileCaching/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ plugins {
alias(libs.plugins.publish)
}


val kotlinJsTargetAttribute = Attribute.of("kotlinJsTarget", String::class.java)

kotlin {
Expand Down Expand Up @@ -126,13 +125,15 @@ mavenPublishing {
coordinates(
groupId = "io.github.frankois944",
artifactId = "ktorfilecaching",
version = "0.4.3"
version = "0.4.3",
)

// Configure POM metadata for the published artifact
pom {
name.set("KMP Library for Ktor client file caching")
description.set("This library can be used by a lot of targets for enabling the file caching of Ktor caching (https://ktor.io/docs/client-caching.html) ")
description.set(
"This library can be used by a lot of targets for enabling the file caching of Ktor caching (https://ktor.io/docs/client-caching.html) ",
)
inceptionYear.set("2024")
url.set("https://github.com/frankois944/KtorKMPFileCaching")

Expand Down Expand Up @@ -163,4 +164,4 @@ mavenPublishing {

// Enable GPG signing for all publications
signAllPublications()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import io.ktor.http.Url
import io.ktor.util.logging.KtorSimpleLogger
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import okio.FileSystem
import okio.Path
Expand All @@ -27,15 +26,14 @@ internal expect fun filesystem(): FileSystem
public class KtorFileCaching(
storedCacheDirectory: Path = "KTorFileCaching".toPath(),
rootStoredCachePath: Path = FileSystem.SYSTEM_TEMPORARY_DIRECTORY,
private val fileSystem: FileSystem = filesystem()
private val fileSystem: FileSystem = filesystem(),
) : CacheStorage {

//<editor-fold desc="Path">
// <editor-fold desc="Path">

private val cacheDir = "$rootStoredCachePath${Path.DIRECTORY_SEPARATOR}$storedCacheDirectory".toPath()
private val cacheSystem = CacheSystem(fileSystem, cacheDir)

//</editor-fold>
// </editor-fold>

private val lock = Mutex() // Coroutine-friendly lock for thread safety
private val metadataCache =
Expand Down Expand Up @@ -67,62 +65,72 @@ public class KtorFileCaching(
override suspend fun find(
url: Url,
varyKeys: Map<String, String>,
): CachedResponseData? = lock.withLock {
LOGGER.debug("""
FIND:
url = $url
varyKeys = $varyKeys
""".trimIndent())
val urlCacheDir = cacheDir.resolve(urlToPath(url))
val varyKeyHash = hashVaryKeys(varyKeys)
cacheSystem.read(urlCacheDir, varyKeyHash)?.let {
Json.decodeFromString<SerializableCachedResponseData>(it)
}?.cachedResponseData
}

override suspend fun findAll(url: Url): Set<CachedResponseData> = lock.withLock {
val urlToPath = urlToPath(url)
val urlCacheDir = cacheDir.resolve(urlToPath)
if (!cacheSystem.exist(urlCacheDir, null)) return emptySet()

LOGGER.debug("""
FINDALL:
url = $url
""".trimIndent())
): CachedResponseData? =
lock.withLock {
LOGGER.debug(
"""
FIND:
url = $url
varyKeys = $varyKeys
""".trimIndent(),
)
val urlCacheDir = cacheDir.resolve(urlToPath(url))
val varyKeyHash = hashVaryKeys(varyKeys)
cacheSystem
.read(urlCacheDir, varyKeyHash)
?.let {
Json.decodeFromString<SerializableCachedResponseData>(it)
}?.cachedResponseData
}

metadataCache[urlToPath]?.addAll(cacheSystem.contentOf(urlCacheDir).map { it.name })
metadataCache[urlToPath]?.mapNotNull { varyKeyHash ->
cacheSystem.read(urlCacheDir, varyKeyHash)?.let {
Json.decodeFromString<SerializableCachedResponseData>(it)
}?.cachedResponseData
}?.toSet() ?: emptySet()
}
override suspend fun findAll(url: Url): Set<CachedResponseData> =
lock.withLock {
val urlToPath = urlToPath(url)
val urlCacheDir = cacheDir.resolve(urlToPath)
if (!cacheSystem.exist(urlCacheDir, null)) return emptySet()

LOGGER.debug(
"""
FINDALL:
url = $url
""".trimIndent(),
)

metadataCache[urlToPath]?.addAll(cacheSystem.contentOf(urlCacheDir).map { it.name })
metadataCache[urlToPath]
?.mapNotNull { varyKeyHash ->
cacheSystem
.read(urlCacheDir, varyKeyHash)
?.let {
Json.decodeFromString<SerializableCachedResponseData>(it)
}?.cachedResponseData
}?.toSet() ?: emptySet()
}

override suspend fun store(
url: Url,
data: CachedResponseData,
): Unit = lock.withLock {
val urlToPath = urlToPath(url)
val urlCacheDir = cacheDir.resolve(urlToPath)
val varyKeyHash = hashVaryKeys(data.varyKeys)
LOGGER.debug("""
STORE:
url = $url
varyKeys = ${data.varyKeys}
""".trimIndent())
cacheSystem.write(urlCacheDir, varyKeyHash, Json.encodeToString(SerializableCachedResponseData(data)))
metadataCache[urlToPath]?.run {
add(varyKeyHash)
} ?: run {
metadataCache[urlToPath] = mutableSetOf(varyKeyHash)
): Unit =
lock.withLock {
val urlToPath = urlToPath(url)
val urlCacheDir = cacheDir.resolve(urlToPath)
val varyKeyHash = hashVaryKeys(data.varyKeys)
LOGGER.debug(
"""
STORE:
url = $url
varyKeys = ${data.varyKeys}
""".trimIndent(),
)
cacheSystem.write(urlCacheDir, varyKeyHash, Json.encodeToString(SerializableCachedResponseData(data)))
metadataCache[urlToPath]?.run {
add(varyKeyHash)
} ?: run {
metadataCache[urlToPath] = mutableSetOf(varyKeyHash)
}
}
}

private fun urlToPath(url: Url): String {
return url.hashCode().toString()
}
private fun urlToPath(url: Url): String = url.hashCode().toString()

private fun hashVaryKeys(varyKeys: Map<String, String>): String {
return varyKeys.hashCode().toString()
}
private fun hashVaryKeys(varyKeys: Map<String, String>): String = varyKeys.hashCode().toString()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ class ApiClient(
val httpClient: HttpClient,
) {
suspend fun getIp() = httpClient.get("https://api.ipify.org/?format=json")

suspend fun getIpWithParam(param: String) = httpClient.get("https://api.ipify.org/?format=json&param=$param")
suspend fun getIpWithParamAndBody(param: String, body: String) = httpClient.post("https://api.ipify.org/?format=json&param=$param") {
setBody(body)
}
}
Loading

0 comments on commit a70cbe6

Please sign in to comment.