com.lightningkite.lightningserver.cache.MetricsCache.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of server-core Show documentation
Show all versions of server-core Show documentation
A set of tools to fill in/replace what Ktor is lacking in.
The newest version!
package com.lightningkite.lightningserver.cache
import com.lightningkite.lightningserver.metrics.MetricType
import com.lightningkite.lightningserver.metrics.MetricUnit
import com.lightningkite.lightningserver.metrics.Metrics
import kotlinx.serialization.KSerializer
import java.time.Duration
class MetricsCache(val wraps: Cache, metricsKeyName: String): Cache by wraps {
val metricKey = MetricType("$metricsKeyName Wait Time", MetricUnit.Milliseconds)
val countMetricKey = MetricType("$metricsKeyName Call Count", MetricUnit.Count)
override suspend fun get(key: String, serializer: KSerializer): T? = Metrics.addPerformanceToSumPerHandler(metricKey, countMetricKey) {
wraps.get(key = key, serializer = serializer)
}
override suspend fun set(key: String, value: T, serializer: KSerializer, timeToLive: Duration?) = Metrics.addPerformanceToSumPerHandler(metricKey, countMetricKey) {
wraps.set(key = key, value = value, serializer = serializer, timeToLive = timeToLive)
}
override suspend fun setIfNotExists(
key: String,
value: T,
serializer: KSerializer,
timeToLive: Duration?
): Boolean = Metrics.addPerformanceToSumPerHandler(metricKey, countMetricKey) {
wraps.setIfNotExists(key = key, value = value, serializer = serializer, timeToLive = timeToLive)
}
override suspend fun add(key: String, value: Int, timeToLive: Duration?) = Metrics.addPerformanceToSumPerHandler(metricKey, countMetricKey) {
wraps.add(key = key, value = value, timeToLive = timeToLive)
}
override suspend fun clear() = Metrics.addPerformanceToSumPerHandler(metricKey, countMetricKey) {
wraps.clear()
}
override suspend fun remove(key: String) = Metrics.addPerformanceToSumPerHandler(metricKey, countMetricKey) {
wraps.remove(key)
}
}