• JedisPool实现


    开始:

    import com.alibaba.fastjson.JSONObject;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    
    import java.util.Set;
    
    /**
     * redis
     * @author*/
    @Service
    @Slf4j
    public class RedisServiceImpl {
        @Autowired
        JedisPool pool;
    
        @Value("${spring.redis.database.order}")
        private Integer orderDBIndex;
    
        /**
         * 同步获取Jedis实例
         *
         * @return Jedis
         */
        public synchronized  Jedis getCache() {
            Jedis connection = pool.getResource();
            connection.select(orderDBIndex);
            return connection;
        }
    
        /**
         * 根据key 获取对象
         *
         * @param key
         * @return
         */
        public  String get(String key) {
            Jedis connection = getCache();
            try {
                return connection.get(key);
            } finally {
                releaseConnection(connection);
            }
        }
    
    
        /**
         * 如果 key 已经持有其他值, SET 就覆写旧值,无视类型
         *
         * @param key
         *            rediskey
         * @param value
         *            如果是不是String转化为json
         * @return OK
         * @throws Exception
         */
        public  String set(String key, Object value) {
            Jedis connection = getCache();
            try {
                if (value instanceof String) {
                    return connection.set(key, (String) value);
                }
    
                String json = JSONObject.toJSONString(value);
                return connection.set(key, json);
            } finally {
                releaseConnection(connection);
            }
        }
    
        /**
         * 校验key存在
         * @param key
         * @return
         */
        public  Boolean exists(String key) {
            Jedis connection = getCache();
            try {
                return connection.exists(key);
            } finally {
                releaseConnection(connection);
            }
        }
    
        /**
         * 如果 key 已经持有其他值, SET 就覆写旧值,无视类型
         * @param key
         * @param value
         * @param expire
         * @return
         */
        public  String set(String key, Object value, Integer expire) {
            Jedis connection = getCache();
            try {
                if (value instanceof String) {
                    return connection.setex(key, expire, (String) value);
                }
                String json = JSONObject.toJSONString(value);
                return connection.setex(key, expire, json);
            } finally {
                releaseConnection(connection);
            }
        }
    
        /**
         * 删除缓存中得对象,根据key
         *
         * @param key
         * @return
         */
        public  Long del(String key) {
            Jedis connection = getCache();
            try {
                return connection.del(key);
            } finally {
                releaseConnection(connection);
            }
        }
    
        /**
         *
         * Description: 获取key的过期时间
         *
         * @param key
         * @return
         * @return long 剩余过期时间(秒)
         * @throw
         */
        public  long ttl(String key) {
            Jedis connection = getCache();
            try {
                return connection.ttl(key);
            } finally {
                releaseConnection(connection);
            }
        }
    
    
        /** key **/
        /**
         * 如果 key 已经持有其他值, SET 就覆写旧值,无视类型
         *
         * @param key
         *            rediskey
         * @param expire
         *            seconds
         * @return 1
         * @throws Exception
         */
        public  Long expire(String key, Integer expire) {
            Jedis connection = getCache();
            try {
                return connection.expire(key, expire);
            } finally {
                releaseConnection(connection);
            }
        }
    
        /**
         * 根据key 获取对象集合
         *
         * @param key
         * @param clazz
         *            集合元素泛型
         * @return
         */
        public  <T> T get(String key, Class<T> clazz) {
            Jedis connection = getCache();
            try {
                return (T) JSONObject.parseObject(
                        connection.get(key), clazz);
            } finally {
                releaseConnection(connection);
            }
    
        }
    
        /**
         * 右进
         * @param key
         * @param value
         * @return
         */
        public  Long rpush(String key, Object value) {
            Jedis connection = getCache();
            try {
                if (value instanceof String) {
                    return connection.rpush(key, (String) value);
                }
                String json = JSONObject.toJSONString(value);
                return connection.rpush(key, json);
            } finally {
                releaseConnection(connection);
            }
        }
    
        /**
         * 右出
         * @param key
         * @return
         */
        public  String rpop(String key) {
            Jedis connection = getCache();
            try {
                return connection.rpop(key);
            } finally {
                releaseConnection(connection);
            }
        }
    
        public Long llen(String key){
            Jedis connection = getCache();
            try {
                return connection.llen(key);
            } finally {
                releaseConnection(connection);
            }
        }
    
        /**
         * 释放连接
         * @param connection
         */
        private  void releaseConnection(Jedis connection) {
            if (connection != null) {
                connection.close();
            }
        }
    
        public  Long lpush(String key, Object value) {
            Jedis connection = getCache();
            try {
                if (value instanceof String) {
                    return connection.lpush(key, (String) value);
                }
                String json = JSONObject.toJSONString(value);
                return connection.lpush(key, json);
            } finally {
                releaseConnection(connection);
            }
        }
    
        /**
         * @Author: 
         * @Description: 模糊查询,比如(查询key前缀+"*")通配符说明:*(0到任意多个字符),?(1个字符)
         * @Date 
         * @Param [key]
         * @return java.util.Set<java.lang.String>
         **/
        public Set<String> keys(String key){
            Jedis connection = getCache();
            try {
                return connection.keys(key);
            } finally {
                releaseConnection(connection);
            }
        }
    }

    结束。

  • 相关阅读:
    Bellman算法
    Codeforces Round #378 (Div. 2) D
    运算符优先级
    Kruskal算法
    Java 大数运算
    无根树转有根树
    欧拉函数模板
    HDU 4135 Co-prime(容斥原理)
    快速求n的质因子(数论)
    Markdown中插入数学公式
  • 原文地址:https://www.cnblogs.com/it-deepinmind/p/12781862.html
Copyright © 2020-2023  润新知