All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lightningkite.lightningserver.cache.MetricsCache.kt Maven / Gradle / Ivy

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)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy