35. Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

【本文章是否对你有用以及是否有好的建议,请留言】

本文章牵涉到的技术点比较多:Spring Data JPA、Redis、Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对以上这些技术点有一定的了解或者也可以先看看这篇文章,针对文章中实际的技术点在进一步了解(注意,您需要自己下载Redis Server到您的本地,所以确保您本地的Redis可用,这里还使用了MySql数据库,当然你也可以内存数据库进行测试)。这篇文章会提供对应的Eclipse代码示例,具体大体的分如下几个步骤:

(1)新建Java MavenProject;

(2)在pom.xml中添加相应的依赖包;

(3)编写Spring Boot启动类;

(4)配置application.properties;

(5)编写RedisCacheConfig配置类;

(6)编写DemoInfo测试实体类;

(7)编写DemoInfoRepository持久化类;

(8)编写DemoInfoService类;

(9)编写DemoInfoController类;

(10)测试代码是否正常运行了

(11)自定义缓存key;

接下来我们看看具体每个步骤具体的操作吧。

(1)新建Java Maven Project;

这个步骤就不细说,新建一个spring-boot-redis Java mavenproject;

(2)在pom.xml中添加相应的依赖包;

在Maven中添加相应的依赖包,主要有:springboot 父节点依赖;spring boot web支持;缓存服务spring-context-support;添加redis支持;JPA操作数据库;mysql 数据库驱动,具体pom.xml文件如下:

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="" target="_blank">http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"" target="_blank">>

<modelVersion>4.0.0</modelVersion>

<groupId>com.kfit</groupId>

<artifactId>spring-boot-redis</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>spring-boot-redis</name>

<url>http://maven.apache.org</url>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!--配置JDK编译版本. -->

<java.version>1.8</java.version>

</properties>

<!-- spring boot父节点依赖,

引入这个之后相关的引入就不需要添加version配置,

spring boot会自动选择最合适的版本进行添加。

-->

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.3.3.RELEASE</version>

</parent>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<scope>test</scope>

</dependency>

<!-- springboot web支持:mvc,aop... -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!--

包含支持UI模版(Velocity,FreeMarker,JasperReports),

邮件服务,

脚本服务(JRuby),

缓存Cache(EHCache),

任务计划Scheduling(uartz)。

-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

</dependency>

<!-- 添加redis支持-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-redis</artifactId>

</dependency>

<!-- JPA操作数据库. -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

<!-- mysql数据库驱动. -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>

<!-- 单元测试. -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

</project>

上面是完整的pom.xml文件,每个里面都进行了简单的注释。

(3)编写Spring Boot启动类(com.kfit.App);

package com.kfit;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

* Spring Boot启动类;

*

* @authorAngel(QQ:412887952)

* @version v.0.1

*/

@SpringBootApplication

public class App {

/**

*-javaagent:.\lib\springloaded-1.2.4.RELEASE.jar -noverify

* @param args

*/

public static voidmain(String[] args) {

SpringApplication.run(App.class,args);

}

}

(4)配置application.properties;

这里主要是配置两个资源,第一就是数据库基本信息;第二就是redis配置;第三就是JPA的配置;

Src/main/resouces/application.properties:

########################################################

###datasource 配置MySQL数据源;

########################################################

spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username= root

spring.datasource.password= root

spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.datasource.max-active=20

spring.datasource.max-idle=8

spring.datasource.min-idle=8

spring.datasource.initial-size=10

########################################################

###REDIS (RedisProperties) redis基本配置;

########################################################

# database name

spring.redis.database=0

# server host1

spring.redis.host=127.0.0.1

# server password

#spring.redis.password=

#connection port

spring.redis.port=6379

# pool settings ...

spring.redis.pool.max-idle=8

spring.redis.pool.min-idle=0

spring.redis.pool.max-active=8

spring.redis.pool.max-wait=-1

# name of Redis server

#spring.redis.sentinel.master=

# comma-separated list of host:portpairs

#spring.redis.sentinel.nodes=

########################################################

### Java Persistence Api自动进行建表

########################################################

# Specify the DBMS

spring.jpa.database= MYSQL

# Show or not log for each sqlquery

spring.jpa.show-sql= true

# Hibernate ddl auto (create,create-drop, update)

spring.jpa.hibernate.ddl-auto= update

# Naming strategy

spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy

# stripped before adding them tothe entity manager)

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

(5)编写RedisCacheConfig配置类;

缓存主要有几个要实现的类:其一就是CacheManager缓存管理器;其二就是具体操作实现类;其三就是CacheManager工厂类(这个可以使用配置文件配置的进行注入,也可以通过编码的方式进行实现);其四就是缓存key生产策略(当然Spring自带生成策略,但是在Redis客户端进行查看的话是系列化的key,对于我们肉眼来说就是感觉是乱码了,这里我们先使用自带的缓存策略)。

。。。。。。。。。。。。。。。。。

版权原因,完整文章,请参考如下:35. Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值