1
package com.tasks.cache.service
3
import com.tasks.cache.Cache
4
import org.redisson.api.RedissonClient
5
import org.slf4j.LoggerFactory
6
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
7
import org.springframework.context.annotation.Primary
8
import org.springframework.stereotype.Component
9
import java.io.Serializable
10
import java.util.concurrent.TimeUnit
15
@ConditionalOnProperty(prefix = "", name = ["redis.host"])
16
class RedisCache(private val redissonClient: RedissonClient) : Cache<Serializable> {
17
//TODO LT?? - may be switch to NEAR (local) cache
18
private val msgSuffix = "messages:"
19
private val msgExpSuffix = "messages_exp:" //diff space for possibility of getting the value
20
var logger = LoggerFactory.getLogger(RedisCache::class.java)!!
21
override fun get(key: String): Serializable? {
22
logger.debug("get with key $key")
23
return redissonClient.getBucket<Serializable>("$msgSuffix$key").get()
26
override fun put(key: String, value: Serializable, expiry:Long) {
27
logger.debug("put with key $key and expiry=$expiry")
28
redissonClient.getBucket<Serializable>("$msgSuffix$key").set(value)
30
redissonClient.getBucket<String>("$msgExpSuffix$key").set("_", expiry,TimeUnit.SECONDS)
34
override fun delete(key: String): Serializable? {
35
logger.debug("delete with key $key")
36
val bucket = redissonClient.getBucket<Serializable>("$msgSuffix$key")
37
val result = bucket.get()
39
redissonClient.getBucket<String>("$msgExpSuffix:$key").delete()