本文整理匯總了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"));
}