Java Code Examples for redis.clients.jedis.Transaction#set()
The following examples show how to use
redis.clients.jedis.Transaction#set() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransactionCommandsTest.java From cachecloud with Apache License 2.0 | 6 votes |
@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")); }
Example 2
Source File: DefaultRedisTransactionCallback.java From easyooo-framework with Apache License 2.0 | 6 votes |
@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; } } }
Example 3
Source File: RedisExample.java From java-platform with Apache License 2.0 | 6 votes |
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(); } }
Example 4
Source File: JedisApiTest.java From easyooo-framework with Apache License 2.0 | 6 votes |
/** * 一个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); }
Example 5
Source File: TransactionCommandsTest.java From cachecloud with Apache License 2.0 | 6 votes |
@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 } }
Example 6
Source File: TransactionCommandsTest.java From cachecloud with Apache License 2.0 | 6 votes |
@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(); }
Example 7
Source File: TransactionTest.java From code with Apache License 2.0 | 6 votes |
/** * 乐观锁:watch * */ public static int doubleAccount(Jedis jedis, String userId) { String key = keyFor(userId); while (true) { jedis.watch(key); int value = Integer.parseInt(jedis.get(key)); value *= 2; // 加倍 Transaction tx = jedis.multi(); tx.set(key, String.valueOf(value)); List<Object> res = tx.exec(); if (res != null) { break; // 成功了 } } return Integer.parseInt(jedis.get(key)); // 重新获取余额 }
Example 8
Source File: TransactionCommandsTest.java From cachecloud with Apache License 2.0 | 6 votes |
@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); }
Example 9
Source File: TransactionCommandsTest.java From cachecloud with Apache License 2.0 | 6 votes |
@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()); }
Example 10
Source File: TransactionCommandsTest.java From cachecloud with Apache License 2.0 | 6 votes |
@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"); }
Example 11
Source File: TaskLock.java From javabase with Apache License 2.0 | 6 votes |
/** * 方案二: * 会重复两次回去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); } }
Example 12
Source File: TaskLock.java From javabase with Apache License 2.0 | 6 votes |
/** * 方案一的坏处: * 假如在极端情况下,可能出现集群各个服务器同时执行到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(); } }
Example 13
Source File: TransactionCommandsTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void testResetStateWhenInMulti() { jedis.auth("foobared"); Transaction t = jedis.multi(); t.set("foooo", "barrr"); jedis.resetState(); assertEquals(null, jedis.get("foooo")); }
Example 14
Source File: TransactionCommandsTest.java From cachecloud with Apache License 2.0 | 5 votes |
@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)); }
Example 15
Source File: TransactionCommandsTest.java From cachecloud with Apache License 2.0 | 5 votes |
@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(); 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(); assertEquals(null, resp); assertTrue(Arrays.equals(bbar, jedis.get(bmykey))); }
Example 16
Source File: RedisEasyTest.java From javabase with Apache License 2.0 | 5 votes |
private static void transation(Transaction transaction) { for (int i = 0; i < KEY_COUNT; i++) { transaction.set("transaction" + i, i + ""); } transaction.exec(); }
Example 17
Source File: JedisSentinelPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@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"); jedis.close(); jedis2 = pool.getResource(); assertTrue(jedis == jedis2); assertEquals("jedis", jedis2.get("hello")); } catch (JedisConnectionException e) { if (jedis2 != null) { jedis2 = null; } } finally { jedis2.close(); pool.destroy(); } }
Example 18
Source File: RedisSessionIdStore.java From datashare with GNU Affero General Public License v3.0 | 5 votes |
@Override public void put(final String sessionId, final String login) { try (Jedis jedis = redis.getResource()) { Transaction transaction = jedis.multi(); transaction.set(sessionId, login); transaction.expire(sessionId, this.ttl); transaction.exec(); } }
Example 19
Source File: RedisUsers.java From datashare with GNU Affero General Public License v3.0 | 5 votes |
void createUser(HashMapUser user) { try (Jedis jedis = redis.getResource()) { Transaction transaction = jedis.multi(); transaction.set(user.login(), user.toJson()); transaction.expire(user.login(), this.ttl); transaction.exec(); } }
Example 20
Source File: BossManager.java From gameserver with Apache License 2.0 | 4 votes |
/** * @param addProgress * @param addTotalUsers * @param key * @param jedisDB * @param boss * @return */ private Boss storeProgressInRedis(int addProgress, int addTotalUsers, String key, Jedis jedisDB, Boss boss) { boolean success = false; while ( !success ) { jedisDB.watch(key); String oldInstance = jedisDB.get(key); boss = Boss.fromString(oldInstance); int newProgress = boss.getProgress()+addProgress; if ( newProgress > boss.getTotalProgress() ) { newProgress = boss.getTotalProgress(); } int newTotalUsers = boss.getTotalUsers()+addTotalUsers; boss.setProgress(newProgress); boss.setTotalUsers(newTotalUsers); if ( newProgress >= boss.getTotalProgress() ) { boss.setBossStatusType(BossStatus.SUCCESS); logger.debug("The boss is killed."); } else { int currentSeconds = (int)(System.currentTimeMillis()/1000); if ( currentSeconds > boss.getEndSecond() ) { logger.debug("The boss is timedout"); boss.setBossStatusType(BossStatus.TIMEOUT); } } //System.in.read(); //sync boss info Transaction trans = jedisDB.multi(); trans.set(key, boss.toString()); List<Object> status = trans.exec(); if ( status != null && status.size()>=1) { success = true; //Reset the expire seconds int endSecond = boss.getEndSecond(); int currentSecond = (int)(System.currentTimeMillis()/1000); int expireSecond = endSecond - currentSecond + 1800; jedisDB.expire(key, expireSecond); break; } else { success = false; } }//while... return boss; }