日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

kotlin使用spring data redis(二)

發布時間:2024/4/13 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 kotlin使用spring data redis(二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

自定義序列化器

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.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看一下

轉載于:https://my.oschina.net/weidedong/blog/2218577

總結

以上是生活随笔為你收集整理的kotlin使用spring data redis(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。