springboot 集成redis

 

1、pom

 

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

 

2、yml

 

spring:
  redis:
    host: 10.233.67.73
    port: 6379
    password: 123445
    database: 4

3、 config

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        redisTemplate.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

4、redisUtils

@Component
@Slf4j
@AllArgsConstructor
public class RedisUtils {

    private final RedisTemplate<String, String> redisTemplate;

    /**
     * 获取hash中单个key的值
     *
     * @param key
     * @param hashKey
     * @return
     */
    public String hGet(String key, String hashKey) {
        Object object = redisTemplate.opsForHash().get(key, hashKey);
        return object == null ? StringUtils.EMPTY : object.toString();
    }

    /**
     * 添加或者更新hash的值
     *
     * @param key
     * @param hashKey
     * @param value
     */
    public void hPut(String key, String hashKey, String value) {
        redisTemplate.opsForHash().put(key, hashKey, value);
    }

    /**
     * 添加全部的hash值
     *
     * @param key
     * @param map
     */
    public void hPutAll(String key, Map<String, String> map) {
        redisTemplate.opsForHash().putAll(key, map);
    }

    /**
     * 删除hash中的键值对
     *
     * @param key
     * @param hashKey
     */
    public void hDel(String key, String... hashKey) {
        redisTemplate.opsForHash().delete(key, hashKey);
    }

    /**
     * 获取hash表中字段的数量
     *
     * @param key
     * @return
     */
    public Long hSize(String key) {
        return redisTemplate.opsForHash().size(key);
    }

    /**
     * 查询全部的hash内容
     *
     * @param key
     * @return
     */
    public Map<String, String> hGetAll(String key) {
        return redisTemplate.execute((RedisCallback<Map<String, String>>) con -> {
            Map<byte[], byte[]> result = con.hGetAll(key.getBytes());
            if (CollUtil.isEmpty(result)) {
                return new HashMap<>();
            }
            Map<String, String> map = new HashMap<>(result.size());
            for (Map.Entry<byte[], byte[]> entry : result.entrySet()) {
                map.put(new String(entry.getKey()), new String(entry.getValue()));
            }
            return map;
        });
    }

    /**
     * 获取部分的hash内容
     *
     * @param key
     * @param hashKeySet
     * @return
     */
    public Map<String, String> hGetList(String key, Set<String> hashKeySet) {
        List<String> list = redisTemplate.<String, String>opsForHash().multiGet(key, hashKeySet);
        Map<String, String> map = new HashMap<>(hashKeySet.size());
        int index = Constant.DEFAULT_INDEX;
        for (String item : hashKeySet) {
            if (list.get(index) == null) {
                index++;
                continue;
            }
            map.put(item, list.get(index));
            index++;
        }
        return map;
    }

 

posted @ 2023-03-30 16:10  介寒食  阅读(74)  评论(0编辑  收藏  举报