1.首先引入依赖,在pom.xml中加入以下代码
org.springframework.boot
spring-boot-starter-data-redis
2.0.3.RELEASE
org.apache.commons
commons-pool2
2.添加yml配置:
spring:
redis:
host: localhost
port: 6379
password: 123456
timeout: 1000ms # 连接超时时间
database: 12 #数据库索引
lettuce:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 1000
# 连接池中的最大空闲连接
max-idle: 80
# 连接池中的最小空闲连接
min-idle: 10
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: 100ms
3.然后创建一个配置类,设置redis的序列化方式,不然无法序列化对象等
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
RedisTemplate template = new RedisTemplate<>();
// 配置连接工厂
template.setConnectionFactory(factory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
Jackson2JsonRedisSerializer jacksonSerial = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSerial.setObjectMapper(om);
// 值采用json序列化
template.setValueSerializer(jacksonSerial);
//使用StringRedisSerializer来序列化和反序列化redis的key值,这样存储不会出现一些符号前缀
template.setKeySerializer(new StringRedisSerializer());
// 设置hash key 和value序列化模式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jacksonSerial);
template.afterPropertiesSet();
return template;
}
}
4.在Service或者要使用的地方进行注入,
@Autowired
private RabbitTemplate rabbitTemplate;
之后就可以使用了
//插入redis中
redisTemplate.opsForList().rightPush("test", "123");
更多的数据类型操作可参考枣面包的博客,里面对String,List,Hash,Set,ZSet的操作有详细介绍
以上是手动的操作,也可以使用注解的方式,可参考nfcm的博客对注解的使用有初步了解
可以使用Spring 的aop自定义注解实现自定义缓存过期时间
1.定义一个注解
import java.lang.annotation.*;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyCache {
//redis的key
String key();
//返回类型
Class type();
//默认缓存时间是一天
long expire() default 60 ;
}
2.添加注解的拦截逻辑,在这里添加缓存逻辑
import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Aspect
@Component
public class AopCacheHandle {
@Autowired
private RedisTemplate redisTemplate;
@Pointcut(value = "@annotation(com.common.cache.MyCache)")
public void pointcut() {
}
@Around(value = "pointcut() && @annotation(cache)")
public Object around(ProceedingJoinPoint point, MyCache cache) {
//根据参数生成key
final String key = parseKey(cache.key(), point.getArgs());
String value = null;
try {
//从redis中获取缓存
value = (String) redisTemplate.opsForValue().get(key);
} catch (Exception ex) {
ex.printStackTrace();
}
if (null != value) {
return JSON.parseObject(value, cache.type());
}
try {
//等待返回结果
Object proceed = point.proceed();
if (proceed != null) {
try {
//设置缓存
redisTemplate.opsForValue().set(key, JSON.toJSONString(proceed), cache.expire(), TimeUnit.SECONDS);
} catch (Exception ex) {
ex.printStackTrace();
}
}
return proceed;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
/**
* redis的键值生成策略
*
* @param key key前缀
* @param args 方法参数列表
*/
private String parseKey(String key, Object[] args) {
//真正的key
StringBuilder sb = new StringBuilder(key + "-");
for (int i = 0; i < args.length; i++) {
sb.append(args[i].toString() + "-");
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
}
参考博客:
https://blog.csdn.net/weixin_34277853/article/details/87221807