Java Code Examples for redis.clients.jedis.Jedis#ttl()

The following examples show how to use redis.clients.jedis.Jedis#ttl() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CacheServiceRedisImpl.java    From redis_util with Apache License 2.0 6 votes vote down vote up
public Long ttl(String key) {
    log.trace("get set expire " + key);
    Jedis jedis = null;
    try {
        jedis = redisConnection.getJedis();
        jedis.select(dbIndex);
        return jedis.ttl(key);
    } catch (Exception e) {
        log.warn(e.getMessage(), e);
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
    return -2L;
}
 
Example 2
Source File: JedisClientPool.java    From BigDataPlatform with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Long ttl(String key) {
	Jedis jedis = jedisPool.getResource();
	Long result = jedis.ttl(key);
	jedis.close();
	return result;
}
 
Example 3
Source File: JedisUtils.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * 以秒为单位,返回给定 key 的剩余生存时间
 * </p>
 *
 * @param key
 * @return 当 key 不存在时,返回 -2 。当 key 存在但没有设置剩余生存时间时,返回 -1 。否则,以秒为单位,返回 key
 *         的剩余生存时间。 发生异常 返回 0
 */
public Long ttl(String key,int indexdb) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        jedis.select(indexdb);
        return jedis.ttl(key);
    } catch (Exception e) {

        log.error(e.getMessage());
        return 0L;
    } finally {
        returnResource(jedisPool, jedis);
    }
}
 
Example 4
Source File: JedisUtil.java    From scaffold-cloud with MIT License 5 votes vote down vote up
/**
 * 返回给定key的有效时间,以秒为单位 当 key 不存在时,返回 -2 。 当 key 存在但没有设置剩余生存时间时,返回 -1 。 否则,以秒为单位,返回 key 的剩余生存时间。
 *
 * @param key
 * @return
 * @author jiaheng
 * @date 2017-9-26
 */
public static long ttl(String key) {
    Long result = 0L;
    Jedis jedis = null;
    try {
        jedis = getResource();
        result = jedis.ttl(key);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        close(jedis);
    }
    return result;
}
 
Example 5
Source File: JedisUtil.java    From Project with Apache License 2.0 5 votes vote down vote up
/**
 * 查询key的过期时间
 * 
 * @param key
 * @return 以秒为单位的时间表示
 */
public long ttl(String key) {
	// ShardedJedis sjedis = getShardedJedis();
	Jedis sjedis = getJedis();
	long len = sjedis.ttl(key);
	sjedis.close();
	return len;
}
 
Example 6
Source File: JedisUtil.java    From SpringBoot-Home with Apache License 2.0 5 votes vote down vote up
/**
 * 获取剩余时间
 *
 * @param key
 * @return
 */
public Long ttl(String key) {
    Jedis jedis = null;
    try {
        jedis = getJedis();
        return jedis.ttl(key);
    } catch (Exception e) {
        log.error("ttl key:{} error", key, e);
        return null;
    } finally {
        close(jedis);
    }
}
 
Example 7
Source File: JedisClientPool.java    From express-ssm with Apache License 2.0 5 votes vote down vote up
@Override
public Long ttl(String key) {
    Jedis jedis = jedisPool.getResource();
    Long result = jedis.ttl(key);
    jedis.close();
    return result;
}
 
Example 8
Source File: JedisClientPool.java    From blog-sample with Apache License 2.0 5 votes vote down vote up
@Override
public Long ttl(String key) {
    Jedis jedis = jedisPool.getResource();
    Long result = jedis.ttl(key);
    jedis.close();
    return result;
}
 
Example 9
Source File: JedisClientPool.java    From paas with Apache License 2.0 5 votes vote down vote up
@Override
public Long ttl(String key) {
    Jedis jedis = jedisPool.getResource();
    Long result = jedis.ttl(key);
    jedis.close();
    return result;
}
 
Example 10
Source File: JedisClientSingle.java    From learning-taotaoMall with MIT License 5 votes vote down vote up
@Override
public long ttl(String key) {
	Jedis jedis = jedisPool.getResource();
	Long result = jedis.ttl(key);
	jedis.close();
	return result;
}
 
Example 11
Source File: JedisSegmentScoredQueueStore.java    From vscrawler with Apache License 2.0 5 votes vote down vote up
private boolean lockQueue(String queueID) {
    if (locked.get() == null) {
        synchronized (this) {
            if (locked.get() == null) {
                locked.set(new AtomicInteger(0));
            }
        }
    }
    boolean ret = false;
    @Cleanup Jedis jedis = jedisPool.getResource();
    if (locked.get().incrementAndGet() > 1) {
        return true;
    }
    String redisLockKey = makeRedisLockKey(queueID);
    long lockRequestTime = System.currentTimeMillis();
    while (true) {
        String result = jedis.set(redisLockKey, "lockTheQueue", "NX", "EX", 120);
        if (StringUtils.isNotEmpty(result) && result.equalsIgnoreCase("OK")) {
            ret = true;
            break;
        }
        if (lockRequestTime + lockWaitTimeStamp < System.currentTimeMillis()) {
            locked.get().decrementAndGet();
            break;
        }
        long sleepTime = jedis.ttl(redisLockKey) * 1000 - 10;
        if (sleepTime > lockRequestTime) {
            return false;
        }
        if (sleepTime > 0) {
            CommonUtil.sleep(sleepTime);
        }
    }
    return ret;
}
 
