本文整理匯總了Java中redis.clients.jedis.Transaction.expire方法的典型用法代碼示例。如果您正苦於以下問題:Java Transaction.expire方法的具體用法?Java Transaction.expire怎麽用?Java Transaction.expire使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類redis.clients.jedis.Transaction
的用法示例。
在下文中一共展示了Transaction.expire方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: taskLockVersionOne
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
/**
* 方案一的壞處:
* 假如在極端情況下,可能出現集群各個服務器同時執行到taskLockVersionOne,或者 doTask執行時間過長,
* 在redis事物還沒提交的時候,會出現同時有多台服務器執行doTask。
* @param id
*/
private static void taskLockVersionOne(String id) {
String key = "default_task_id";
String value = jedis.get(key);
// 用redis 事物是防止 在set成功之後 在執行doTask或者其他情況導致程序終止沒有執行到transaction.expire()
// 導致單台機器一直占著鎖會有單點事故
Transaction transaction = null;
try {
transaction = jedis.multi();
if (value == null) {
transaction.set(key, id);
doTask(id);
// 設置過期是防止單點錯誤
transaction.expire(key, 30);
} else {
if (value.equals(id)) {
doTask(id);
}
}
} catch (Exception e) {
log.error("e" + e);
} finally {
transaction.exec();
}
}
示例2: taskLockVersionTwo
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
/**
* 方案二:
* 會重複兩次回去jedis.get(key);
* 同時如果expire時間小於doTask時間,也會出現同時有兩個任務執行doTask情況。
* @param id
*/
private static void taskLockVersionTwo(String id) {
String key = "default_task_id";
String value = jedis.get(key);
// 用redis 事物是防止 在set成功之後 在執行doTask或者其他情況導致程序終止沒有執行到transaction.expire()
// 導致單台機器一直占著鎖會有單點事故
Transaction transaction = null;
try {
transaction = jedis.multi();
if (value == null) {
transaction.set(key, id);
transaction.expire(key, 30);
}
} catch (Exception e) {
log.error("e" + e);
} finally {
transaction.exec();
}
value = jedis.get(key);
if (value.equals(id)) {
doTask(id);
}
}
示例3: main
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
public static void main(String[] args) throws InterruptedException {
ApplicationContext ac = new ClassPathXmlApplicationContext("root-context.xml");
JedisPool pool = (JedisPool) ac.getBean("jedisPool");
int count = 0;
while (true) {
Jedis jedis = pool.getResource();
jedis.get("keyName");
Transaction tx = jedis.multi();
tx.incr("keyName");
tx.expire("keyName", 10);
tx.exec();
jedis.close();
System.out.println("jedisPool active count:" + pool.getNumActive());
System.out.println("jedisPool idle count:" + pool.getNumIdle());
Thread.sleep(10);
}
}
示例4: transactionNoPipeline
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
public void transactionNoPipeline() {
Jedis jedis = pool.getResource();
try {
Transaction tx = jedis.multi();
for (int i = 0; i < rowCount; i++) {
String key = RandomStringUtils.randomAlphabetic(8);
tx.set(key, RandomStringUtils.randomNumeric(5));
tx.expire(key, 5 * 60);
}
tx.exec();
} catch (Exception e) {
pool.returnResource(jedis);
}
}
示例5: add
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
public void add(String key, String value) {
if(fake) {
ConcurrentLinkedQueue<String> stack = circularStackMap.get(key);
if(stack==null) {
synchronized(circularStackMap) {
stack = circularStackMap.get(key);
if(stack==null) {
stack = new ConcurrentLinkedQueue<String>();
circularStackMap.put(key, stack);
}
}
}
stack.add(value);
while(stack.size() > capacity) {
stack.poll();
}
} else {
try(Jedis jedis = jedisPool.getResource()) {
Transaction transaction = jedis.multi();
transaction.lpush(key, value);
transaction.ltrim(key, 0, capacity); // right away trim to capacity so that we can pretend it is a circular list
transaction.expire(key, expireInSeconds); // don't keep the data forever
transaction.exec();
} catch (JedisException e) {
// make sure we can find back jedis related stuff in kibana
throw new IllegalStateException("problem connecting to jedis", e);
}
}
}
示例6: increment
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
public void increment(String key, int windowInSecond) throws Exception {
long currentMs = System.currentTimeMillis();
long maxScoreMs = currentMs - windowInSecond * 1000;
Transaction redis = jedisPool.getResource().multi();
redis.zremrangeByScore(key, 0, maxScoreMs);
redis.zadd(key, currentMs, currentMs + "-" + Math.random()); //keep members unique
redis.expire(key, windowInSecond);
redis.exec();
}
示例7: onFailure
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
@Override
protected boolean onFailure(Map<String, Response<Long>> storedValue, Transaction jedisMulti) {
if (storedValue.get(Const.REDIS_SERVICE_CAPACITY_DAILY_TTL).get() < 0) {
String serviceId = requestInfo.getServiceId();
String serviceQuota = ehcacheFactory.getServiceInfoCache()
.get(serviceId).getDailyCapacity();
jedisMulti.hset(Const.REDIS_SERVICE_CAPACITY_DAILY, serviceId, serviceQuota);
jedisMulti.expire(Const.REDIS_SERVICE_CAPACITY_DAILY, Const.SECOND_OF_DAY);
return true;
}
return false;
}
示例8: bumpTimeout
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
private void bumpTimeout() {
try (Jedis jedis = sessionManager.jedisPool.getResource()) {
Transaction transaction = jedis.multi();
transaction.expire(sessionId, maxInactiveInterval);
transaction.expire(sessionId + CREATED_FIELD, maxInactiveInterval);
transaction.exec();
}
}
示例9: setAndExpireWithTran
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
public void setAndExpireWithTran(){
Jedis jedis = getResource();
try {
Transaction transaction = jedis.multi();
transaction.set("x", Long.valueOf(count.incrementAndGet()).toString());
transaction.expire("x", 600);
transaction.exec();
}finally {
returnResource(jedis);
}
}