欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot2.3整合redis緩存自定義序列化的實現(xiàn)

 更新時間:2020年08月12日 11:13:14   作者:xhnb  
這篇文章主要介紹了SpringBoot2.3整合redis緩存自定義序列化的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.引言

我們使用redis作為緩存中間件時,當(dāng)我們第一次查詢數(shù)據(jù)的時候,是去數(shù)據(jù)庫查詢,然后查到的數(shù)據(jù)封裝到實體類中,實體類會被序列化存入緩存中,當(dāng)?shù)诙尾閿?shù)據(jù)時,會直接去緩存中查找被序列化的數(shù)據(jù),然后反序列化被我們獲取。我們在緩存中看到的序列化數(shù)據(jù)不直觀,如果想看到類似json的數(shù)據(jù)格式,就需要自定義序列化規(guī)則。

2.整合redis

pom.xml:

<!--引入redis-->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>2.3.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
    </dependency>

application.yml:

spring:
 redis:
  host: 192.168.85.130
  port: 6379
  database: 0

springboot主配置類要加上@EnableCaching注解

3.自定義序列化

@Configuration
public class MyRedisConfig {
  @Bean
  public RedisTemplate<Object, Object> empRedisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException {
    RedisTemplate<Object,Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
    template.setDefaultSerializer(serializer);
    return template;
  }

  @Bean
  public CacheManager cacheManager(RedisConnectionFactory factory){
    RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
        .entryTtl(Duration.ofDays(1))
        .disableCachingNullValues()
        .serializeKeysWith(RedisSerializationContext.SerializationPair
            .fromSerializer(new StringRedisSerializer()))
        .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
    return RedisCacheManager.builder(factory).cacheDefaults(cacheConfiguration).build();}
}

4.測試

DeptService:

@Service
public class DeptService {
  @Autowired
  DepartmentMapper departmentMapper;
  @Cacheable(value = "dept")
  public Department findById(Integer id){
  System.out.println("查詢"+id+"號部門");
    Department department = departmentMapper.getDeptById(id);
    return department;
  }
}

EmployeeService:

@Service
public class EmployeeService {
  @Autowired
  EmployeeMapper employeeMapper;
 	@Cacheable(value = "emp")
  public Employee findById(Integer id){
    System.out.println("查詢"+id+"號員工");
    Employee employee = employeeMapper.getEmpById(id);
    return employee;
  }
}

@Cacheable(value = “dept”) :該注解在方法上,方法傳入?yún)?shù)默認為key值,方法返回值為value值,注解的參數(shù)value = "dept"是緩存的名子

結(jié)果:

到此這篇關(guān)于SpringBoot2.3整合redis緩存自定義序列化的實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot2.3 redis自定義序列化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java Web開發(fā)之基于Session的購物商店實現(xiàn)方法

    Java Web開發(fā)之基于Session的購物商店實現(xiàn)方法

    這篇文章主要介紹了Java Web開發(fā)之基于Session的購物商店實現(xiàn)方法,涉及Java針對session的操作及數(shù)據(jù)庫操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • Java使用jxl包寫Excel文件適合列寬實現(xiàn)

    Java使用jxl包寫Excel文件適合列寬實現(xiàn)

    用jxl.jar包,讀寫過Excel文件。也沒有注意最適合列寬的問題,但是jxl.jar沒有提供最適合列寬的功能,上次用到寫了一下,可以基本實現(xiàn)最適合列寬。
    2013-11-11
  • IntelliJ安裝并使用Rust IDE插件

    IntelliJ安裝并使用Rust IDE插件

    這篇文章主要介紹了IntelliJ安裝并使用Rust IDE插件,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • java把字符串寫入文件里的簡單方法分享

    java把字符串寫入文件里的簡單方法分享

    這篇文章主要介紹了java把字符串寫入到文件里的簡單方法,這是跟一個外國朋友學(xué)的代碼,需要的朋友可以參考下
    2014-03-03
  • 詳解記錄Java Log的幾種方式

    詳解記錄Java Log的幾種方式

    很多小伙伴不知道如何記錄日志,今天特地整理了本篇文章,文中有非常詳細的介紹及代碼示例,對小伙伴們很有幫助,需要的朋友可以參考下
    2021-06-06
  • spring boot 枚舉使用的坑整理

    spring boot 枚舉使用的坑整理

    在本篇文章里我們給大家整理了關(guān)于spring boot 枚舉使用的坑以及相關(guān)知識點內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-08-08
  • Mac電腦安裝多個JDK版本的詳細圖文教程

    Mac電腦安裝多個JDK版本的詳細圖文教程

    目前使用的主流版本還是JDK 8,但偶爾會想體驗下新版本(或者舊版本),如果能裝多個版本的JDK,而且很方便的切換就好了,這篇文章主要給大家介紹了關(guān)于Mac電腦安裝多個JDK版本的相關(guān)資料,需要的朋友可以參考下
    2024-03-03
  • SpringBoot整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題

    SpringBoot整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題

    這篇文章主要介紹了SpringBoot整合SpringSecurityOauth2實現(xiàn)鑒權(quán)-動態(tài)權(quán)限,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • 詳解SpringBoot中@NotNull,@NotBlank注解使用

    詳解SpringBoot中@NotNull,@NotBlank注解使用

    這篇文章主要為大家詳細介紹了Spring?Boot中集成Validation與@NotNull,@NotBlank等注解的簡單使用,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-08-08
  • 詳解SpringBoot開發(fā)使用@ImportResource注解影響攔截器

    詳解SpringBoot開發(fā)使用@ImportResource注解影響攔截器

    這篇文章主要介紹了詳解SpringBoot開發(fā)使用@ImportResource注解影響攔截器,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11

最新評論