本文提綱 一、緩存的應(yīng)用場景 二、更新緩存的策略 三、運行?springboot-mybatis-redis?工程案例 四、springboot-mybatis-redis?工程代碼配置詳解 運行環(huán)境: Mac OS 10.12.x JDK 8 + Redis 3.2.8 Spring Boot 1.5.1.RELEASE
一、緩存的應(yīng)用場景
什么是緩存? 在互聯(lián)網(wǎng)場景下,尤其 2C 端大流量場景下,需要將一些經(jīng)常展現(xiàn)和不會頻繁變更的數(shù)據(jù),存放在存取速率更快的地方。緩存就是一個存儲器,在技術(shù)選型中,常用 Redis 作為緩存數(shù)據(jù)庫。緩存主要是在獲取資源方便性能優(yōu)化的關(guān)鍵方面。 Redis 是一個高性能的 key-value 數(shù)據(jù)庫。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. 緩存的應(yīng)用場景有哪些呢? 比如常見的電商場景,根據(jù)商品 ID 獲取商品信息時,店鋪信息和商品詳情信息就可以緩存在 Redis,直接從 Redis 獲取。減少了去數(shù)據(jù)庫查詢的次數(shù)。但會出現(xiàn)新的問題,就是如何對緩存進行更新?這就是下面要講的。
二、更新緩存的策略
參考《緩存更新的套路》http://coolshell.cn/articles/17416.html,緩存更新的模式有四種:Cache aside, Read through, Write through, Write behind caching。 這里我們使用的是 Cache Aside 策略,從三個維度:(摘自 耗子叔叔博客) 失效:應(yīng)用程序先從cache取數(shù)據(jù),沒有得到,則從數(shù)據(jù)庫中取數(shù)據(jù),成功后,放到緩存中。 命中:應(yīng)用程序從cache中取數(shù)據(jù),取到后返回。 更新:先把數(shù)據(jù)存到數(shù)據(jù)庫中,成功后,再讓緩存失效。 大致流程如下: 獲取商品詳情舉例 a. 從商品 Cache 中獲取商品詳情,如果存在,則返回獲取 Cache 數(shù)據(jù)返回。 b. 如果不存在,則從商品 DB 中獲取。獲取成功后,將數(shù)據(jù)存到 Cache 中。則下次獲取商品詳情,就可以從 Cache 就可以得到商品詳情數(shù)據(jù)。 c. 從商品 DB 中更新或者刪除商品詳情成功后,則從緩存中刪除對應(yīng)商品的詳情緩存
<?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 連接驅(qū)動依賴 -->????????<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>