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);
            }
        }
    }

    结束。

  • 相关阅读:
    iOS开发—block介绍
    iOS开发—页面传值汇总
    解决Xcode升级后一些Xcode插件不能使用的问题
    记录一些优秀的iOS第三方框架
    【转】iOS开发—SQLite的简单使用
    iOS—dictionary写入文件出现的几个问题
    NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象
    【原】自定义tableviewcell中多个button点击实现不同功能
    【转】自定义tableViewCell中button push viewcontroller的实现(delegate和Block)
    限制UITextField只可以输入数字
  • 原文地址:https://www.cnblogs.com/it-deepinmind/p/12781862.html
Copyright © 2011-2022 走看看