SpringBoot整合redis


项目中整合redis配置

springboot整合redis缓存

maven依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

redis序列换用fastjson

  1. 设置fastjson序列化

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    package com.obox.obox.configuration;

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.parser.ParserConfig;
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.fasterxml.jackson.databind.JavaType;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.type.TypeFactory;
    import org.springframework.data.redis.serializer.RedisSerializer;
    import org.springframework.data.redis.serializer.SerializationException;
    import org.springframework.util.Assert;

    import java.nio.charset.Charset;

    public class FastJsonRedisSerializer<T> implements RedisSerializer {


    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
    private ObjectMapper objectMapper = new ObjectMapper();
    private Class<T> c ;

    static {
    ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
    }

    public FastJsonRedisSerializer(Class<T> c){

    super();
    this.c = c ;
    }

    @Override
    public byte[] serialize(Object o) throws SerializationException {
    return JSON.toJSONString(o, SerializerFeature.WriteClassName).getBytes();
    }

    @Override
    public Object 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,c) ;

    }

    public void setObjectMapper(ObjectMapper objectMapper) {
    Assert.notNull(objectMapper, "'objectMapper' must not be null");
    this.objectMapper = objectMapper;
    }

    protected JavaType getJavaType(Class<?> clazz) {
    return TypeFactory.defaultInstance().constructType(clazz);
    }

    }
  2. redis中注册配置fatsjosn序列化工具

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    package com.obox.obox.configuration;


    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.cache.CacheManager;
    import org.springframework.cache.annotation.CachingConfigurationSelector;
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.RedisConnectionFailureException;
    import org.springframework.data.redis.cache.RedisCacheConfiguration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.RedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;

    import java.time.Duration;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;

    @Configuration
    @EnableCaching
    public class RedisConfig extends CachingConfigurerSupport {

    /*@Bean(name="redisTemplate")
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){

    RedisTemplate<String,Object> template = new RedisTemplate<>() ;
    // 配置连接工厂
    template.setConnectionFactory(factory);

    FastJsonRedisSerializer serializer = new FastJsonRedisSerializer(Object.class);

    ObjectMapper mapper = new ObjectMapper() ;
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY) ;
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    serializer.setObjectMapper(mapper);

    template.setValueSerializer(serializer);
    template.setKeySerializer(new StringRedisSerializer());
    template.afterPropertiesSet();

    return template ;


    }*/

    @Bean
    public RedisSerializer fastJson2JsonRedisSerializer() {
    return new FastJsonRedisSerializer(Object.class);
    }

    @Bean
    public RedisTemplate initRedisTemplate(RedisConnectionFactory redisConnectionFactory, RedisSerializer fastJson2JsonRedisSerializer) throws Exception {
    RedisTemplate redisTemplate = new RedisTemplate();
    redisTemplate.setConnectionFactory(redisConnectionFactory);
    redisTemplate.setValueSerializer(fastJson2JsonRedisSerializer);
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.afterPropertiesSet();
    return redisTemplate;
    }

    }

redis工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

package com.obox.obox.Utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisUtil {


@Autowired
private RedisTemplate<String,Object> redisTemplate ;



public RedisUtil(RedisTemplate<String,Object> redis){

redisTemplate = redis ;
}

/**
* 是否存在key值
* @param key
* @return
*/

public boolean HasKey(String key){

if (redisTemplate.hasKey(key)){

return true ;
}else {

return false ;
}
}

/**
*
* 添加缓存
* @param key
* @param value
* @return
*/
public boolean AddItem(String key ,Object value){

if (value == null){

return false ;
}else {

redisTemplate.opsForValue().set(key,value);
return true ;
}
}

/**
*
* 删除缓存
* @param key
* @return
*/
public boolean DelItem(String key){

if (redisTemplate.hasKey(key)){

redisTemplate.delete(key) ;
return true ;
}else {

return false ;
}
}

/**
*
* 更新缓存
* @param key
* @param value
* @return
*/
public boolean UpdateItem(String key,Object value){

if (this.DelItem(key) && this.AddItem(key,value) ){

return true ;
}else
{
return false ;
}

}

/**
*
* 获取缓存
* @param key
* @return
*/
public Object GetItem (String key){

if (this.HasKey(key)){

return redisTemplate.opsForValue().get(key) ;
}else {

return null ;
}

}
}