Redis常用操作工具类---包含单例和集群模式

  • 接口:
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: lcx
 * @Date: Created at 2018/8/18 13:40.
 * @Description:
 */
public interface JedisCommonDao {
    /**
     * 序列化方式从redis中获取对象
     *
     * @param key
     * @return
     */
    <T> T getObject(String key, Class<T> clazz);

    /**
     * 序列化方式向redis中存入对象(1小时过期)
     *
     * @param key
     * @param obj
     * @return
     */
    String putObject(String key, Object obj);
    /**
     * 序列化方式向redis中存入对象并设置过期时间(秒)
     *
     * @param key
     * @param obj
     * @return
     */
    String putObject(String key, Object obj, int expire) ;


    /**
     * 指定的键值
     *
     * @param key
     * @return
     */
    public Long del(String key);

    /**
     * JSON形式从redis中获取对象数组
     *
     * @param key
     * @return
     */
    <T> List getList(String key, Class<T> clazz);

    /**
     * JSON形式向redis中存入对象
     *
     * @param key
     * @param obj
     * @return
     */
    String put(String key, Object obj) ;

    /**
     * 从Redis中取值
     *
     * @param key
     * @return
     */
    String getValue(String key) ;


    /**
     * 往redis里存入值,如果成功则返回1,失败返回0
     * @param key
     * @return
     */
    Long setNxValue(String key);

    /**
     * 往redis Set 键里面添加成员
     * @param key 键
     * @param expire 时间 单位s
     * @param members
     * @return
     */
    Long addSetValue(String key, int expire, String... members);

    /**
     * redis Set 键里面删除成员
     * @param key
     * @return
     */
    Long moveSetValue(String key, String... members) ;

    /**
     * 获取redis Set 键里面所有成员
     * @param key
     * @return
     */
    Set<String> getSetValues(String key);

    /**
     * 存入map
     * @param key
     * @param map
     * @return
     */
    String putMapValues(String key, Map<byte[], byte[]> map);

    /**
     * 根据key 获取map中数据
     * @param key
     * @param filed
     * @param clazz
     * @param <T>
     * @return
     */
    <T> T getMapObject(String key, String filed, Class<T> clazz);

    /**
     * 根据key获取map
     * @param key
     * @return
     */
    Map<byte[],byte[]> getAllMap(String key);
    /**
     * 往redis map中存值,如果已经存在就跳过
     * @param key
     * @param filed
     * @param obj
     * @return
     */
    Long putMapObject(String key, String filed, Object obj);

    <T> void setMap(String key, Map<String, T> map);

    <T> Map<String,T> getMap(String key);

    Long putMapString(String key, String filed, String s, int expire);

    Long incr(String key);

    String setString(String key, String value);

}

  • 工具类
package com.redis;

import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.runtime.RuntimeSchema;

import java.io.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 序列化工具类
 * <p>
 * Created by lcx on 2018/04/19.
 */
public class ProtostuffUtil {

    private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>();

    private static <T> Schema<T> getSchema(Class<T> clazz) {
        Schema<T> schema = (Schema<T>) cachedSchema.get(clazz);
        if (schema == null) {
            schema = RuntimeSchema.getSchema(clazz);
            if (schema != null) {
                cachedSchema.put(clazz, schema);
            }
        }
        return schema;
    }

    /**
     * 序列化
     *
     * @param obj
     * @return
     */
    public static <T> byte[] serializer(T obj) {
        @SuppressWarnings("unchecked")
        Class<T> clazz = (Class<T>) obj.getClass();
        LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
        try {
            Schema<T> schema = getSchema(clazz);
            return ProtostuffIOUtil.toByteArray(obj, schema, buffer);
        } catch (Exception e) {
            throw new IllegalStateException(e.getMessage(), e);
        } finally {
            buffer.clear();
        }
    }

