Redis相关配置

这是Redis的相关配置

package com.oscar.springbootbackenderp.core.redis;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.oscar.springbootbackenderp.configuration.CustomObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author oscar
 * @description:
 * @date 2022/8/29 21:45
 */

@Slf4j
@Component
public class RedisUtils {


        @Autowired
        RedisTemplate redisTemplate;

        @Autowired
    private CustomObjectMapper objectMapper;

    //保存字符串类型的数据
    public void set(String key, String value, Long expire) {
        redisTemplate.opsForValue().set(key, value,expire);
    }

    public String get(String key) {
        return     (String) redisTemplate.opsForValue().get(key);
    }

        public long remove(String... key) {
           return redisTemplate.delete( Arrays.asList(key));
    }


    @Autowired(required = false)
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisSerializer stringSerializer = new StringRedisSerializer();
        //使用StringRedisSerializer来序列化和反序列化redis的ke
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(stringSerializer);
        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
        redisTemplate.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        this.redisTemplate = redisTemplate;
    }


    //将对象以序列化的方式存放在redis中
    public void setObject(String key, Object value, Long expire)  {
            //序列化为Json
        String jsonValue = null;
        try {
            jsonValue = objectMapper.writeValueAsString(value);
        } catch (JsonProcessingException e) {
            log.error("redis execution error!", e);
            e.printStackTrace();
        }
        redisTemplate.opsForValue().set(key,  jsonValue,Math.toIntExact(expire));
    }


        //读取redis数据,反序列化为json
    public <T> T getObject(String key, TypeReference<T> typeReference) throws IOException {
        T object = null;
        String jsonValue = (String) redisTemplate.opsForValue().get(key);
        //序列化为Json
        try {
            object = objectMapper.readValue(jsonValue, typeReference);
        } catch (JsonProcessingException e) {
            log.error("redis execution error!", e);
            e.printStackTrace();
        }
        return object;
    }

    public Boolean expire(String key, Long expire) {

            //从redis中获取
       return redisTemplate.expire(key, expire, TimeUnit.SECONDS);

    }

        //查询特定的key
    public Set<String> getKeys(String pattern) {
        Set<String> keys = null;
            //从redis中获取
            keys = redisTemplate.keys(pattern);

        return keys;
    }



}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值