最近花空余时间学习了一下redis缓存,redis作为一个缓存的技术框架很受开发者的喜欢,我觉得有必要去学习一下。
下面是我整合的一个小demo:
1.创建一个springboot项目,文件层次如下图所示:
2.在pom.xml中加入mysql,redis,jpa等依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
pom.xml文件:
<?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>com.example</groupId>
<artifactId>redis-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>redis-test</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.28</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.依赖包配置好了,接下来,配置application.properties文件:
#端口号设定
server.port=8400
# database数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/tjytest?useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#最大活跃数
spring.datasource.max-active=20
#初始化数量
spring.datasource.initial-size=1
#最大连接等待超越时间
spring.datasource.max-wait=60000
#打开PSCache,并且指定每个连接Cache的大小
spring.datasource.pool-prepared-statements=true
spring.datasource.maximum-pool-size=20
#jpa
spring.jpa.show-sql=true
#配置redis
spring.redis.port=6379
spring.redis.host=127.0.0.1
spring.redis.database=0
spring.redis.jedis.pool.max-active=20
spring.redis.jedis.pool.max-idle=20
spring.redis.jedis.pool.min-idle=1
spring.redis.jedis.pool.max-wait=60000
spring.redis.timeout=0
4.在java目录下新建一个config包,在其中建一个RedisConfiguration.java文件,代码如下所示:
package com.example.demo.config;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;
/**
* Created by Administrator on 2018/8/15.
*/
@Configuration
@EnableCaching
public class RedisConfiguration {
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate){
return new RedisCacheManager(redisTemplate);
}
}
此处说明;@EnableCaching---开启我们的项目支持缓存
CacheManager作为一个缓存管理器,此方法,项目启动的时候,自动加载。
5.开始写点业务,进行测试啦,简单点就好
分别新建,pojo,model,service,controller包,如下所示:
- 在model包下新建User.java文件,代码如下:
package com.example.demo.model;
import javax.persistence.*;
import java.io.Serializable;
/**
* Created by Administrator on 2018/8/15.
*/
@Table(name = "user") //对应数据库中的user表
@Entity(name = "User")
public class User implements Serializable{
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "age")
private int age;
@Column(name = "name")
private String name;
@Column(name = "password")
private String password;
}
setter or getter 此处代码就不贴了,自行加上就可以了。
- 在pojo包下新建一个UserRepository.java文件,代码如下:
package com.example.demo.pojo;
import com.example.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Created by Administrator on 2018/8/15.
*/
public interface UserRepository extends JpaRepository<User, Integer> {
}
- 在service下新建一个UserService.java文件,为了简单测试,我们加一个findAll()方法,代码如下:
package com.example.demo.service;
import com.example.demo.model.User;
import com.example.demo.pojo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by Administrator on 2018/8/15.
*/
@Service
//该注解是用来开启声明的类参与缓存
@CacheConfig(cacheNames = "user")
public class UserService {
@Autowired
private UserRepository userRepository;
// 此注解配置方法的缓存参数,可自定义缓存的key以及value
@Cacheable
public List<User> findAll(){
return userRepository.findAll();
}
}
- 在controller包下新建一个UserController.java文件, 代码如下:
package com.example.demo.controller;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Created by Administrator on 2018/8/15.
* 缓存测试类
*/
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/findAll")
public List<User> findAllUser(){
return userService.findAll();
}
}
方便测试,此处直接使用@RestController注解将结果以json格式返回到页面。
基本代码,现在我们启动项目,使用http://127.0.0.1:8400/findAll 访问
到此为止,代码没有问题的,项目成功启动:
访问http://127.0.0.1:8400/findAll则看见
我们加了hibernate的sql打印配置控制台如下:
由于我们加了缓存,再次访问页面还是一样的结果,但是控制台不会再次打印:
为了验证已成功缓存,我们打开redis-dli.exe应用程序,输入keys * 查看全部缓存key
这里显示出来的key并不美观,所以实际中,我们需要自己自定义key,大家可以自行操作,只需要在RedisConfiguration.java文件中增加缓存配置。
6.总结
在SpringBoot中启用redis缓存写起来也挺简单的,只需要加几个注解即可。同时我们可以通过增加缓存配置的方式,让存储到redis中的key尽量使其美观一点,再者也是为了有良好的可读性。
博主的第一篇博客,写得有些粗略,学到与此,就做个记录,后续方便自己阅读,各位发现问题还望指出来。