本文整理匯總了Java中redis.clients.jedis.Transaction.exec方法的典型用法代碼示例。如果您正苦於以下問題:Java Transaction.exec方法的具體用法?Java Transaction.exec怎麽用?Java Transaction.exec使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類redis.clients.jedis.Transaction
的用法示例。
在下文中一共展示了Transaction.exec方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: hincrby
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
@Test public void hincrby() {
// GIVEN
Transaction tr = this.jedis.multi();
// WHEN
Response<Long> result1 = tr.hincrBy(KEY, FIELD, 3);
Response<Long> result2 = tr.hincrBy(KEY, FIELD, 4);
List<Object> results = tr.exec();
// THEN
String value = this.jedis.hget(KEY, FIELD);
assertEquals("7", value);
assertEquals(2, results.size());
assertEquals(3, (long) results.get(0));
assertEquals(7, (long) results.get(1));
assertEquals(3, (long) result1.get());
assertEquals(7, (long) result2.get());
}
示例2: testResetStateWithFullyExecutedTransaction
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
@Test
public void testResetStateWithFullyExecutedTransaction() {
Jedis jedis2 = new Jedis(jedis.getClient().getHost(), jedis.getClient().getPort());
jedis2.auth("foobared");
Transaction t = jedis2.multi();
t.set("mykey", "foo");
t.get("mykey");
List<Object> resp = t.exec();
assertNotNull(resp);
assertEquals(2, resp.size());
jedis2.resetState();
jedis2.close();
}
示例3: transactionResponseWithError
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
@Test
public void transactionResponseWithError() {
Transaction t = jedis.multi();
t.set("foo", "bar");
Response<Set<String>> error = t.smembers("foo");
Response<String> r = t.get("foo");
List<Object> l = t.exec();
assertEquals(JedisDataException.class, l.get(1).getClass());
try {
error.get();
fail("We expect exception here!");
} catch (JedisDataException e) {
// that is fine we should be here
}
assertEquals(r.get(), "bar");
}
示例4: select
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
@Test
public void select() {
jedis.select(1);
jedis.set("foo", "bar");
jedis.watch("foo");
Transaction t = jedis.multi();
t.select(0);
t.set("bar", "foo");
Jedis jedis2 = createJedis();
jedis2.select(1);
jedis2.set("foo", "bar2");
List<Object> results = t.exec();
if(results.isEmpty()){
results = null;
}
assertNull(results);
}
示例5: testCloseable
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
@Test
public void testCloseable() throws IOException {
// we need to test with fresh instance of Jedis
Jedis jedis2 = new Jedis(hnp.getHost(), hnp.getPort(), 500);
jedis2.auth("foobared");
Transaction transaction = jedis2.multi();
transaction.set("a", "1");
transaction.set("b", "2");
transaction.close();
try {
transaction.exec();
fail("close should discard transaction");
} catch (JedisDataException e) {
assertTrue(e.getMessage().contains("EXEC without MULTI"));
// pass
}
}
示例6: testExec
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
@Test
public void testExec() throws Exception {
int rnd = ThreadLocalRandom.current().nextInt(0, presetElements.size());
String key = presetElementKeys.get(rnd);
String value = String.valueOf(presetElements.get(key));
Transaction transaction = jedis.multi();
Snapshot snapshot = commandTracker.snapshot();
Snapshot discardSnapshot = discardTracker.snapshot();
Snapshot txsnapshot = execTracker.snapshot();
txsnapshot.increment();
Response<Long> added = transaction.sadd(key, value);
transaction.exec();
assertEquals(1, (long) added.get());
txsnapshot.validate();
snapshot.validate();
discardSnapshot.validate();
}
示例7: deleteMulti
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
@Override
public boolean deleteMulti(String... keys) {
Jedis jedis = null;
try {
jedis = pool.getResource();
Transaction trans = jedis.multi();
for (String key : keys) {
trans.del(key.getBytes());
}
trans.exec();
pool.returnResource(jedis);
} catch (Exception e) {
LOGGER.warn("Failed to delete key from cache {0}", keys, e);
pool.returnBrokenResource(jedis);
return false;
}
return true;
}
示例8: getAndRemZRange
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
public Set<String> getAndRemZRange(String key, long score) {
Jedis jedis = null;
try {
jedis = pool.getResource();
Transaction trans = jedis.multi();
trans.zrangeByScore(key.getBytes(), MIN_INF, SafeEncoder.encode(String.valueOf(score)));
trans.zremrangeByScore(key.getBytes(), MIN_INF, SafeEncoder.encode(String.valueOf(score)));
List<Object> response = trans.exec();
Set<byte[]> data = (Set<byte[]>) response.get(0);
Set<String> members = new LinkedHashSet<>(data.size());
for (byte[] d : data) {
members.add(new String(d));
}
pool.returnResource(jedis);
return members;
} catch (Exception e) {
LOGGER.warn("Failed to get zrem keys from cache {0}:{1}", key, score, e);
pool.returnBrokenResource(jedis);
throw e;
}
}
示例9: doRelease
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
@Override
protected void doRelease(String token) {
jedisClient.watch(lockName);
String currentToken = jedisClient.get(lockName);
if (currentToken == null){
jedisClient.unwatch();
return;
}
if (currentToken.equals(token)) {
Transaction t = jedisClient.multi();
t.del(lockName);
t.exec();
} else {
jedisClient.unwatch();
}
}
示例10: doExtend
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
@Override
protected boolean doExtend(final String token, long additionalTime){
jedisClient.watch(lockName);
String currentToken = jedisClient.get(lockName);
if (currentToken == null){
jedisClient.unwatch();
return false;
}
if (token.equals(currentToken)) {
long expiration = jedisClient.pttl(lockName);
if (expiration < 0) {
jedisClient.unwatch();
return false;
}
Transaction t = jedisClient.multi();
t.pexpire(lockName, expiration + additionalTime);
List response = t.exec();
return (!response.isEmpty()) && ((Long)response.get(0) == 1);
}
jedisClient.unwatch();
return false;
}
示例11: updateQueuedJob
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
@Override
public synchronized void updateQueuedJob(int jobId, JSONObject newJob) {
try (Jedis jedis = jedisPool.getResource()) {
jedis.select(redisDBIndex);
if (jedis.zcard(queue) > 0) {
Optional<Double> score = null;
Set<Tuple> tuples = jedis.zrangeByScoreWithScores(queue, 0, Double.MAX_VALUE);
String old = null;
for(Tuple data : tuples) {
JSONObject detail = JSONObject.parseObject(data.getElement());
int id = detail.getIntValue(ScheduledConstants.JOB_ID);
if(id == jobId) {
score = Optional.ofNullable(data.getScore());
old = data.getElement();
break;
}
}
if(score.isPresent()){
Transaction tx = jedis.multi();
tx.zrem(queue, old);
tx.zadd(queue, score.get(), newJob.toJSONString());
tx.exec();
}
}
}
}
示例12: execute
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
public void execute(TransactionCallBack rc) {
Jedis jedis = null;
Transaction transaction;
List<Object> object = null;
try {
jedis = RedisPool.getJedis();
transaction = jedis.multi();
rc.execute(transaction);
object = transaction.exec();
} catch (Exception e) {
log.error("執行redis操作異常" + e.getLocalizedMessage());
}
finally {
RedisPool.returnJedis(jedis);
}
}
示例13: 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();
}
}
示例14: exec
import redis.clients.jedis.Transaction; //導入方法依賴的package包/類
public List<Object> exec(long logIndex, Transaction transaction) {
String flag = getClassName() + ".exec";
List<Object> res = null;
Jedis jds = null;
try {
jds = jedisPool.getResource();
res = transaction.exec();
jedisPool.returnResource(jds);
return res;
} catch (Exception e) {
jedisPool.returnBrokenResource(jds);
FRCLogger.getInstance().warn(logIndex, flag, "exception:", e);
res = null;
return res;
}
}
示例15: 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);
}
}