微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

封装redis工具类 实现快速存取值

下面是封装好的redis工具类

保存位置

com 
D javasm 
common 
async 
cache 
CacheServtce 
JsonUtils 
Keyutils

 

1 reids工具类

这个类封装的是redis,直接使用这个类的方法向reids中存取值,设置过期时间

import lombok.requiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedistemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@requiredArgsConstructor
public class CacheService {
    /**
     * redis工具类
     */

    //获得认StringRedistemplate
    @Autowired
    private StringRedistemplate stringRedistemplate;

    //设置认事件
    public static final long defaultTime = 2;


    /**
     * 存入redis 设置认有效时间的缓存
     */
    public void setCacheWithDefaultTime(String key, String value) {
        stringRedistemplate.opsForValue().set(key, value, defaultTime, TimeUnit.MINUTES);

    }

    /**
     * 存入redis 设置自定义有效时间的缓存
     */
    public void setCacheWithCustomerTime(String key, String value, long minute) {
        stringRedistemplate.opsForValue().set(key, value, minute, TimeUnit.MINUTES);

    }

    /**
     * 设置无效间的缓存
     * 存入redis
     */
    public void setCache(String key, String value) {
        stringRedistemplate.opsForValue().set(key, value);

    }

    /**
     * 判断redis是否存在
     */
    public boolean exitsKey(String key) {
        return stringRedistemplate.hasKey(key);
    }

    /**
     * 获得redis
     */
    public String getCache(String key) {
        if (exitsKey(key)) {

            return stringRedistemplate.opsForValue().get(key);
        }
        return "";
    }

    /**
     * 清除redis
     */
    public void clearCache(String key) {
        stringRedistemplate.delete(key);
    }

}

2 Json 转换类

上面封装的redis工具类存入的key-value都是str,实际使用时

存——要将后端的各种数据类型(主要是单个obj以及各种集合)转为str

取——将value转回原来的数据类型

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionLikeType;

import java.util.List;

/**'
 * Json 转换类
 */
public class JsonUtils {

    static ObjectMapper objectMapper=new ObjectMapper();

    /**
     * 对象转字符串
     */
    public static String obj2Str(Object obj) {

        try {
            String s = objectMapper.writeValueAsstring(obj);
            return s;
        } catch (JsonProcessingException e) {
            e.printstacktrace();
        }
        return "";

    }


    /**
     * 字符串转对象
     */
    public static <T> T str2Obj(String str, Class<T> clazz) {

        try {
            T t = objectMapper.readValue(str, clazz);
            return t;
        } catch (Exception e) {
            e.printstacktrace();
        }
        return null;
    }

    /**
     * 字符串转换集合
     */

    public static <T> List<T> str2List(String str, Class<T> clazz){
        CollectionLikeType collectionLikeType = objectMapper.getTypeFactory().constructCollectionLikeType(List.class, clazz);
        try {
            List<T> list = objectMapper.readValue(str, collectionLikeType);
            return list;
        } catch (JsonProcessingException e) {
            e.printstacktrace();
        }
        return null;

    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