Jedis操作Redis

Jedis操作Redis的常用封装方法

@Resource(name="jedispool")
    private JedisPool pool=null;
    /**
     * 设置缓存对象过期时间
     * @param key 缓存的key
     * @param seconds 秒,多少秒后过期自动销毁
     */
    public boolean expireKey(String key,int seconds){
        Jedis jedis=null;
        ObjectOutputStream w=null;
        ByteArrayOutputStream b=null;
        try{
            jedis=pool.getResource();
            long result=jedis.expire(key.getBytes("utf-8"), seconds);
            return result>1;
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            try{if(jedis!=null){pool.returnResourceObject(jedis);}}catch(Exception ex){}
            try{b.close();}catch(Exception ex){}
            try{w.close();}catch(Exception ex){}
        }
        return false;
    }
    
    /**
     * 查询缓存对象还有多少秒过期
     * @param key 缓存对象的key
     * @return 剩余秒数。   如果key不存在返回-2,如果没有为key设置过期时间返回-1
     */
    public long selectGqTime(String key){
        Jedis jedis=null;
        ObjectOutputStream w=null;
        ByteArrayOutputStream b=null;
        try{
            jedis=pool.getResource();
            return jedis.ttl(key.getBytes("utf-8"));
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            try{if(jedis!=null){pool.returnResourceObject(jedis);}}catch(Exception ex){}
            try{b.close();}catch(Exception ex){}
            try{w.close();}catch(Exception ex){}
        }
        return -1;
    }
    
    /**
     * 向缓存添加对象
     * @param key
     * @param obj
     */
    public void setObject(String key,Serializable obj){
        Jedis jedis=null;
        ObjectOutputStream w=null;
        ByteArrayOutputStream b=null;
        try{
            jedis=pool.getResource();
            b=new ByteArrayOutputStream();
            w=new ObjectOutputStream(b);
            w.writeObject(obj);
            w.flush();
            b.flush();
            byte[] data=b.toByteArray();
            jedis.set(key.getBytes("utf-8"), data);
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            try{if(jedis!=null){pool.returnResourceObject(jedis);}}catch(Exception ex){}
            try{b.close();}catch(Exception ex){}
            try{w.close();}catch(Exception ex){}
        }
    }
    
    /**
     * 从缓存中获取对象
     * @param key
     * @return
     */
    public Object getObject(String key){
        Jedis jedis=null;
        ObjectInputStream w=null;
        ByteArrayInputStream b=null;
        try{
            jedis=pool.getResource();
            if(jedis.exists(key.getBytes("utf-8"))){
                byte[] data=jedis.get(key.getBytes("utf-8"));
                b=new ByteArrayInputStream(data);
                w=new ObjectInputStream(b);
                Object obj=w.readObject();
                return obj;
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            try{if(jedis!=null){pool.returnResourceObject(jedis);}}catch(Exception ex){}
            try{b.close();}catch(Exception ex){}
            try{w.close();}catch(Exception ex){}
        }
        return null;
    }
    
    /**
     * 删除缓存对象
     * @param key
     */
    public void delObject(String key){
        Jedis jedis=null;
        try{
            jedis=pool.getResource();
            jedis.del(key.getBytes("utf-8"));
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            try{if(jedis!=null){pool.returnResourceObject(jedis);}}catch(Exception ex){}
        }
    }
    
    public JedisPool getPool() {
        return pool;
    }

    public void setPool(JedisPool pool) {
        this.pool = pool;
    }
原文地址:https://www.cnblogs.com/aeolian/p/8557251.html