欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot 集成Redis操作方法

程序员文章站 2022-05-29 07:57:46
...

一、service层集成redis操作方法

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

public interface RedisServer {
    <T> void put(String key, T obj);
    <T> void put(String key, T obj, int timeout);
    <T> void put(String key, T obj, int timeout, TimeUnit unit);

    <T> T get(String key, Class<T> cls);

    <E,T extends Collection<E>> T get(String key, Class<E> cls, Class<T> collectionCls);
    <T> T putIfAbsent(String key, Class<T> cls, Supplier<T> supplier);
    <T> T putIfAbsent(String key, Class<T> cls, Supplier<T> supplier, int timeout);

    <E,T extends Collection<E>> T putIfAbsent(String key, Class<E> cls, Class<T> collectionCls, Supplier<T> supplier);

    boolean exists(String key);
    void del(String key);
    boolean expire(String key, long timeout, TimeUnit unit);
    boolean expire(String key, long timeout);

    void put(String key, String value);
    void put(String key, String value, int timeout);
    void put(String key, String value, int timeout, TimeUnit unit);
    String get(String key);

    void putHash(String key, Map<Object, Object> m);
    Map<Object, Object> getHash(String key);
}

二、service层实现

import com.yh.business.framework.base.util.JsonUtils;
import com.yh.business.framework.common.redis.RedisServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;


@Service("redisServer")
public class RedisServerImpl implements RedisServer {

    @Autowired
    private StringRedisTemplate redisTemplate;

    public <T> void put(String key, T obj) {
        redisTemplate.opsForValue().set(key, JsonUtils.toJson(obj));
    }

    public <T> void put(String key, T obj, int timeout) {
        put(key,obj,timeout,TimeUnit.MINUTES);
    }

    public <T> void put(String key, T obj, int timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, JsonUtils.toJson(obj),timeout,unit);
    }

    public <T> T get(String key, Class<T> cls) {
        return JsonUtils.fromJson(JsonUtils.toJson(redisTemplate.opsForValue().get(key)), cls);
    }

    public <E, T extends Collection<E>> T get(String key, Class<E> cls, Class<T> collectionCls) {
        return JsonUtils.fromJson(JsonUtils.toJson(redisTemplate.opsForValue().get(key)), cls, collectionCls);
    }

    public <T> T putIfAbsent(String key, Class<T> cls, Supplier<T> supplier) {
        T t=get(key,cls);
        if(null==t){
            t=supplier.get();
            if(null!=t)
                put(key,t);
        }
        return t;
    }

    public <T> T putIfAbsent(String key, Class<T> cls, Supplier<T> supplier, int timeout) {
        T t=get(key,cls);
        if(null==t){
            t=supplier.get();
            if(null!=t)
                put(key,t,timeout);
        }
        return t;
    }

    public <E, T extends Collection<E>> T putIfAbsent(String key, Class<E> cls, Class<T> collectionCls,
                                                      Supplier<T> supplier) {
        T t=get(key,cls,collectionCls);
        if(null==t || t.isEmpty()){
            t=supplier.get();
            if(null!=t && t.size()>0)
                put(key,t);
        }
        return t;
    }

    public boolean exists(String key) {
        return redisTemplate.hasKey(key);
    }

    public void del(String key) {
        redisTemplate.delete(key);
    }

    public boolean expire(String key, long timeout, TimeUnit unit) {
        return redisTemplate.expire(key, timeout, unit);
    }

    public boolean expire(String key, long timeout) {
        return redisTemplate.expire(key, timeout, TimeUnit.MINUTES);
    }

    public void put(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public void put(String key, String value, int timeout) {
        put(key,value,timeout,TimeUnit.MINUTES);
    }

    public void put(String key, String value, int timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

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

    public void putHash(String key, Map<Object,Object> m) {
        redisTemplate.opsForHash().putAll(key, m);
    }

    public Map<Object, Object> getHash(String key) {
        try{
            return redisTemplate.opsForHash().entries(key);
        }catch(Exception e){
            return null;
        }
    }
}

三、测试
1、用put方法直接添加键值对,get方法去值,输出“hello redis!”。
2、del方法删除键值对,删除完后输出为 null。

相关标签: 后端技术