springboot中redis的使用
在目前的软件开发中,redis的使用非常常见,redis一款内存高速缓存数据库,他的特点在于 1 、据持久化的,2 性能快 ,3 Key-Value 型DB,下面以部分代码来看redis如何在springboot中使用
1 下载windows版本redis
为了方便学习,我们先来下载一个windows版本的redis,虽然Redis项目不正式支持Windows。 但是,Microsoft开放技术小组开发和维护这个Windows端口针对Win64。下载地址如下 https://github.com/MicrosoftArchive/redis/releases
下载完直接解压,然后通过cmd切换到redis解压的目录,通过redis-server.exe redis.windows.conf启动即可,如果需要修改redis相关配置,修改redis.windows.conf文件即可,访问客户端同样命令行模式切换到redis解压的文件夹,执行redis-cli.exe即可
2 springboot集成redis
spring集成redis只需在pom.xml中添加如下依赖即可,为了方便起见,我们采用1.5.9版本的redis jar包和1.5.8版本的springboot,因为spring2.0以后的版本默认集成的redis2.0版本,redis连接池使用的lettuce,并且redis配置类也会有所不同。
<?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>redisdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>redisdemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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-data-redis</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后在application.yml配置redis相关连接信息
# Redis
spring:
redis:
host: 127.0.0.1
port: 6379
pool:
max-active: 40
max-wait: -1
max-idle: 16
min-idle: 0
timeout: 0
password:
# 连接的数据库 redis默认启动16个数据库
database: 0
注意,若database选择非0的话,在命令行查看redis数据时需切换数据库,切换命令(如切换到数据库2):select 2
新建redis配置类
配置了redis作为缓存,以及初始化redis操作类RedisTemplate实例。redis相关操作都是依赖RedisTemplate来实现的。
package com.example.redisdemo.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
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.connection.RedisConnectionFactory;
import o