溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-08-13 11:23:43 來(lái)源:億速云 閱讀:439 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)實(shí)現(xiàn)SpringBoot2.3整合redis緩存自定義序列化的方法,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

1.引言

我們使用redis作為緩存中間件時(shí),當(dāng)我們第一次查詢數(shù)據(jù)的時(shí)候,是去數(shù)據(jù)庫(kù)查詢,然后查到的數(shù)據(jù)封裝到實(shí)體類中,實(shí)體類會(huì)被序列化存入緩存中,當(dāng)?shù)诙尾閿?shù)據(jù)時(shí),會(huì)直接去緩存中查找被序列化的數(shù)據(jù),然后反序列化被我們獲取。我們?cè)诰彺嬷锌吹降男蛄谢瘮?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.測(cè)試

DeptService:

@Service
public class DeptService {
  @Autowired
  DepartmentMapper departmentMapper;
  @Cacheable(value = "dept")
  public Department findById(Integer id){
  System.out.println("查詢"+id+"號(hào)部門");
    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+"號(hào)員工");
    Employee employee = employeeMapper.getEmpById(id);
    return employee;
  }
}

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

結(jié)果:

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

關(guān)于實(shí)現(xiàn)SpringBoot2.3整合redis緩存自定義序列化的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI