Java Code Examples for redis.clients.jedis.ShardedJedis#expire()

The following examples show how to use redis.clients.jedis.ShardedJedis#expire() . 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: RedisClientTemplate.java    From spring-boot-seed with MIT License 6 votes vote down vote up
/**
 * 设置单个值
 *
 * @param key   key
 * @param value value
 * @return result
 */
public String set(String key, String value, int seconds) {
    String result = null;

    ShardedJedis shardedJedis = getRedisClient();
    if (shardedJedis == null) {
        return null;
    }

    try {
        result = shardedJedis.set(key, value);
        shardedJedis.expire(key, seconds);
    } catch (Exception e) {
        log.error("RedisClientTemplate set error !", e);
    } finally {
        shardedJedis.close();
    }
    return result;
}
 
Example 2
Source File: RedisClientTemplate.java    From spring-boot-seed with MIT License 6 votes vote down vote up
/**
 * 在某段时间后失效
 *
 * @param key     key
 * @param seconds 失效时间
 * @return result
 */
public Long expire(String key, int seconds) {
    Long result = null;
    ShardedJedis shardedJedis = getRedisClient();
    if (shardedJedis == null) {
        return null;
    }

    try {
        result = shardedJedis.expire(key, seconds);
    } catch (Exception e) {
        log.error("RedisClientTemplate expire error !", e);
    } finally {
        shardedJedis.close();
    }
    return result;
}
 
Example 3
Source File: RedisShardedPoolUtil.java    From mmall20180107 with Apache License 2.0 6 votes vote down vote up
/**
 * set the key's period of validity
 * @param key
 * @param exTime
 * @return
 */
public static Long expire(String key,int exTime){

    ShardedJedis jedis = null;
    Long result = null;

    try {
        jedis = RedisShardedPool.getJedis();
        result = jedis.expire(key,exTime);
    } catch (Exception e) {
        log.error("delete key:{}  error",key,e);
        RedisShardedPool.returnBrokenResource(jedis);
        e.printStackTrace();
    }


    RedisShardedPool.returnResource(jedis);

    return result;

}
 
Example 4
Source File: RedisServiceImpl.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
@Override
public long increment(final String key, final int expire, final Callback<Long> callback) {
	checkParameters(key, callback);
	final ShardedJedis jedis = getJedis();

	long incr = jedis.incr(key);
	jedis.expire(key, expire);

	try {
		incr = callback.callback(incr);
	} catch (final Exception e) {
		throw new IncrementRedisException("method: increment, key: " + key + ", incr: " + incr, e);
	}

	returnResource(jedis);
	return incr;
}
 
Example 5
Source File: RedisCacheClient.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
/**
 * 给对应key设置存活时间
 */
@Override
public long expire(String key, int seconds) {
	ShardedJedis redis = null;
	long r = 0L;
	try {
		redis = pool.getResource();
		key = getKeyAll(key);
		r = redis.expire(key, seconds);
		return r;
	} catch (RuntimeException e) { 
        if(redis != null ) {
	        pool.returnBrokenResource(redis);
	    }
        logger.error("redis expire(String key, int seconds):", e);
        return r;
	} finally{ 
         if(redis != null ) {
            pool.returnResource(redis);
         }
	}
	
}
 
Example 6
Source File: ShardedRedisCache.java    From framework with Apache License 2.0 6 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @param key
 * @param value
 * @param expireTime
 * @return <br>
 */
@Override
public boolean setnx(final String key, final String value, final int expireTime) {
    ShardedJedis shardedJedis = null;
    try {
        shardedJedis = shardedPool.getResource();
        if (shardedJedis.setnx(key, value) - 1 == 0) {
            shardedJedis.expire(key, expireTime);
            return true;
        }
    }
    finally {
        if (shardedJedis != null) {
            shardedJedis.close();
        }
    }
    return false;
}
 
Example 7
Source File: JedisCacheService.java    From howsun-javaee-framework with Apache License 2.0 6 votes vote down vote up
/**
 * 设置过期时间
 */
public Long expire(String key, int seconds){
	ShardedJedis jedis = null;
	try {
		jedis = cacheFactory.getResource();
		return jedis.expire(key, seconds);
	}
	catch (Exception e) {
		log.error("操作缓存失败:", e);
	}
	finally{
		if(jedis != null){
			try {cacheFactory.returnResource(jedis);}
			catch (Exception e2) {
				log.error("不能释放缓存操纵对象:",e2);
			}
		}
	}
	return null;
}
 
