重点是添加依赖和配置,redis具体更详情的使用见redis相关章节
- 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 配置
#################redis基础配置#################
spring.redis.database=3
spring.redis.host=127.0.0.1
spring.redis.password=112233
spring.redis.port=12388
# 连接超时时间 单位 ms(毫秒)
spring.redis.timeout=3000
#################redis线程池设置#################
# 连接池中的最大空闲连接,默认值也是8。
spring.redis.pool.max-idle=500
#连接池中的最小空闲连接,默认值也是0。
spring.redis.pool.min-idle=50
# 连接池最大连接数,如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
spring.redis.pool.max-active=2000
# 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
spring.redis.pool.max-wait=1000
#################redis哨兵设置#################
# Redis服务器master的名字
#spring.redis.sentinel.master=master8026
# redis-sentinel的配置地址和端口
#spring.redis.sentinel.nodes=xxx
- 代码使用
package com.controller;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.model.User;
@Controller
public class RedisController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping("/redisString")
public String redisString(){
//设置缓存,建议每个键都设置过期时间
stringRedisTemplate.opsForValue().set("test", "test", 10, TimeUnit.SECONDS);
//获取缓存值
String value = stringRedisTemplate.opsForValue().get("test");
//删除某个键
stringRedisTemplate.delete("test");
//操作set
stringRedisTemplate.opsForSet().add("testSet", "1","2");
stringRedisTemplate.opsForSet().add("testSet", "5");
Set<String> members = stringRedisTemplate.opsForSet().members("testSet");//获取set内的所有值
stringRedisTemplate.opsForSet().remove("testSet", "1","2");//移除set内的多个对象
//操作list
stringRedisTemplate.opsForList().rightPush("testList", "1");
stringRedisTemplate.opsForList().rightPush("testList", "3");
stringRedisTemplate.opsForList().rightPush("testList", "4");
List<String> list = stringRedisTemplate.opsForList().range("testList", 0, -1);//获取list内的所有元素
return "home";
}
@RequestMapping("/redisObject")
public String redisObject(){
//设置缓存,建议每个键都设置过期时间
redisTemplate.opsForValue().set("test2", "test2", 10, TimeUnit.SECONDS);
//获取缓存值
String value = (String) redisTemplate.opsForValue().get("test2");
//删除某个键
redisTemplate.delete("test2");
User user = new User();
user.setName("gary");
redisTemplate.opsForValue().set("user",user);
User user2 = (User) redisTemplate.opsForValue().get("user");
return "home";
}
public void zadd(String key, double score, String value){
redisTemplate.execute(new RedisCallback() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
connection.zAdd(key.getBytes(), score, value.getBytes());
return 1L;
}
});
}
}