本文整理汇总了Java中redis.clients.jedis.Transaction.set方法的典型用法代码示例。如果您正苦于以下问题:Java Transaction.set方法的具体用法?Java Transaction.set怎么用?Java Transaction.set使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redis.clients.jedis.Transaction
的用法示例。
在下文中一共展示了Transaction.set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
示例2: 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");
}
示例3: execGetResponse
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
@Test
public void execGetResponse() {
Transaction t = jedis.multi();
t.set("foo", "bar");
t.smembers("foo");
t.get("foo");
List<Response<?>> lr = t.execGetResponse();
try {
lr.get(1).get();
fail("We expect exception here!");
} catch (JedisDataException e) {
// that is fine we should be here
}
assertEquals("bar", lr.get(2).get());
}
示例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: 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();
}
}
示例7: 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);
}
}
示例8: 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();
assertNull(results);
}
示例9: testResetStateWhenInWatch
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
@Test
public void testResetStateWhenInWatch() {
jedis.watch("mykey", "somekey");
// state reset : unwatch
jedis.resetState();
Transaction t = jedis.multi();
nj.connect();
nj.auth("foobared");
nj.set("mykey", "bar");
nj.disconnect();
t.set("mykey", "foo");
List<Object> resp = t.exec();
assertNotNull(resp);
assertEquals(1, resp.size());
assertEquals("foo", jedis.get("mykey"));
}
示例10: testTrans
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
public void testTrans() {// 0.304秒
Jedis jedis = new Jedis("120.25.241.144", 6379);
jedis.auth("b840fc02d52404542994");
long start = System.currentTimeMillis();
Transaction tx = jedis.multi();
for (int i = 0; i < 1000; i++) {
tx.set("n" + i, "n" + i);
System.out.println(i);
}
tx.exec();
long end = System.currentTimeMillis();
System.out.println("共花费:" + (end - start) / 1000.0 + "秒");
jedis.disconnect();
try {
Closeables.close(jedis, true);
} catch (IOException e) {
e.printStackTrace();
}
}
示例11: doCallback
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
@Override
public void doCallback(Transaction tx) {
for (Command cmd : redisCommands) {
switch (cmd.getOp()) {
case SET:
tx.set(cmd.getCacheKey(), cmd.getCacheValue());
break;
case MOD:
tx.set(cmd.getCacheKey(), cmd.getCacheValue());
break;
case DEL:
tx.del(cmd.getCacheKey());
break;
case ADD_MEMBERS:
tx.sadd(cmd.getCacheGroupKey(), cmd.getGroupValues());
break;
case DEL_MEMBERS:
tx.srem(cmd.getCacheGroupKey(), cmd.getGroupValues());
break;
case SETS:
tx.mset(cmd.getKeyvalues());
default:
break;
}
}
}
示例12: testTransactions
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
/**
* 一个client发起的事务中的命令可以连续的执行,而中间不会插入其他client的命令。
*/
@Test
@Ignore
public void testTransactions(){
Jedis jedis = new Jedis("localhost");
long start = System.currentTimeMillis();
Transaction tx = jedis.multi();
for(int i = 0; i< COUNTER; i++){
tx.set("t" + i, "t" + i);
// 提示使用JedisTransaction代替
if(i == 100){
System.out.println(jedis.get("t1"));
// 无法读取到,是正确的
//System.out.println(new Jedis("localhost").get("t1"));
}
}
List<Object> results = tx.exec();
long end = System.currentTimeMillis();
logger.info("Tx Set: " + ((end - start)/1000.0) + " seconds");
jedis.close();
System.out.println("results: " + results);
}
示例13: watch
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
@Test
public void watch() throws UnknownHostException, IOException {
jedis.watch("mykey", "somekey");
Transaction t = jedis.multi();
nj.connect();
nj.auth("foobared");
nj.set("mykey", "bar");
nj.disconnect();
t.set("mykey", "foo");
List<Object> resp = t.exec();
if(resp.isEmpty()){
resp = null;
}
assertEquals(null, resp);
assertEquals("bar", jedis.get("mykey"));
// Binary
jedis.watch(bmykey, "foobar".getBytes());
t = jedis.multi();
nj.connect();
nj.auth("foobared");
nj.set(bmykey, bbar);
nj.disconnect();
t.set(bmykey, bfoo);
resp = t.exec();
if(resp.isEmpty()){
resp = null;
}
assertEquals(null, resp);
assertTrue(Arrays.equals(bbar, jedis.get(bmykey)));
}
示例14: unwatch
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
@Test
public void unwatch() throws UnknownHostException, IOException {
jedis.watch("mykey");
String val = jedis.get("mykey");
val = "foo";
String status = jedis.unwatch();
assertEquals("OK", status);
Transaction t = jedis.multi();
nj.connect();
nj.auth("foobared");
nj.set("mykey", "bar");
nj.disconnect();
t.set("mykey", val);
List<Object> resp = t.exec();
assertEquals(1, resp.size());
assertEquals("OK", resp.get(0));
// Binary
jedis.watch(bmykey);
byte[] bval = jedis.get(bmykey);
bval = bfoo;
status = jedis.unwatch();
assertEquals(Keyword.OK.name(), status);
t = jedis.multi();
nj.connect();
nj.auth("foobared");
nj.set(bmykey, bbar);
nj.disconnect();
t.set(bmykey, bval);
resp = t.exec();
assertEquals(1, resp.size());
assertEquals("OK", resp.get(0));
}
示例15: testResetStateWhenInMulti
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
@Test
public void testResetStateWhenInMulti() {
jedis.auth("foobared");
Transaction t = jedis.multi();
t.set("foooo", "barrr");
jedis.resetState();
assertEquals(null, jedis.get("foooo"));
}