成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

基于內(nèi)存和 Redis 的兩級 Java 緩存框架

開發(fā) 前端
第一級緩存使用內(nèi)存(同時支持 Ehcache 2.x、Ehcache 3.x 和 Caffeine),第二級緩存使用 Redis(推薦)/Memcached 。由于大量的緩存讀取會導(dǎo)致 L2 的網(wǎng)絡(luò)成為整個系統(tǒng)的瓶頸,因此 L1 的目標(biāo)是降低對 L2 的讀取次數(shù)。

環(huán)境:SpringBoot2.7.12 + j2cache2.8.5

1. 簡介

J2Cache 是 OSChina 目前正在使用的兩級緩存框架(要求至少 Java 8)。第一級緩存使用內(nèi)存(同時支持 Ehcache 2.x、Ehcache 3.x 和 Caffeine),第二級緩存使用 Redis(推薦)/Memcached 。由于大量的緩存讀取會導(dǎo)致 L2 的網(wǎng)絡(luò)成為整個系統(tǒng)的瓶頸,因此 L1 的目標(biāo)是降低對 L2 的讀取次數(shù)。該緩存框架主要用于集群環(huán)境中。單機(jī)也可使用,用于避免應(yīng)用重啟導(dǎo)致的緩存冷啟動后對后端業(yè)務(wù)的沖擊。

數(shù)據(jù)讀取

  • 讀取順序 -> L1 -> L2 -> DB
  • 數(shù)據(jù)更新
    從數(shù)據(jù)庫中讀取最新數(shù)據(jù),依次更新 L1 -> L2 ,發(fā)送廣播清除某個緩存信息
    接收到廣播(手工清除緩存 & 一級緩存自動失效),從 L1 中清除指定的緩存信息

2. 實(shí)戰(zhàn)案例

2.1 依賴管理

<dependency>
  <groupId>net.oschina.j2cache</groupId>
  <artifactId>j2cache-core</artifactId>
  <version>2.8.5-release</version>
</dependency>
<dependency>
  <groupId>net.oschina.j2cache</groupId>
  <artifactId>j2cache-spring-boot2-starter</artifactId>
  <version>2.8.0-release</version>
</dependency>

2.2 配置

redis:
  # 地址, 多個地址使用‘,’逗號分割
  hosts: localhost:6379
  # 數(shù)據(jù)庫索引
  database: 11
  # 密碼
  password: xxxooo
  # 連接超時時間
  timeout: 10s
  # 連接池中的最小空閑連接
  min-idle: 0
  # 連接池中的最大空閑連接
  max-idle: 8
  # 連接池的最大數(shù)據(jù)庫連接數(shù)
  max-active: 8
  # #連接池最大阻塞等待時間(使用負(fù)值表示沒有限制)
  max-wait: -1ms
---
j2cache:
  openSpringCache: true
  # 緩存中不存在時,運(yùn)行緩存空對象
  allowNullValues: true
  redisClient: lettuce
  l2CacheOpen: true
  # 一級緩存使用caffeine
  L1:
    provider_class: caffeine
  L2:
    #使用springRedis替換二級緩存
    provider_class: net.oschina.j2cache.cache.support.redis.SpringRedisProvider
    config_section: redis
  #使用springRedis進(jìn)行廣播通知緩失效
  broadcast: net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy
# 上面配置的一級緩存為caffeine, 那么這里對一級緩存的配置就必須以這個caffeine開頭  
caffeine:
  # 配置一級,二級緩存的region,有效時間
  region.xj: 10000, 120s
---
spring:
  cache:
    # 一級緩存使用caffeine
    type: caffeine

2.3 核心操作類

@Service
public class UserService {


  private final UserRepository userRepository ;
  public UserService(UserRepository userRepository) {
    this.userRepository = userRepository ;
  }
  @Transactional
  public User save(User user) {
    return this.userRepository.saveAndFlush(user) ;
  }
  @Cacheable(value = {"xj"}, key = "#id")
  public User get(Long id) {
    return this.userRepository.findById(id).orElse(null) ;
  }
  @Transactional
  @CacheEvict(value = {"xj"}, key = "#id")
  public void remove(Long id) {
    this.userRepository.deleteById(id) ;
  }
}

以上是基本的操作,非常簡單。

2.4 Controller接口

@RestController
@RequestMapping("/users")
public class UserController {


  private final UserService userService ;
  // 通過CacheChannel操作j2cache緩存方法
  private final CacheChannel cacheChannel;
  
  public UserController(UserService userService, CacheChannel cacheChannel) {
    this.userService = userService ;
    this.cacheChannel = cacheChannel ; 
  }
  
