本文整理匯總了Java中redis.clients.jedis.ShardedJedis.expire方法的典型用法代碼示例。如果您正苦於以下問題:Java ShardedJedis.expire方法的具體用法?Java ShardedJedis.expire怎麽用?Java ShardedJedis.expire使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類redis.clients.jedis.ShardedJedis
的用法示例。
在下文中一共展示了ShardedJedis.expire方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: set
import redis.clients.jedis.ShardedJedis; //導入方法依賴的package包/類
/**
* 設置單個值
*
* @param key 鍵
* @param value 值
* @return 結果
*/
public String set(String key, String value, int timeout) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return null;
}
try {
result = shardedJedis.set(key, value);
if (0 != timeout) {
shardedJedis.expire(key, timeout);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
示例2: expire
import redis.clients.jedis.ShardedJedis; //導入方法依賴的package包/類
/**
* 在某段時間後實現
*
* @param key 鍵
* @param seconds 時間
* @return 返回
*/
public Long expire(String key, int seconds) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return null;
}
try {
result = shardedJedis.expire(key, seconds);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
示例3: lock
import redis.clients.jedis.ShardedJedis; //導入方法依賴的package包/類
/**
* 獲取分布式鎖
*
* @param lockedKey 建議業務名稱+業務主鍵
* @param timeout 獲取分布式鎖的超時時間(毫秒)
* @return true:獲取鎖成功,fasle:獲取鎖失敗
*/
public boolean lock(String lockedKey, long timeout) {
long nano = System.nanoTime();
ShardedJedis shardedJedis = shardedJedisPool.getResource();
long timeoutNanos = timeout * 1000000L;
while ((System.nanoTime() - nano) < timeoutNanos) {
if (shardedJedis.setnx(lockedKey, CoreDateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS")) == 1) {
shardedJedis.expire(lockedKey, 5);
log.info("RedisSimpleLockUtils.lock-->lockedKey=‘{}’ , timeout={}毫秒", lockedKey, timeout);
return true;
}
// 短暫休眠,nano避免出現活鎖
try {
Thread.sleep(2, new Random().nextInt(500));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return false;
}
示例4: run
import redis.clients.jedis.ShardedJedis; //導入方法依賴的package包/類
public <K> K run(String key, Executor<K> executor, Integer... expire) {
ShardedJedis jedis = getJedis();
if (jedis == null) {
return null;
}
try {
K result = executor.execute(jedis);
if (jedis.exists(key)) {
if (expire == null || expire.length == 0) {
jedis.expire(key, EXPIRE);
} else if (expire.length == 1) {
jedis.expire(key, expire[0]);
}
}
return result;
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
if (jedis != null) {
jedis.close();
}
}
return null;
}
示例5: run
import redis.clients.jedis.ShardedJedis; //導入方法依賴的package包/類
public static <K> K run(String key, Executor<K> executor, Integer... expire) {
ShardedJedis jedis = getJedis();
if (jedis == null) {
return null;
}
try {
K result = executor.execute(jedis);
if (jedis.exists(key)) {
if (expire == null || expire.length == 0) {
jedis.expire(key, EXPIRE);
} else if (expire.length == 1) {
jedis.expire(key, expire[0]);
}
}
return result;
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
if (jedis != null) {
jedis.close();
}
}
return null;
}
示例6: increment
import redis.clients.jedis.ShardedJedis; //導入方法依賴的package包/類
@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;
}
示例7: expire
import redis.clients.jedis.ShardedJedis; //導入方法依賴的package包/類
/**
* 給對應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);
}
}
}
示例8: set
import redis.clients.jedis.ShardedJedis; //導入方法依賴的package包/類
@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);
}
示例9: saveMap
import redis.clients.jedis.ShardedJedis; //導入方法依賴的package包/類
@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);
}
示例10: hset
import redis.clients.jedis.ShardedJedis; //導入方法依賴的package包/類
@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;
}
示例11: hmset
import redis.clients.jedis.ShardedJedis; //導入方法依賴的package包/類
@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);
}
}