zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    }

    结束。

  • 相关阅读:
    git 常用操作命令行
    Mysql 命令行...
    bootstrap
    10.11 android输入系统_补充知识_activity_window_decor_view关系
    10.10 android输入系统_APP获得并处理输入事件流程
    10.9 android输入系统_APP跟输入系统建立联系和Dispatcher线程_分发dispatch
    10.8 android输入系统_实战_使用GlobalKey一键启动程序
    10.7 android输入系统_Dispatcher线程情景分析_Reader线程传递事件和dispatch前处理
    10.6 android输入系统_Dispatcher线程_总体框架
    10.5 android输入系统_Reader线程_使用EventHub读取事件和核心类及配置文件_实验_分析
  • 原文地址:https://www.cnblogs.com/it-deepinmind/p/12781862.html
Copyright © 2011-2022 走看看