  @GetMapping("/save")
  public User save() {
    User user = new User() ;
    int num = new Random().nextInt(80);
    user.setAge(num) ;
    user.setName("姓名 - " + num) ;
    user.setSex(num >= 50 ? "男" : "女") ;
    return this.userService.save(user) ;
  }
  
  @GetMapping("/{id}")
  public Object get(@PathVariable("id") Long id) {
    // 從指定的region,指定的key獲取數(shù)據(jù),如果一級,二級緩存中不存在,則通過第三個參數(shù)Function手動獲取
    // 如果緩存中不存在時,同時配置了允許緩存空對象,則會緩存一個空對象到緩存中
    return this.cacheChannel.get("xj", id.toString(), key -> this.userService.get(id) , true) ;
  }
  
  @GetMapping("/delete/{id}")
  public Object remove(@PathVariable("id") Long id) {
    this.userService.remove(id) ;
    return "success" ;
  }
  
}

2.5 測試

先通過save接口添加數(shù)據(jù)

圖片圖片

查詢id=2的數(shù)據(jù)

圖片圖片

level=3 表示本次數(shù)據(jù)緩存中不存在,從數(shù)據(jù)庫中獲取的。刷新頁面

圖片圖片

level=2,本次數(shù)據(jù)從二級緩存redis中獲取。再次刷新頁面

圖片圖片

level=1,本次數(shù)據(jù)從一級緩存caffeine中獲取。后續(xù)再怎么刷新只要緩存沒有過期都將從一級緩存中獲取。

測試不存在的數(shù)據(jù)

圖片圖片

從數(shù)據(jù)庫中查詢不存在的數(shù)據(jù)。

圖片圖片

緩存了空對象。

測試刪除數(shù)據(jù)

圖片圖片

緩存中會立即清除

圖片圖片

以上是本篇文章的全部內(nèi)容,希望對你有幫助。

完畢!!!

責(zé)任編輯:武曉燕 來源: Spring全家桶實(shí)戰(zhàn)案例源碼
相關(guān)推薦

2019-07-10 15:41:50

RedisJava緩存

2022-03-31 13:58:37

分布式SpringRedis

2022-12-16 12:16:21

2022-04-15 11:26:14

緩存功能

2022-03-18 13:59:46

緩存RedisCaffeine

2023-02-26 11:15:42

緩存循環(huán)依賴

2017-05-12 15:01:11

政務(wù)云江西云計(jì)算

2017-05-15 11:50:47

江西政務(wù)云

2019-07-04 15:13:16

分布式緩存Redis

2017-04-11 09:30:46

互聯(lián)網(wǎng)

2022-04-28 13:58:41

Redis6客戶端服務(wù)端

2024-12-03 14:38:07

CaffeineRedis二級緩存

2019-10-11 08:41:18

JavaMemcached數(shù)據(jù)庫

2018-05-05 14:44:19

工作管理崗位

2021-06-02 06:49:18

Redis緩存設(shè)計(jì).

2024-11-11 17:12:22

2009-06-30 14:08:00

Hibernate緩存

2022-05-27 09:25:12

攜程酒店本地緩存查詢服務(wù)

2012-12-17 14:54:55

算法緩存Java

2009-12-09 09:57:05

ibmdwJavaPHP
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 欧美一区二区在线播放 | 久久69精品久久久久久久电影好 | 亚洲三级免费看 | av黄色片 | 国产精品免费在线 | 中文字幕在线第一页 | 国产91久久久久久 | 亚洲第一区国产精品 | 91国内精品 | 91亚洲国产成人久久精品网站 | 性一交一乱一伦视频免费观看 | 国产一级久久久久 | 古典武侠第一页久久777 | 久久久精彩视频 | 99久久久久 | 久久机热| 日韩欧美国产精品综合嫩v 一区中文字幕 | 亚洲一区二区三区视频免费观看 | 成在线人视频免费视频 | 国产精品久久久久久久久久久久久 | 成人av播放 | 国产精品成人一区二区三区吃奶 | www亚洲精品 | 人人爱干 | 男女网站视频 | 色在线视频网站 | 中文字幕在线视频免费视频 | 国产精品成人一区二区三区 | 免费精品一区 | 在线一区二区三区 | 亚洲国产一区二区三区在线观看 | 国产精品视频久久久久久 | 99久久精品免费看国产高清 | 精品国产乱码久久久久久闺蜜 | 国产精品久久久久久久久久久久 | 亚洲福利一区二区 | 91九色在线观看 | 久久亚洲一区二区三区四区 | 欧美一级久久 | 国产成人在线播放 | 中文字幕亚洲免费 |