Caffeine cache logging
Asked Answered
D

1

8

I am using caffeine cache provider with spring annotations, I am not able to see the logs from caffeinecachemanager when the entry’s put in cache or evicted or any activities that happens on cache.Do I have to explicitly mention the properties to enable or disable the caffeine logs ?

Can anyone please help with the below ? How to log a message when @cachable populate the cache? How to log when a cache entry evicted from cache ? Is there any way I can check all cache entries stored with cache name ? Does AOP configurations required when using spring annotations and CaffeineCacheManager. ?if so pls help with the sample example.

Disenchant answered 9/2, 2018 at 6:56 Comment(2)
Any help from This forum can be appreciated !!!Disenchant
Does this answer your question? Spring cache logging on @Cacheable hitBenavidez
W
0

The ideal would be to use aspects, but I didn't want to delve into that as it was just for debugging to understand the actual underlying behavior.

So I did the below, using kotlin but you can adapt it to your needs.

First, created a delegator class:

class DelegatingCache<K, V>(
    val name: String,
    private val delegate: Cache<K, V>
) : Cache<K, V> {
    private val logger = LoggerFactory.getLogger(javaClass)

    override fun getIfPresent(key: K): V? = delegate.getIfPresent(key)
        .also { logger.warn("$name getIfPresent(key=$key) = $it") }

    override fun get(key: K, mappingFunction: Function<in K, out @PolyNull V>?): @PolyNull V =
        delegate.get(key, mappingFunction)
            .also { logger.warn("$name get(key=$key) = $it") }

    override fun getAllPresent(keys: Iterable<K>): Map<K, V> = delegate.getAllPresent(keys)
        .also { logger.warn("$name getAllPresent") }

    override fun getAll(
        keys: MutableIterable<K>?,
        mappingFunction: Function<in MutableSet<out K>, out MutableMap<out K, out V>>?
    ): MutableMap<K, V>? = delegate.getAll(keys, mappingFunction)
        .also { logger.warn("$name getAll") }

    override fun put(key: K, value: V) = delegate.put(key, value)
        .also { logger.warn("$name put(key=$key, value=$value)") }

    override fun putAll(map: Map<out K, V>) = delegate.putAll(map)
        .also { logger.warn("$name putAll") }

    override fun invalidate(key: K) = delegate.invalidate(key)
        .also { logger.warn("$name invalidate(key=$key)") }

    override fun invalidateAll(keys: Iterable<K>) = delegate.invalidateAll(keys)
        .also { logger.warn("$name invalidateAll(keys: Iterable<K>)") }

    override fun invalidateAll() = delegate.invalidateAll()
        .also { logger.warn("$name invalidateAll") }

    override fun estimatedSize(): Long = delegate.estimatedSize()
        .also { logger.warn("$name estimatedSize = $it") }

    override fun stats(): CacheStats = delegate.stats()
        .also { logger.warn("$name stats = $it") }

    override fun asMap(): ConcurrentMap<K, V> = delegate.asMap()
        .also { logger.warn("$name asMap") }

    override fun cleanUp() = delegate.cleanUp()
        .also { logger.warn("$name cleanUp") }

    override fun policy(): Policy<K, V> = delegate.policy()
        .also { logger.warn("$name policy = $it") }
}

Then, when you are building your Cache instances on your @Configuration class, you just wrap the cache in this delegator class before returning it.

private fun <K, V> buildCache(
    cacheName: String,
): Cache<K, V> {
    return Caffeine.newBuilder()
        // ...
        .build<K, V>()
        .let { DelegatingCache(cacheName, it) }
}

Cheers

Wes answered 21/12, 2023 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.