实战步骤:
1、官网:https://docs.spring.io/spring-boot/docs/2.1.8.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-redis
集群文档:https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#cluster
2、springboot整合redis相关依赖引入
org.springframework.boot
spring-boot-starter-data-redis
3、配置文件配置在application.properties
#=========redis基础配置=========
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
# 连接超时时间 单位 ms(毫秒)
spring.redis.timeout=3000
#=========redis线程池设置=========
# 连接池中的最大空闲连接,默认值也是8。
spring.redis.pool.max-idle=200
#连接池中的最小空闲连接,默认值也是0。
spring.redis.pool.min-idle=200
# 如果赋值为-1,则表示不限制;pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
spring.redis.pool.max-active=2000
# 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时
spring.redis.pool.max-wait=1000
4、常见StirngRedisTemplate类和缓存实操(使用自动注入)
1、注入模板
@Autowired
private StirngRedisTemplate strTplRedis
2、类型String,List,Hash,Set,ZSet
对应的方法分别是opsForValue()、opsForList()、opsForHash()、opsForSet()、opsForZSet()
工程结构图
简单的RedisController类
@RestController
@RequestMapping("/redis/my")
public class RedisController {
/**
* 注入redis字符串模板类
*/
@Autowired
private StringRedisTemplate template;
@GetMapping("save")
public Object save(){
//ValueOperations
//保存数据
template.opsForValue().set("names","actor or actress");
return JsonData.buildSuccess();
}
@GetMapping("get")
public Object get(){
//获得数据
Object data =template.opsForValue().get("names");
return JsonData.buildSuccess(data);
}
}
效果1:
简单的RedisController2类
@RestController
@RequestMapping("/redis2/my")
public class Redis2Controller {
/**
* 注入redis字符串模板类
*/
@Autowired
private RedisClient template;
@GetMapping("save")
public Object save(){
//保存数据
template.set("names0","comedy");
return JsonData.buildSuccess();
}
@GetMapping("get")
public Object get(){
//获得数据
Object data =template.get("names0");
return JsonData.buildSuccess(data);
}
@GetMapping("adduser")
public Object saveUser(){
//1.对象封装
User user = new User(1,"baby","狒狒","000001",1 ,new Date());
//2.将对象转换成字符串
String str= JsonUtils.obj2String(user);
//3.保存数据
boolean flag=template.set("user",str);
//4.返回
return JsonData.buildSuccess(flag);
}
@GetMapping("getuser")
public Object getUser(){
//1.获得数据
String data =template.get("user");
//2.将字符串转换成对象
User user= JsonUtils.string2Obj(data,User.class);
//3.返回数据
return JsonData.buildSuccess(user);
}
}
JsonUtils公共类:
package net.hlx.myspringboot.redis_demo.utils;
import java.io.IOException;
import org.springframework.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* JSON工具类
*/
public class JsonUtils {
//实例化对象
private static ObjectMapper objectMapper = new ObjectMapper();
//对象转字符串
public static String obj2String(T obj){
if (obj == null){
return null;
}
try {
return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//字符串转对象
public static T string2Obj(String str,Class clazz){
if (StringUtils.isEmpty(str) || clazz == null){
return null;
}
try {
return clazz.equals(String.class)? (T) str :objectMapper.readValue(str,clazz);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
RedisClient 封装成公共类
package net.hlx.myspringboot.redis_demo.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
/**
* 功能描述:redis工具类
*/
@Component
public class RedisClient {
@Autowired
private StringRedisTemplate redisTpl; //redis模板类
/**
* 功能描述:设置key-value到redis中
* @param key
* @param value
* @return
*/
public boolean set(String key ,String value){
try{
redisTpl.opsForValue().set(key, value);
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
/**
* 功能描述:通过key获取缓存里面的值
* @param key
* @return
*/
public String get(String key){
return redisTpl.opsForValue().get(key);
}
}
效果2:
可以做个测试类测试:
@RunWith(SpringRunner.class) //底层用junit SpringJUnit4ClassRunner
@SpringBootTest //启动整个springboot工程
public class RedisDemoApplicationTests {
@Autowired
private RedisClient redisClient;
@Test
public void contextLoads() {
User user = new User("your","政府","1234456",1 ,new Date());
String str= JsonUtils.obj2String(user);
//保存为字符串哦
boolean flag= redisClient.set("user1",str);
System.out.println(flag);
}
}
效果: