1、maven依赖
org.springframework.boot
spring-boot-starter-data-redis
2、配置文件
redis:
database: 0
host: 127.0.0.1
port: 6379
timeout: 6000ms # 连接超时时长(毫秒)
完成以上两步即可使用了。
3、高级配置 RedisConfig.java
package com.hsshy.beam.common.config;
import org.springframework.beans.factory.annotation.Autowired;
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.*;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Redis配置
*
* @author hs
* @date 2019-12-07
* redis配置升级
*/
@Configuration
public class RedisConfig {
@Autowired
private RedisConnectionFactory factory;
@Bean
public RedisTemplate redisTemplate() {
// 创建一个模板类
RedisTemplate redisTemplate = new RedisTemplate<>();
// 设置key的序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
// 设置value的序列化器
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
Jackson2JsonRedisSerializer jacksonSeial = 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);
jacksonSeial.setObjectMapper(om);
redisTemplate.setValueSerializer(jacksonSeial);
// 将刚才的redis连接工厂设置到模板类中
//spring默认帮我们读取application.properties文件并且注册了一个factorybean
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
@Bean
public HashOperations hashOperations(RedisTemplate redisTemplate) {
return redisTemplate.opsForHash();
}
@Bean
public ValueOperations valueOperations(RedisTemplate redisTemplate) {
return redisTemplate.opsForValue();
}
@Bean
public ListOperations listOperations(RedisTemplate redisTemplate) {
return redisTemplate.opsForList();
}
@Bean
public SetOperations setOperations(RedisTemplate redisTemplate) {
return redisTemplate.opsForSet();
}
@Bean
public ZSetOperations zSetOperations(RedisTemplate redisTemplate) {
return redisTemplate.opsForZSet();
}
}
4、redis工具类
package com.gizhi.beam.core.utils;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
@Resource(name="redisTemplate")
private ValueOperations valueOperations;
@Resource(name="redisTemplate")
private HashOperations hashOperations;
@Resource(name="redisTemplate")
private ListOperations listOperations;
@Resource(name="redisTemplate")
private SetOperations setOperations;
@Resource(name="redisTemplate")
private ZSetOperations zSetOperations;
/** 默认过期时长,单位:秒 */
public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
/** 不设置过期时长 */
public final static long NOT_EXPIRE = -1;
public void set(String key, Object value, long expire){
valueOperations.set(key, toJson(value));
if(expire != NOT_EXPIRE){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
}
public void set(String key, Object value){
set(key, value, DEFAULT_EXPIRE);
}
public T get(String key, Class clazz, long expire) {
String value = valueOperations.get(key);
if(expire != NOT_EXPIRE){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return value == null ? null : fromJson(value, clazz);
}
public T get(String key, Class clazz) {
return get(key, clazz, NOT_EXPIRE);
}
public String get(String key, long expire) {
String value = valueOperations.get(key);
if(expire != NOT_EXPIRE){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return value;
}
public String get(String key) {
return get(key, NOT_EXPIRE);
}
public void delete(String key) {
redisTemplate.delete(key);
}
/**
* Object转成JSON数据
*/
private String toJson(Object object){
if(object instanceof Integer || object instanceof Long || object instanceof Float ||
object instanceof Double || object instanceof Boolean || object instanceof String){
return String.valueOf(object);
}
return JSON.toJSONString(object);
}
/**
* JSON数据,转成Object
*/
private T fromJson(String json, Class clazz){
return JSON.parseObject(json, clazz);
}
/**
* listOperations使用
*/
public void lset(String key, List list, long expire){
listOperations.leftPush(key,list);
if(expire != NOT_EXPIRE){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
}
public void lset(String key, List list){
lset(key, list, DEFAULT_EXPIRE);
}
public List lget(String key, long expire) {
List list = (List)listOperations.leftPop(key);
if(expire != NOT_EXPIRE){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return list;
}
public List lget(String key) {
return lget(key, NOT_EXPIRE);
}
/**
* hashOperations的使用
*/
public void hset(String key, Map map, long expire){
hashOperations.putAll(key,map);
if(expire != NOT_EXPIRE){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
}
public void hset(String key,Map map){
hset(key, map, DEFAULT_EXPIRE);
}
/**
* 获取整个map
*/
public Map hget(String key, long expire) {
Map map = hashOperations.entries(key);
if(expire != NOT_EXPIRE){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return map;
}
public Map hget(String key) {
return hget(key, NOT_EXPIRE);
}
/**
* 获取map里的一个值
*/
public Object hget(String key,String mkey, long expire) {
Object value = hashOperations.get(key,mkey);
if(expire != NOT_EXPIRE){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return value;
}
public Object hget(String key,String mkey) {
return hget(key,mkey,NOT_EXPIRE);
}
/**
* 获取map里的所有值
*/
public List hgetv(String key,long expire) {
List list = hashOperations.values(key);
if(expire != NOT_EXPIRE){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return list;
}
public List hgetv(String key) {
return hgetv(key,NOT_EXPIRE);
}
/**
* 获取map里的所有key
*/
public Set hgetk(String key,long expire) {
Set list = hashOperations.keys(key);
if(expire != NOT_EXPIRE){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return list;
}
public Set hgetk(String key) {
return hgetk(key,NOT_EXPIRE);
}
}
6、测试
redisUtil.set("site_rule_config",siteRuleConfig);
SiteRuleConfig siteRuleConfig= redisUtil.get("site_rule_config",SiteRuleConfig.class);
List memberList = new ArrayList<>();
Member member1 = new Member();
member1.setNickName("好好");
Member member2 = new Member();
member2.setNickName("萨达");
memberList.add(member1);
memberList.add(member2);
redisUtil.lset("test",memberList,10);
System.out.println(redisUtil.lget("test"));
Map map=new HashMap();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
map.put("key4","value4");
map.put("key5","value5");
redisUtil.hset("map",map);
//整个map
System.out.println(redisUtil.hget("map"));
//map里的某个值
System.out.println(redisUtil.hget("map","key2"));
//map里的所有值
System.out.println(redisUtil.hgetv("map"));
//map里的所有key
System.out.println(redisUtil.hgetk("map"));