1.pom.xml引入redis
<!--redis开始-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--redis结束-->
2.yml配置
redis:
port: 6379
host: 127.0.0.1
database: 5
3.redis config
package cn.stylefeng.guns.redis;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author zhoujie
* @version JDK1.8.0_171
* @date on 2020/02/29 19:25
* @description V1.0
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
//小范围白名单
// ParserConfig.getGlobalInstance().addAccept("com.andy.framdemo.");
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用FastJSON的序列化方式
template.setValueSerializer(fastJsonRedisSerializer);
// hash的value序列化方式采用FastJSON的序列化方式
template.setHashValueSerializer(fastJsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
4.fastjson序列化
package cn.stylefeng.guns.redis;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import java.nio.charset.Charset;
/**
*
* @version JDK1.8.0_171
* @description 序列化器
*/
public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class<T> clazz;
public FastJsonRedisSerializer(Class<T> clazz) {
super();
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName,SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullListAsEmpty).getBytes(DEFAULT_CHARSET);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length <= 0) {
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return (T) JSON.parseObject(str, clazz);
}
}
后面可以使用redistemplate及@cacheable等注解做缓存
再附上windows的redis连接工具链接: https://pan.baidu.com/s/13saHZRdgbfFAGf8dYt8eKw 提取码: b3te
本文详细介绍了如何在Spring Boot项目中引入并配置Redis,包括pom.xml依赖添加、yml配置文件设置、自定义RedisConfig类以及使用FastJson进行序列化操作。通过本教程,读者将学会使用RedisTemplate进行数据操作,并了解如何利用@Cacheable等注解实现缓存功能。
21万+

被折叠的 条评论
为什么被折叠?



