Redis简介
Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
Redis支持数据的备份,即master-slave模式的数据备份。
第一步:
pom.xml文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
第二步:
application.properties文件:
spring.redis.database=0
# redis server host
spring.redis.host=127.0.0.1
# redis password
spring.redis.password=
# connection port
spring.redis.port=6379
# time out
spring.redis.timeout=10000
# 配置缓存过期时间
spring.redis.expiretime=1800
# 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
# set Key prefix.
# spring.cache.redis.key-prefix=dev
# Entry expiration in milliseconds. By default the entries never expire.
spring.cache.redis.time-to-live=1d
# Whether to use the key prefix when writing to Redis.
spring.cache.redis.use-key-prefix=true
第三步:
编写RedisConfig文件:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org