本文整理汇总了Java中redis.clients.jedis.Jedis.multi方法的典型用法代码示例。如果您正苦于以下问题:Java Jedis.multi方法的具体用法?Java Jedis.multi怎么用?Java Jedis.multi使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redis.clients.jedis.Jedis
的用法示例。
在下文中一共展示了Jedis.multi方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testResetStateWithFullyExecutedTransaction
import redis.clients.jedis.Jedis; //导入方法依赖的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();
}
示例2: testCloseable
import redis.clients.jedis.Jedis; //导入方法依赖的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
}
}
示例3: deleteMulti
import redis.clients.jedis.Jedis; //导入方法依赖的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;
}
示例4: getAndRemZRange
import redis.clients.jedis.Jedis; //导入方法依赖的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;
}
}
示例5: returnResourceShouldResetState
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
@Test
public void returnResourceShouldResetState() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(1);
config.setBlockWhenExhausted(false);
JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");
Jedis jedis = pool.getResource();
try {
jedis.set("hello", "jedis");
Transaction t = jedis.multi();
t.set("hello", "world");
} finally {
jedis.close();
}
Jedis jedis2 = pool.getResource();
try {
assertTrue(jedis == jedis2);
assertEquals("jedis", jedis2.get("hello"));
} finally {
jedis2.close();
}
pool.destroy();
assertTrue(pool.isClosed());
}
示例6: returnResourceShouldResetState
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
@Test
public void returnResourceShouldResetState() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(1);
config.setBlockWhenExhausted(false);
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000,
"foobared", 2);
Jedis jedis = pool.getResource();
Jedis jedis2 = null;
try {
jedis.set("hello", "jedis");
Transaction t = jedis.multi();
t.set("hello", "world");
pool.returnResource(jedis);
jedis2 = pool.getResource();
assertTrue(jedis == jedis2);
assertEquals("jedis", jedis2.get("hello"));
} catch (JedisConnectionException e) {
if (jedis2 != null) {
pool.returnBrokenResource(jedis2);
jedis2 = null;
}
} finally {
if (jedis2 != null) jedis2.close();
pool.destroy();
}
}
示例7: releaseLock
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
/**
* 释放锁
*
* @param lockName 锁的key
* @param identifier 释放锁的标识
*/
public boolean releaseLock(String lockName, String identifier){
Jedis conn = null;
String lockKey = lockPrefix + lockName;
boolean retFlag = false;
ShardedJedis shardedJedis = jedisPool.getResource();
try {
conn = shardedJedis.getShard(lockKey);
while (true) {
// 监视lock,准备开始事务
conn.watch(lockKey);
// 通过前面返回的value值判断是不是该锁,若是该锁,则删除,释放锁
if (identifier.equals(conn.get(lockKey))) {
Transaction transaction = conn.multi();
transaction.del(lockKey);
List<Object> results = transaction.exec();
if (results == null) {
continue;
}
retFlag = true;
}
conn.unwatch();
break;
}
} catch (JedisException e) {
LOGGER.error("releaseLock found exception", e);
} finally {
if (conn != null) {
jedisPool.returnResource(shardedJedis);
}
}
return retFlag;
}