成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

springboot 整合redis

elarity / 1513人閱讀

摘要:與整合默認(rèn)使用的是,相較于,是一個(gè)可伸縮的,線程安全的客戶端。在處理高并發(fā)方面有更多的優(yōu)勢(shì)。使用依賴主要需要的依賴為配置配置使用與整合可以在不更改現(xiàn)有代碼邏輯的基礎(chǔ)上,通過(guò)增加注解的方式,實(shí)現(xiàn)緩存。

springboot2.0 與redis整合默認(rèn)使用的是Lettuce,相較于jedis,lettuce 是一個(gè)可伸縮的,線程安全的redis客戶端。在處理高并發(fā)方面有更多的優(yōu)勢(shì)。

RedisTemplate 使用

依賴, 主要需要的依賴為

 compile("org.springframework.boot:spring-boot-starter-data-redis")
 compile("org.apache.commons:commons-pool2:2.6.0")
 compile("com.alibaba:fastjson:1.2.33")

yml 配置

 spring:
   redis:
     database: 1
     host: 192.168.1.XX
     port: 6379
     lettuce:
       pool:
         max-active: 8
         max-wait: -1ms
         min-idle: 0

redisTemplate 配置

@Configuration
public class RedisConfiguration {

@Bean
public RedisTemplate redisTemplate(LettuceConnectionFactory connectionFactory){
    RedisTemplate template = new RedisTemplate();
    template.setConnectionFactory(connectionFactory);

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setKeySerializer(new StringRedisSerializer());
    template.afterPropertiesSet();

    return  template;
    }
}
  
1. 使用
 @Autowired
RedisTemplate redisTemplate;
 public void addData(){
    MyInfo myInfo = new MyInfo("11","22","s344");
    redisTemplate.opsForValue().set("11",myInfo);
    redisTemplate.opsForValue().set("myse","mys");
}
```
spring-cache 與 redis 整合

spring cache 可以在不更改現(xiàn)有代碼邏輯的基礎(chǔ)上,通過(guò)增加注解的方式,實(shí)現(xiàn)緩存。 減少代碼侵入

在gradle文件中增加 spring-cache的相關(guān)依賴 compile("org.springframework.boot:spring-boot-starter-cache")

配置CacheManager, 在Configuration 配置類上增加EnableCache 注解;

配置CacheManager Bean,代碼為

    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
     ObjectMapper objectMapper = new ObjectMapper();
     objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
     objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
     jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

     RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);

     RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
     cacheConfiguration = cacheConfiguration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));
     cacheConfiguration.entryTtl(Duration.ofMinutes(10));
     CacheManager cacheManager = new RedisCacheManager(redisCacheWriter, cacheConfiguration);
     return cacheManager;

完成上述配置后,便可以在service中直接使用spring-cache注解,完整代碼: 代碼

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/62035.html

相關(guān)文章

  • SpringBoot非官方教程 | 第九篇: SpringBoot整合Redis

    摘要:經(jīng)過(guò)上述兩步的操作,你可以訪問(wèn)數(shù)據(jù)了。數(shù)據(jù)訪問(wèn)層通過(guò)來(lái)訪問(wèn)分鐘過(guò)期單元測(cè)試啟動(dòng)單元測(cè)試,你發(fā)現(xiàn)控制臺(tái)打印了單元測(cè)試通過(guò)源碼下載參考資料 這篇文章主要介紹springboot整合redis 引入依賴 在pom文件中添加redis依賴: org.springframework.boot spring-boot-starter-data-redis 配置數(shù)據(jù)源 spri...

    csRyan 評(píng)論0 收藏0
  • 兩年了,我寫(xiě)了這些干貨!

    摘要:開(kāi)公眾號(hào)差不多兩年了,有不少原創(chuàng)教程,當(dāng)原創(chuàng)越來(lái)越多時(shí),大家搜索起來(lái)就很不方便,因此做了一個(gè)索引幫助大家快速找到需要的文章系列處理登錄請(qǐng)求前后端分離一使用完美處理權(quán)限問(wèn)題前后端分離二使用完美處理權(quán)限問(wèn)題前后端分離三中密碼加鹽與中異常統(tǒng)一處理 開(kāi)公眾號(hào)差不多兩年了,有不少原創(chuàng)教程,當(dāng)原創(chuàng)越來(lái)越多時(shí),大家搜索起來(lái)就很不方便,因此做了一個(gè)索引幫助大家快速找到需要的文章! Spring Boo...

    huayeluoliuhen 評(píng)論0 收藏0
  • springboot系列】springboot整合獨(dú)立模塊 redis 做緩存

    摘要:至此,已完成整合獨(dú)立模塊做緩存詳情請(qǐng)看地址相關(guān)文章系列整合獨(dú)立模塊 項(xiàng)目github地址:https://github.com/5-Ason/aso...具體可看 ./db/db-redis 和 ./db/db-cache 兩個(gè)模塊 // TODO 在整合redis之前需要先本地配置好redis環(huán)境,遲點(diǎn)有時(shí)間補(bǔ)一下linux下下載安裝配置redis 本文主要實(shí)現(xiàn)的是對(duì)數(shù)據(jù)操作進(jìn)行獨(dú)立...

    qianfeng 評(píng)論0 收藏0
  • springboot系列】springboot整合獨(dú)立模塊 redis 做緩存

    摘要:至此,已完成整合獨(dú)立模塊做緩存詳情請(qǐng)看地址相關(guān)文章系列整合獨(dú)立模塊 項(xiàng)目github地址:https://github.com/5-Ason/aso...具體可看 ./db/db-redis 和 ./db/db-cache 兩個(gè)模塊 // TODO 在整合redis之前需要先本地配置好redis環(huán)境,遲點(diǎn)有時(shí)間補(bǔ)一下linux下下載安裝配置redis 本文主要實(shí)現(xiàn)的是對(duì)數(shù)據(jù)操作進(jìn)行獨(dú)立...

    Jokcy 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<