美文网首页java
Redis在java客户端的使用--Jedis(原生实现)

Redis在java客户端的使用--Jedis(原生实现)

作者: 二妹是只猫 | 来源:发表于2019-03-07 16:55 被阅读0次

    服务端

    在终端中启动redis服务端:
    redis-server

    客户端(Jedis)

    pom文件导入jedis:
    <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.6.0</version>
    </dependency>
    
    创建JedisPool以用于设置jedis参数并获取jedis实例对象:
    public class RedisPool {
        //jedis链接池
        private static JedisPool pool;
        //最大连接数
        private static Integer maxTotal = Integer.parseInt(PropertiesUtil.getProperty("redis.max.tota","20"));
        //最多空闲实例个数
        private static Integer maxIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.max.idle","10"));
        private static Integer minIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.min.tota","2"));
        //在borrow一个jedis实例时,是否需要验证操作
        private static Boolean testOnBorrow = Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.borro","true"));
        //在return一个jedis实例时,是否需要验证操作
        private static Boolean testOnRetrun = Boolean.parseBoolean(PropertiesUtil.getProperty("redis.max.tota","true"));
    
        private static String redisIp = PropertiesUtil.getProperty("redis1.ip");
        private static Integer redisPort = Integer.parseInt(PropertiesUtil.getProperty("redis1.port"));
    
        private static void initPool(){
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(maxTotal);
            config.setMaxIdle(maxIdle);
            config.setMinIdle(minIdle);
    
            config.setTestOnBorrow(testOnBorrow);
            config.setTestOnReturn(testOnRetrun);
            //连接耗尽的是否是否阻塞
            config.setBlockWhenExhausted(true);
    
    
            pool = new JedisPool(config,redisIp,redisPort,1000+2);
        }
    
        static {
            initPool();
        }
        public static Jedis getJedis(){
           return pool.getResource();
        }
    
        public static void returnBrokenResource(Jedis jedis){
            pool.returnBrokenResource(jedis);
        }
        public static void returnResource(Jedis jedis){
            pool.returnResource(jedis);
        }
    
    • 设置config参数
    • 在JedisPool构造器中传入config,ip,端口号和过期时间
    • 上述初始化完成过后就可以通过getJedis()获取到jedis对象,进行存储、查看、设置等操作,具体命令查看Redis基础命令
    • jedis操作后成功调用returnResource失败调用returnBrokenResources方法,这里真正的将之前的操作设置到了服务器。
    这里封装了一个工具类以供参考:
    public class RedisPoolUtil {
    
        public static String set(String key, String value) {
            Jedis jedis = null;
            String result = null;
    
            try {
                jedis = RedisPool.getJedis();
                result = jedis.set(key, value);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("set key:{}  valut:{} error {}", key, value, e);
                RedisPool.returnBrokenResources(jedis);
                return result;
            }
            RedisPool.returnBrokenResources(jedis);
            return result;
        }
    
        //设置key的有效期,单位是秒
        public static Long expire(String key, int exTime) {
            Jedis jedis = null;
            Long result = null;
    
            try {
                jedis = RedisPool.getJedis();
                result = jedis.expire(key, exTime);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("expire key:{}  exTime:{} error {}", key, exTime, e);
                RedisPool.returnBrokenResources(jedis);
                return result;
            }
            RedisPool.returnBrokenResources(jedis);
            return result;
        }
    
    
    
        public static String setEx(String key, String value, int exTime) {
            Jedis jedis = null;
            String result = null;
    
            try {
                jedis = RedisPool.getJedis();
                result = jedis.setex(key, exTime, value);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("setEx key:{}  valut:{} error {}", key, value, e);
                RedisPool.returnBrokenResources(jedis);
                return result;
            }
            RedisPool.returnBrokenResources(jedis);
            return result;
        }
    
        public static String get(String key) {
            Jedis jedis = null;
            String result = null;
    
            try {
                jedis = RedisPool.getJedis();
                result = jedis.get(key);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("get key:{}  error {}", key, e);
                RedisPool.returnBrokenResources(jedis);
                return result;
            }
            RedisPool.returnBrokenResources(jedis);
            return result;
        }
    
        public static Long del(String key) {
            Jedis jedis = null;
            Long result = null;
    
            try {
                jedis = RedisPool.getJedis();
                result = jedis.del(key);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("del key:{}  error {}", key, e);
                RedisPool.returnBrokenResources(jedis);
                return result;
            }
            RedisPool.returnBrokenResources(jedis);
            return result;
        }
    }
    
    测试:
      public static void main(String[] args) {
            Jedis jedis = pool.getResource();
    
            RedisPoolUtil.set("test","testvalue");
    
            String value = RedisPoolUtil.get("test");
    
            RedisPoolUtil.setEx("exTest","exValue",60*10);
    
    //        RedisPoolUtil.del("test");
    
            System.out.println("args = [" + args + "]");
        }
    
    客户端中查看:
    image.png

    通过keys *查看到test与exTest存储成功,并成功查询到test的值,ttl命令查看exTest的有效时间。这样一个简单的jedis就通过测试了

    相关文章

      网友评论

        本文标题:Redis在java客户端的使用--Jedis(原生实现)

        本文链接:https://www.haomeiwen.com/subject/nlqhpqtx.html