    /**
     * 反序列化
     *
     * @param data
     * @param clazz
     * @return
     */
    public static <T> T deserializer(byte[] data, Class<T> clazz) {
        try {
            T obj = clazz.newInstance();
            Schema<T> schema = getSchema(clazz);
            ProtostuffIOUtil.mergeFrom(data, obj, schema);
            return obj;
        } catch (Exception e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }

    public static byte[] serialize(Object value) {
        if (value == null) {
            throw new NullPointerException("Can't serialize null");
        }
        byte[] rv=null;
        ByteArrayOutputStream bos = null;
        ObjectOutputStream os = null;
        try {
            bos = new ByteArrayOutputStream();
            os = new ObjectOutputStream(bos);
            os.writeObject(value);
            os.close();
            bos.close();
            rv = bos.toByteArray();
        } catch (IOException e) {
            throw new IllegalArgumentException("Non-serializable object", e);
        } finally {
            try {
                if(os!=null)os.close();
                if(bos!=null)bos.close();
            }catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return rv;
    }

    public static Object deserialize(byte[] in) {
        Object rv=null;
        ByteArrayInputStream bis = null;
        ObjectInputStream is = null;
        try {
            if(in != null) {
                bis=new ByteArrayInputStream(in);
                is=new ObjectInputStream(bis);
                rv=is.readObject();
                is.close();
                bis.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(is!=null)is.close();
                if(bis!=null)bis.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return rv;
    }

}

  • 单例实现:

import com.alibaba.fastjson.JSON;
import com.redis.JedisCommonDao;
import com.redis.ProtostuffUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Redis 通用DAO
 * Created by lcx on 2018/4/20.
 */
@Service
public class SingleRedisDao implements JedisCommonDao {

    /*Redis超时时间*/
    public static final int TIMEOUT_ONE_HOUR = 60 * 60;
    public static final int REDIS_ONE_DAY= 24 * 60 * 60;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ShardedJedisPool shardedJedisPool;

    /**
     * 序列化方式从redis中获取对象
     *
     * @param key
     * @return
     */
    @Override
    public <T> T getObject(String key, Class<T> clazz) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                byte[] bytes = jedis.get(key.getBytes());
                if (bytes != null) {
                    T obj = ProtostuffUtil.deserializer(bytes, clazz);
                    if (obj != null) {
                        return obj;
                    }
                }
            } finally {
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 序列化方式向redis中存入对象(1小时过期)
     *
     * @param key
     * @param obj
     * @return
     */
    @Override
    public String putObject(String key, Object obj) {
        return putObject(key, obj, REDIS_ONE_DAY);
    }

    /**
     * 序列化方式向redis中存入对象并设置过期时间(秒)
     *
     * @param key
     * @param obj
     * @return
     */
    @Override
    public String putObject(String key, Object obj, int expire) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                /*存储到redis,成功返回"OK",失败则返回错误信息*/
                byte[] bytes = ProtostuffUtil.serializer(obj);
                return jedis.setex(key.getBytes(), expire, bytes);
            } finally {
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 指定的键值
     *
     * @param key
     * @return
     */
    @Override
    public Long del(String key) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                return jedis.del(key);
            } finally {
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * JSON形式从redis中获取对象数组
     *
     * @param key
     * @return
     */
    @Override
    public <T> List getList(String key, Class<T> clazz) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                String objStr = jedis.get(key);
                if (objStr != null) {
                    return JSON.parseArray(objStr, clazz);
                }
            } finally {
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * JSON形式向redis中存入对象
     *
     * @param key
     * @param obj
     * @return
     */
    @Override
    public String put(String key, Object obj) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                /*存储到redis,成功返回"OK",失败则返回错误信息*/
                return jedis.setex(key, REDIS_ONE_DAY, JSON.toJSONString(obj));
            } finally {

                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 从Redis中取值
     *
     * @param key
     * @return
     */
    @Override
    public String getValue(String key) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                return jedis.get(key);
            } finally {
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }



    /**
     * 往redis里存入值,如果成功则返回1,失败返回0
     * @param key
     * @return
     */
    @Override
    public Long setNxValue(String key) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                return jedis.setnx(key,key);
            } finally {
                jedis.expire(key,REDIS_ONE_DAY);
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 往redis Set 键里面添加成员
     * @param key 键
     * @param expire 时间 单位s
     * @param members
     * @return
     */
    @Override
    public Long addSetValue(String key, int expire, String... members) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                Long i = jedis.sadd(key,members);
                jedis.expire(key,expire);
                return i;
            } finally {
                jedis.expire(key,REDIS_ONE_DAY);
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * redis Set 键里面删除成员
     * @param key
     * @return
     */
    @Override
    public Long moveSetValue(String key, String... members) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                return jedis.srem(key,members);
            } finally {

                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 获取redis Set 键里面所有成员
     * @param key
     * @return
     */
    @Override
    public Set<String> getSetValues(String key) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                return jedis.smembers(key);
            } finally {
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    @Override
    public String putMapValues(String key, Map<byte[], byte[]> map) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                return jedis.hmset(key.getBytes(), map);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            } finally {
                jedis.expire(key,REDIS_ONE_DAY);
                jedis.close();
            }
        }catch (Exception e){
            logger.error(e.getMessage(), e);
        }
        return null;
    }


