本文提綱 一、緩存的應用場景 二、更新緩存的策略 三、運行?springboot-mybatis-redis?工程案例 四、springboot-mybatis-redis?工程代碼配置詳解 運行環境: Mac OS 10.12.x JDK 8 + Redis 3.2.8 Spring Boot 1.5.1.RELEASE
一、緩存的應用場景
什么是緩存? 在互聯網場景下,尤其 2C 端大流量場景下,需要將一些經常展現和不會頻繁變更的數據,存放在存取速率更快的地方。緩存就是一個存儲器,在技術選型中,常用 Redis 作為緩存數據庫。緩存主要是在獲取資源方便性能優化的關鍵方面。 Redis 是一個高性能的 key-value 數據庫。GitHub 地址:https://github.com/antirez/redis?。Github 是這么描述的: Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values are supported: Strings, Lists, Sets, Sorted Sets, Hashes, HyperLogLogs, Bitmaps. 緩存的應用場景有哪些呢? 比如常見的電商場景,根據商品 ID 獲取商品信息時,店鋪信息和商品詳情信息就可以緩存在 Redis,直接從 Redis 獲取。減少了去數據庫查詢的次數。但會出現新的問題,就是如何對緩存進行更新?這就是下面要講的。
二、更新緩存的策略
參考《緩存更新的套路》http://coolshell.cn/articles/17416.html,緩存更新的模式有四種:Cache aside, Read through, Write through, Write behind caching。 這里我們使用的是 Cache Aside 策略,從三個維度:(摘自 耗子叔叔博客) 失效:應用程序先從cache取數據,沒有得到,則從數據庫中取數據,成功后,放到緩存中。 命中:應用程序從cache中取數據,取到后返回。 更新:先把數據存到數據庫中,成功后,再讓緩存失效。 大致流程如下: 獲取商品詳情舉例 a. 從商品 Cache 中獲取商品詳情,如果存在,則返回獲取 Cache 數據返回。 b. 如果不存在,則從商品 DB 中獲取。獲取成功后,將數據存到 Cache 中。則下次獲取商品詳情,就可以從 Cache 就可以得到商品詳情數據。 c. 從商品 DB 中更新或者刪除商品詳情成功后,則從緩存中刪除對應商品的詳情緩存
<?xml version="1.0"?encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?????????xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">????<modelVersion>4.0.0</modelVersion>????<groupId>springboot</groupId>????<artifactId>springboot-mybatis-redis</artifactId>????<version>0.0.1-SNAPSHOT</version>????<name>springboot-mybatis-redis :: 整合 Mybatis 并使用 Redis 作為緩存</name>????<!-- Spring Boot 啟動父依賴 -->????<parent>????????<groupId>org.springframework.boot</groupId>????????<artifactId>spring-boot-starter-parent</artifactId>????????<version>1.5.1.RELEASE</version>????</parent>????<properties>????????<mybatis-spring-boot>1.2.0</mybatis-spring-boot>????????<mysql-connector>5.1.39</mysql-connector>????????<spring-boot-starter-redis-version>1.3.2.RELEASE</spring-boot-starter-redis-version>????</properties>????<dependencies>????????<!-- Spring Boot Reids 依賴 -->????????<dependency>????????????<groupId>org.springframework.boot</groupId>????????????<artifactId>spring-boot-starter-redis</artifactId>????????????<version>${spring-boot-starter-redis-version}</version>????????</dependency>????????<!-- Spring Boot Web 依賴 -->????????<dependency>????????????<groupId>org.springframework.boot</groupId>????????????<artifactId>spring-boot-starter-web</artifactId>????????</dependency>????????<!-- Spring Boot Test 依賴 -->????????<dependency>????????????<groupId>org.springframework.boot</groupId>????????????<artifactId>spring-boot-starter-test</artifactId>????????????<scope>test</scope>????????</dependency>????????<!-- Spring Boot Mybatis 依賴 -->????????<dependency>????????????<groupId>org.mybatis.spring.boot</groupId>????????????<artifactId>mybatis-spring-boot-starter</artifactId>????????????<version>${mybatis-spring-boot}</version>????????</dependency>????????<!-- MySQL 連接驅動依賴 -->????????<dependency>????????????<groupId>mysql</groupId>????????????<artifactId>mysql-connector-java</artifactId>????????????<version>${mysql-connector}</version>????????</dependency>????????<!-- Junit -->????????<dependency>????????????<groupId>junit</groupId>????????????<artifactId>junit</artifactId>????????????<version>4.12</version>????????</dependency>????</dependencies></project>