本文整理汇总了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);
}
}