pom.xml中加入:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
application.properties或application.yml中:
#Redis 数据库索引(默认为 0)
spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
#Redis 服务器连接密码(默认为空)
spring.redis.password=
#springboot 2.0 redis默认客户端已换成lettuce
#连接池最大连接数(使用负值表示没有限制) 默认 8
spring.redis.lettuce.pool.max-active=8
#连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
spring.redis.lettuce.pool.max-wait=-1
#连接池中的最大空闲连接 默认 8
spring.redis.lettuce.pool.max-idle=8
#连接池中的最小空闲连接 默认 0
spring.redis.lettuce.pool.min-idle=0
spring.redis.timeout=5000
使用:
@Autowired
public RedisTemplate redisTemplate;
存入:
redisTemplate.opsForValue().set(“onlineusername”, userMap, expireTime, TimeUnit.MINUTES);
获取:
HashMap<String, SysUserVo> userMap = (HashMap<String, SysUserVo>) redisTemplate.opsForValue().get(“onlineusername”);