Example 8
Source File: MarsRedisTemplate.java    From Mars-Java with MIT License 5 votes vote down vote up
/**
 * 更新byte类型的数据,主要更新过期时间
 * @param key
 * @param expireTimeSecond
 */
public void updateObjectEnableTime(byte[] key, int expireTimeSecond) {
    ShardedJedis jedis = null;
    try {
        jedis = getShardedJedis();
        jedis.expire(key, expireTimeSecond);
    } catch (Exception e) {
        logger.error("updateObject error:" + key, e);
    } finally {
        recycleJedis(jedis);
    }
}
 
Example 9
Source File: JedisUtil.java    From xxl-sso with GNU General Public License v3.0 5 votes vote down vote up
/**
 * expire reset
 *
 * @param key
 * @param seconds 存活时间,单位/秒
 * @return Integer reply, specifically:
 * 1: the timeout was set.
 * 0: the timeout was not set since the key already has an associated timeout (versions lt 2.1.3), or the key does not exist.
 */
public static long expire(String key, int seconds) {
    Long result = null;
    ShardedJedis client = getInstance();
    try {
        result = client.expire(key, seconds);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return result;
}
 
Example 10
Source File: RedisShardedPoolUtil.java    From mmall-kay-Java with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param key
 * @param exTime seconds
 * @return 1: success 0:fail
 */
public static Long expire(String key,int exTime){
    ShardedJedis jedis = null;
    Long result=null;
    try {
        jedis = RedisShardedPool.getJedis();
        result = jedis.expire(key, exTime);
    } catch (Exception e) {
        log.error("expire key:{} error",key,e);
        RedisShardedPool.returnBrokenResource(jedis);
        return result;
    }
    RedisShardedPool.returnResource(jedis);
    return result;
}
 
Example 11
Source File: RedisServiceImpl.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
@Override
public boolean set(final String key, final String value, final int expire) {
	checkParameters(key, value);
	final ShardedJedis jedis = getJedis();
	final String result = jedis.set(key, value);
	jedis.expire(key, expire);
	returnResource(jedis);
	return SUCCESS.equalsIgnoreCase(result);
}
 
Example 12
Source File: RedisServiceImpl.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
@Override
public boolean saveMap(final String key, final Map<String, String> value, final int expire) {
	checkParameters(key, value);

	final ShardedJedis jedis = getJedis();
	final String result = jedis.hmset(key, value);
	jedis.expire(key, expire);

	returnResource(jedis);
	return SUCCESS.equalsIgnoreCase(result);
}
 
Example 13
Source File: RedisServiceImpl.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
@Override
public boolean hset(final String key, final String field, final String value, final int expire) {
	checkParameters(key, field, value);

	final ShardedJedis jedis = getJedis();
	final long result = jedis.hset(key, field, value);

	jedis.expire(key, expire);

	returnResource(jedis);
	return result == 0;
}
 
Example 14
Source File: RedisServiceImpl.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
@Override
public boolean hmset(final String key, final Map<String, String> hash, final int expire) {
	checkParameters(key, hash);

	final ShardedJedis jedis = getJedis();
	try {
		final String result = jedis.hmset(key, hash);
		jedis.expire(key, expire);
		return SUCCESS.equalsIgnoreCase(result);
	} catch (final JedisConnectionException e) {
		throw new MapRedisException("method: hmset, key:" + key, e);
	} finally {
		returnResource(jedis);
	}
}
 
Example 15
Source File: JedisManager.java    From es-service-parent with Apache License 2.0 5 votes vote down vote up
/**
 * 设置超时时间
 * 
 * @param key
 * @param seconds
 * @return
 */
public boolean expire(String key, int seconds) {
    ShardedJedis shardedJedis = shardedJedisPool.getResource();
    try {
        return shardedJedis.expire(key, seconds) == 1;
    } catch (Exception e) {
        shardedJedis.close();
        return false;
    }
}
 
Example 16
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public long expire(final String key, final int seconds) {
    Assert.hasText(key);

    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        return jedis.expire(key, seconds);
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}