kotlin使用spring data redis(二)
生活随笔
收集整理的這篇文章主要介紹了
kotlin使用spring data redis(二)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
自定義序列化器
1.標(biāo)準(zhǔn)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.SerializationExceptionopen 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.Exceptionclass 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 = 0bytes.forEachIndexed { index, byte ->run {if (byte == '_'.toByte()) {counter++if(counter == 2){counter = indexreturn@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 nullval compressContent = bytes.sliceArray((counter + 1)..(bytes.size - 1))val decompressLength = if (compressContent.size > originContentLength.length) compressContent.size else originContentLength.lengthval 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 {@Beanfun redisTemplate(redisConnectionFactory: LettuceConnectionFactory): RedisTemplate<String, Any> {val template = RedisTemplate<String, Any>()template.keySerializer = StringRedisSerializer()template.valueSerializer = Jackson2ZstdSerializer()template.setConnectionFactory(redisConnectionFactory)return template} }4.用起來吧
@Autowiredprivate 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看一下
轉(zhuǎn)載于:https://my.oschina.net/weidedong/blog/2218577
總結(jié)
以上是生活随笔為你收集整理的kotlin使用spring data redis(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【测试基础】测试用例的设计方法
- 下一篇: 3种纯CSS实现中间镂空的12色彩虹渐变