    /**
     * 序列化方式向redis map中存入对象并设置过期时间(秒)
     *
     * @param key
     * @param obj
     * @return
     */
    public Long putMapObject(String key,String filed, Object obj) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                /*存储到redis,成功返回"OK",失败则返回错误信息*/
                byte[] bytes = ProtostuffUtil.serializer(obj);
                return jedis.hsetnx(key.getBytes(),filed.getBytes(), bytes);
            } finally {
                jedis.expire(key,REDIS_ONE_DAY);
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 序列化方式向redis map中存入对象并设置过期时间(秒)
     *
     * @param key
     * @param s
     * @return
     */
    public Long putMapString(String key,String filed, String s,int expire) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                return jedis.hsetnx(key,filed, s);
            } finally {
                jedis.expire(key,expire);
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    @Override
    public Long incr(String key) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                return jedis.incr(key);
            } finally {
                jedis.expire(key,REDIS_ONE_DAY);
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return 0L;
    }

    @Override
    public String setString(String key, String value) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                return jedis.setex(key,REDIS_ONE_DAY,value);
            } finally {
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

   /* *//**
     * 序列化方式从redis中获取对象
     *
     * @param key
     * @return
     *//*
    public <T> T putAllMapObject(String key,Map<String,Object> objectMap) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                *//*存储到redis,成功返回"OK",失败则返回错误信息*//*
               // byte[] bytes = ProtostuffUtil.serializer(obj);
                return jedis.hmset()
            } finally {
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }*/

    /**
     * 序列化方式从redis中获取对象
     *
     * @param key
     * @return
     */

    @Override
    public <T> T getMapObject(String key, String filed, Class<T> clazz) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                byte[] bytes = jedis.hget(key.getBytes(),filed.getBytes());
                if (bytes != null) {
                    T obj = ProtostuffUtil.deserializer(bytes, clazz);
                    if (obj != null) {
                        return obj;
                    }
                }
            } finally {
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    @Override
    public Map<byte[], byte[]> getAllMap(String key) {
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                Map<byte[], byte[]> allMap = jedis.hgetAll(key.getBytes());
                return allMap;
            } finally {
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 设置 map
     * @param <T>
     * @param key
     * @param map
     */
    public <T> void setMap(String key ,Map<String,T> map){
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                jedis.set(key.getBytes(),ProtostuffUtil.serialize(map));
            } finally {
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

    }

    /**
     * 获取map
     * @param <T>
     * @param key
     * @return Map
     */
    public <T> Map<String,T> getMap(String key){
        try {
            ShardedJedis jedis = shardedJedisPool.getResource();
            try {
                byte[] in = jedis.get(key.getBytes());
                Map<String, T> map = (Map<String, T>) ProtostuffUtil.deserialize(in);
                return map;
            } finally {
                jedis.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return null;
    }
}

  • 集群模式
package com.redis.cluster;

import com.alibaba.fastjson.JSON;
;
import com.redis.JedisCommonDao;
import com.redis.ProtostuffUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCluster;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: lcx
 * @Date: Created at 2018/8/18 11:03.
 * @Description:
 */
@Component
public class JedisClientClusterDao implements JedisCommonDao {

    /*Redis超时时间*/
    public static final int REDIS_TIMEOUT = 60 * 60;
    public static final int REDIS_ONE_DAY= 24 * 60 * 60;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private JedisCluster jedisCluster;

    /**
     * 序列化方式从redis中获取对象
     *
     * @param key
     * @return
     */
    @Override
    public <T> T getObject(String key, Class<T> clazz) {
        try {
            byte[] bytes = jedisCluster.get(key.getBytes());
            if (bytes != null) {
                T obj = ProtostuffUtil.deserializer(bytes, clazz);
                if (obj != null) {
                    return obj;
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 序列化方式向redis中存入对象(1小时过期)
     *
     * @param key
     * @param obj
     * @return
     */
    @Override
    public String putObject(String key, Object obj) {
        return putObject(key, obj, REDIS_ONE_DAY);
    }

    /**
     * 序列化方式向redis中存入对象并设置过期时间(秒)
     *
     * @param key
     * @param obj
     * @return
     */
    @Override
    public String putObject(String key, Object obj, int expire) {
        try {
            /*存储到redis,成功返回"OK",失败则返回错误信息*/
            byte[] bytes = ProtostuffUtil.serializer(obj);
            return jedisCluster.setex(key.getBytes(), expire, bytes);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }


    /**
     * 指定的键值
     *
     * @param key
     * @return
     */
    @Override
    public Long del(String key) {
        try {
            return jedisCluster.del(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * JSON形式从redis中获取对象数组
     *
     * @param key
     * @return
     */
    @Override
    public <T> List getList(String key, Class<T> clazz) {
        try {
            String objStr = jedisCluster.get(key);
            if (objStr != null) {
                return JSON.parseArray(objStr, clazz);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * JSON形式向redis中存入对象
     *
     * @param key
     * @param obj
     * @return
     */
    @Override
    public String put(String key, Object obj) {
        try {
            /*存储到redis,成功返回"OK",失败则返回错误信息*/
            return jedisCluster.setex(key, REDIS_ONE_DAY, JSON.toJSONString(obj));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 从Redis中取值
     *
     * @param key
     * @return
     */
    @Override
    public String getValue(String key) {
        try {
            return jedisCluster.get(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }



    /**
     * 往redis里存入值,如果成功则返回1,失败返回0
     * @param key
     * @return
     */
    @Override
    public Long setNxValue(String key) {
        try {
            return jedisCluster.setnx(key,key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }finally {
             jedisCluster.expire(key,REDIS_ONE_DAY);
        }
        return null;
    }


    /**
     * 往redis Set 键里面添加成员
     * @param key 键
     * @param expire 时间 单位s
     * @param members
     * @return
     */
    @Override
    public Long addSetValue(String key, int expire, String... members) {
        try {
            Long i = jedisCluster.sadd(key,members);
            jedisCluster.expire(key,expire);
            return i;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }finally {
            jedisCluster.expire(key,REDIS_ONE_DAY);
        }
        return null;
    }

    /**
     * redis Set 键里面删除成员
     * @param key
     * @return
     */
    @Override
    public Long moveSetValue(String key, String... members) {
        try {
            return jedisCluster.srem(key,members);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 获取redis Set 键里面所有成员
     * @param key
     * @return
     */
    @Override
    public Set<String> getSetValues(String key) {
        try {
            return jedisCluster.smembers(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }finally {
            jedisCluster.expire(key,REDIS_ONE_DAY);
        }
        return null;
    }


    /**
     * 获取redis Set 键里面所有成员
     * @param key
     * @return
     */
    @Override
    public String putMapValues(String key, Map<byte[],byte[]> map) {
        try {
            String isOk = jedisCluster.hmset(key.getBytes(),map);
            jedisCluster.expire(key.getBytes(),REDIS_ONE_DAY);
            return isOk;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     *  根据键取值
     * @param key
     * @param filed
     * @param clazz
     * @param <T>
     * @return
     */
    @Override
    public <T> T getMapObject(String key, String filed, Class<T> clazz) {
        try {
            byte[] bytes = jedisCluster.hget(key.getBytes(),filed.getBytes());
            if (bytes != null) {
                T obj = ProtostuffUtil.deserializer(bytes, clazz);
                if (obj != null) {
                    return obj;
                }
            }
        }catch (Exception e){
           logger.error(e.getMessage(),e);
        }
        return null;
    }

    public Map<byte[],byte[]> getAllMap(String key) {
        try {
            Map<byte[],byte[]> allMap = jedisCluster.hgetAll(key.getBytes());
            return allMap;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return null;
    }

    /**
     * 序列化方式向redis map中存入对象并设置过期时间(秒)
     *
     * @param key
     * @param obj
     * @return
     */
    public Long putMapObject(String key,String filed, Object obj) {
        try {
            try {
                /*存储到redis,成功返回"OK",失败则返回错误信息*/
                byte[] bytes = ProtostuffUtil.serializer(obj);
                return jedisCluster.hsetnx(key.getBytes(),filed.getBytes(), bytes);
            } finally {
                jedisCluster.expire(key,REDIS_ONE_DAY);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }


    /**
     * 设置 map
     * @param <T>
     * @param key
     * @param map
     */
    public <T> void setMap(String key ,Map<String,T> map){
        try {
            jedisCluster.set(key.getBytes(),ProtostuffUtil.serialize(map));
        } catch (Exception e) {
            logger.error("Set key error : "+e);
        }
    }
    /**
     * 获取map
     * @param <T>
     * @param key
     * @return Map
     */
    public <T> Map<String,T> getMap(String key){
        try {
            byte[] in = jedisCluster.get(key.getBytes());
            Map<String, T> map = (Map<String, T>) ProtostuffUtil.deserialize(in);
            return map;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public Long putMapString(String key,String filed, String s,int expire){
        try {
            try {
                return jedisCluster.hsetnx(key,filed,s);
            } finally {
                jedisCluster.expire(key,expire);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return 0L;
    }

    public Long incr(String key){
        try {
            try {
                return jedisCluster.incr(key);
            } finally {
                jedisCluster.expire(key,REDIS_ONE_DAY);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return 0L;
    }

    public String setString(String key,String value){
        try {
            try {
                return jedisCluster.setex(key,REDIS_ONE_DAY,value);
            } finally {
                jedisCluster.expire(key,REDIS_ONE_DAY);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值