Skip to content

Commit

Permalink
Use Caffeine for cache
Browse files Browse the repository at this point in the history
Fixes #135 again
  • Loading branch information
WarningImHack3r committed Sep 28, 2024
1 parent 04e7ad3 commit e73a7a8
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 38 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ repositories {

// Dependencies are managed with Gradle version catalog - read more: https://docs.gradle.org/current/userguide/platforms.html#sub:version-catalog
dependencies {
implementation(libs.caffeine)
implementation(libs.datetime)
implementation(libs.semver4j)
implementation(libs.serialization)
Expand Down
6 changes: 4 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
[versions]
# libraries
caffeine = "3.1.8"
datetime = "0.6.1"
junit = "4.13.2"
serialization = "1.7.3"
semver4j = "5.4.0"

# plugins
changelog = "2.2.1"
datetime = "0.6.1"
intellijPlatform = "2.1.0"
kotlin = "2.0.20"
kover = "0.8.3"
qodana = "2024.2.3"
serialization = "1.7.3"

[libraries]
caffeine = { group = "com.github.ben-manes.caffeine", name = "caffeine", version.ref = "caffeine" }
datetime = { group = "org.jetbrains.kotlinx", name = "kotlinx-datetime", version.ref = "datetime" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
semver4j = { group = "org.semver4j", name = "semver4j", version.ref = "semver4j" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package com.github.warningimhack3r.npmupdatedependencies.backend.engine

import com.github.benmanes.caffeine.cache.Caffeine
import com.github.warningimhack3r.npmupdatedependencies.backend.extensions.asBoolean
import com.github.warningimhack3r.npmupdatedependencies.backend.extensions.asJsonArray
import com.github.warningimhack3r.npmupdatedependencies.backend.extensions.asJsonObject
import com.github.warningimhack3r.npmupdatedependencies.backend.extensions.asString
import com.github.warningimhack3r.npmupdatedependencies.backend.extensions.isBlankOrEmpty
import com.github.warningimhack3r.npmupdatedependencies.backend.models.CacheEntry
import com.github.warningimhack3r.npmupdatedependencies.settings.NUDSettingsState
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import kotlinx.datetime.Clock
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import kotlin.time.Duration.Companion.minutes
import java.util.concurrent.TimeUnit

@Service(Service.Level.PROJECT)
class NPMJSClient(private val project: Project) {
Expand All @@ -30,7 +29,12 @@ class NPMJSClient(private val project: Project) {
fun getInstance(project: Project): NPMJSClient = project.service()
}

private val cache = mutableMapOf<String, CacheEntry<String>>()
private val cache = Caffeine.newBuilder()
.expireAfterWrite(NUDSettingsState.instance.cacheDurationMinutes.toLong(), TimeUnit.MINUTES)
.removalListener<String, String> { k, _, removalCause ->
log.debug("Package $k removed from cache: $removalCause")
}
.build<String, String>()

private fun getRegistry(packageName: String): String {
log.info("Getting registry for package $packageName")
Expand Down Expand Up @@ -67,20 +71,9 @@ class NPMJSClient(private val project: Project) {
}

private fun getResponseBody(uri: URI): String {
// Only cache /latest requests to save up on memory
val shouldUseCache = uri.path.endsWith("/latest")
if (shouldUseCache) {
cache[uri.toString()]?.let { cachedBody ->
if (Clock.System.now()
> cachedBody.addedAt + NUDSettingsState.instance.cacheDurationMinutes.minutes
) {
cache.remove(uri.toString())
return@let
}
log.debug("GET $uri (cached)")
return cachedBody.data
}
log.debug("Entry for $uri not found in cache")
cache.getIfPresent(uri.toString())?.let { cachedData ->
log.debug("GET $uri (from cache)")
return cachedData
}

log.debug("GET $uri")
Expand All @@ -90,9 +83,8 @@ class NPMJSClient(private val project: Project) {
return HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString())
.body().also { body ->
if (!shouldUseCache) return@also
log.debug("Caching response for $uri")
cache[uri.toString()] = CacheEntry(body, Clock.System.now())
cache.put(uri.toString(), body)
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.github.warningimhack3r.npmupdatedependencies.backend.models
import kotlinx.datetime.Instant

data class DataState<T>(
override val data: T?,
override val addedAt: Instant,
val data: T?,
val addedAt: Instant,
val comparator: String
) : AbstractCacheEntry<T?>
)

0 comments on commit e73a7a8

Please sign in to comment.