Example 12
Source File: BaseJedisOperationQueueStore.java    From vscrawler with Apache License 2.0 5 votes vote down vote up
private boolean lockQueueInternal(String queueID) {
    if (locked.get() == null) {
        synchronized (this) {
            if (locked.get() == null) {
                locked.set(new AtomicInteger(0));
            }
        }
    }
    if (locked.get().incrementAndGet() > 1) {
        return true;
    }
    Jedis jedis = jedisPool.getResource();
    try {
        String redisLockKey = makeRedisLockKey(queueID);
        long lockRequestTime = System.currentTimeMillis();
        while (true) {
            String result = jedis.set(redisLockKey, "lockTheQueue", "NX", "EX", 120);
            if (result.equalsIgnoreCase("OK")) {
                return true;
            }
            if (lockRequestTime + lockWaitTimeStamp > System.currentTimeMillis()) {
                locked.get().decrementAndGet();
                return false;
            }
            long sleepTime = jedis.ttl(redisLockKey) * 1000 - 10;
            if (sleepTime > lockRequestTime) {
                return false;
            }
            if (sleepTime > 0) {
                CommonUtil.sleep(sleepTime / 4);
            }
        }
    } finally {
        IOUtils.closeQuietly(jedis);
    }
}
 
Example 13
Source File: JedisUtil.java    From BigData with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 查询key的过期时间
 * 
 * @param String
 *            key
 * @return 以秒为单位的时间表示
 * */
public long ttl(String key) {
	// ShardedJedis sjedis = getShardedJedis();
	Jedis sjedis = getJedis();
	long len = sjedis.ttl(key);
	returnJedis(sjedis);
	return len;
}
 
Example 14
Source File: TestFixture.java    From shiro-redis with MIT License 5 votes vote down vote up
public static Long getRedisTTL(String key, RedisSerializer keySerializer) {
    Jedis jedis = doGetRedisInstance();
    Long ttl = 0L;
    try {
        ttl = jedis.ttl(keySerializer.serialize(key));
    } catch (SerializationException e) {
        e.printStackTrace();
    }
    jedis.close();
    return ttl;
}
 
Example 15
Source File: ManageLoginStatusDialog.java    From gameserver with Apache License 2.0 4 votes vote down vote up
public void init() {
	this.setTitle("管理登陆状态");
	this.setSize(320, 430);
	Point p = WindowUtils.getPointForCentering(this);
	this.setLocation(p);
	this.setModal(true);
	this.statusField.setActionCommand(CMD_STATUS_SELECT);
	this.statusField.addActionListener(this);
	this.statusField.setSelectedIndex(user.getLoginStatus().ordinal());
	try {
		Jedis jedis = AdminJedis.getInstance().getJedis();
		Long ttlLong = jedis.ttl(LoginManager.getInstance().getUserPauseKey(user.getUsername()));
		if ( ttlLong != null && ttlLong.intValue()>0 ) {
			this.valueField.setValue(ttlLong.intValue());
		} else {
			this.valueField.setValue(0);
			if ( user.getLoginStatus() == UserLoginStatus.PAUSE ) {
				this.statusField.setSelectedIndex(0);
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	this.descField.setText(user.getLoginStatusDesc());
	switch ( user.getStatus() ) {
		case NORMAL:
			this.chatField.setSelectedIndex(0);
			break;
		case CHAT_DISABLE:
			this.chatField.setSelectedIndex(1);
			break;
	}
	this.okButton.setActionCommand(ActionName.OK.name());
	this.okButton.addActionListener(this);
	this.cancelButton.setActionCommand(ActionName.CANCEL.name());
	this.cancelButton.addActionListener(this);
	this.previewButton.setActionCommand(CMD_PREVIEW);
	this.previewButton.addActionListener(this);
	
	JXPanel loginPanel = new JXPanel(new MigLayout("wrap 2, gap 10px", "[45%][55%]"));
	loginPanel.add(this.statusLabel, "sg lbl");
	loginPanel.add(this.statusField, "sg fd, grow");
	loginPanel.add(this.valueField, "sg fd, grow");
	loginPanel.add(this.timeUnitField, "sg fd, grow");
	loginPanel.add(this.descField, "span, grow");
	loginPanel.add(this.previewButton, "gaptop 5px, span, split 3, align center");		
	loginPanel.setBorder(BorderFactory.createTitledBorder("登陆管理"));
					
	JXPanel chatPanel = new JXPanel(new MigLayout("wrap 2, gap 10px", "[45%][55%]"));
	chatPanel.setBorder(BorderFactory.createTitledBorder("聊天管理"));
	chatPanel.add(this.chatLabel, "sg lbl");
	chatPanel.add(this.chatField, "sg fd, grow");
	
	JXPanel panel = new JXPanel(new MigLayout("wrap 1, gap 10px", "[100%]"));
	this.setLayout(new MigLayout("wrap 1"));
	panel.add(loginPanel);
	panel.add(chatPanel);
	panel.add(this.okButton, "gaptop 5px, span, split 3, align center");
	panel.add(this.cancelButton);

	this.add(panel, "width 100%, height 100%");
}
 
Example 16
Source File: DefaultRedis.java    From craft-atom with MIT License 4 votes vote down vote up
private Long ttl0(Jedis j, String key) {
	return j.ttl(key);
}