kotlin使用spring data redis(二)


声明:本文转载自https://my.oschina.net/weidedong/blog/2218577,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

自定义序列化器

1.标准json序列化器,时间类型禁用时间戳

import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.springframework.data.redis.serializer.RedisSerializer
import org.springframework.data.redis.serializer.SerializationException

open class Jackson2Serializer : RedisSerializer<Any> {
    private var mapper: ObjectMapper = jacksonObjectMapper()

    init {
        mapper.registerModules(Jdk8Module(), JavaTimeModule())
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
    }

    override fun serialize(t: Any?): ByteArray? {
        if (t == null) {
            return ByteArray(0)
        }

        try {
            return mapper.writeValueAsBytes(t)
        } catch (e: JsonProcessingException) {
            throw SerializationException("Could not write JSON: " + e.message, e)
        }


    }

    override fun deserialize(bytes: ByteArray?): Any? {
        if (bytes == null) {
            return null
        }

        try {
            return mapper.readValue(bytes)
        } catch (e: Exception) {
            throw SerializationException("Could not read JSON: " + e.message, e)
        }
    }

}

2.支持压缩(zstd)

import com.fasterxml.jackson.core.JsonProcessingException
import com.github.luben.zstd.Zstd
import org.springframework.data.redis.serializer.SerializationException
import java.lang.Exception

class Jackson2ZstdSerializer : Jackson2Serializer() {


    override fun serialize(t: Any?): ByteArray? {

        if (t == null) {
            return ByteArray(0)
        }
        try {
            val json = super.serialize(t)
            val compressContent = Zstd.compress(json)
            val compressHeader = "zstd_${json!!.size}_".toByteArray()
            return compressHeader + compressContent
        } catch (e: JsonProcessingException) {
            throw e
        } catch (ex: Exception) {
            throw SerializationException("Could not compress JSON: " + ex.message, ex)
        }
    }

    override fun deserialize(bytes: ByteArray?): Any? {
        if (bytes == null) {
            return null
        }

        try {
            var counter = 0
            bytes.forEachIndexed { index, byte ->
                run {
                    if (byte == '_'.toByte()) {
                        counter++
                        if(counter == 2){
                            counter = index
                            return@forEachIndexed
                        }
                    }
                }
            }


            val compressHeader = bytes.sliceArray(0..counter)
            val compressHeaderString = String(compressHeader)
            if (!compressHeaderString.contains("zstd")) {
                return null
            }
            val originContentLength = "[0-9]+".toRegex().find(compressHeaderString)?.value ?: return null
            val compressContent = bytes.sliceArray((counter + 1)..(bytes.size - 1))
            val decompressLength = if (compressContent.size > originContentLength.length) compressContent.size else originContentLength.length
            val decompressContent = Zstd.decompress(compressContent, decompressLength)
            return super.deserialize(decompressContent)

        } catch (e: Exception) {
            throw SerializationException("Could not read JSON: " + e.message, e)
        }
    }

3.启用Jackson2ZstdSerializer

@Configuration
class RedisCacheAutoConfiguration {

    @Bean
    fun redisTemplate(redisConnectionFactory: LettuceConnectionFactory): RedisTemplate<String, Any> {

        val template = RedisTemplate<String, Any>()
        template.keySerializer = StringRedisSerializer()
        template.valueSerializer = Jackson2ZstdSerializer()
        template.setConnectionFactory(redisConnectionFactory)
        return template
    }
}

4.用起来吧

@Autowired
  private lateinit var redisTemplate: RedisTemplate<String, Any>
  
    redisTemplate.opsForValue().set("aaa","aa",100,TimeUnit.SECONDS)
        val p = Passenger(1,"zhangsan", LocalDateTime.parse("2018-08-09T12:33:22.123"))
        redisTemplate.opsForValue().set("user",p,100,TimeUnit.SECONDS)

5.用Redis Desk Manager看一下

本文发表于2018年09月28日 12:00
(c)注:本文转载自https://my.oschina.net/weidedong/blog/2218577,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 1849 讨论 1 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1