天天看點

Springboot中緩存實作方案

抽象和接口的差別

  • ​​寫在前面​​
  • ​​一、本地緩存​​
  • ​​1.1、Guava + @Cacheable​​
  • ​​1.2、EhCache + @Cacheable​​
  • ​​1.3、HazelCast + @Cacheable()​​
  • ​​二、緩存伺服器 Redis​​
  • ​​2.1、Redis + @Cacheable​​
  • ​​2.2、Redistemplate​​
  • ​​2.3、Redission​​

寫在前面

Spring中,緩存的使用,主要是配置一個cacheManage,緩存資料的存放,就要考慮,是快取區域還是緩存伺服器,快取區域有很多優秀的基于本地緩存的架構,Guava,EhCache,HazelCast,緩存伺服器主要是redis,Memcache等

一、本地緩存

1.1、Guava + @Cacheable

1.2、EhCache + @Cacheable

1.3、HazelCast + @Cacheable()

HazelCast,是一個很優秀的緩存架構,和Derby等其他 常作為嵌入式資料庫設計方案,分布式架構中的分布式配置,會考慮用到這些可嵌入式的資料庫。

二、緩存伺服器 Redis

2.1、Redis + @Cacheable

需要配置序列化機制,還有自定義緩存時間,需要額外的配置

2.2、Redistemplate

2.3、Redission

spring:
  redis:
    redisson:
      config: classpath:redisson-config.yaml      
#Redisson配置
singleServerConfig:
  address: "redis://ip:6379"
  password: null
  clientName: null
  database: 15 # 選擇使用哪個資料庫0~15
  idleConnectionTimeout: 10000
  pingTimeout: 1000
  connectTimeout: 10000
  timeout: 3000
  retryAttempts: 3
  retryInterval: 1500
  reconnectionTimeout: 3000
  failedAttempts: 3
  subscriptionsPerConnection: 5
  subscriptionConnectionMinimumIdleSize: 1
  subscriptionConnectionPoolSize: 50
  connectionMinimumIdleSize: 32
  connectionPoolSize: 64
  dnsMonitoringInterval: 5000
  #dnsMonitoring: false

threads: 0
nettyThreads: 0
codec:
  class: "org.redisson.codec.JsonJacksonCodec"
transportMode: "NIO"