public static void main(String[] args) { JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost"); Jedis jedis = null; try { jedis = pool.getResource(); /// ... do stuff here ... for example jedis.set("foo", "bar"); String foobar = jedis.get("foo"); System.out.println("foobar=" + foobar); jedis.zadd("sose", 0, "car"); jedis.zadd("sose", 0, "bike"); Set<String> sose = jedis.zrange("sose", 0, -1); jedis.watch("foo"); Transaction t = jedis.multi(); t.set("foo", "bar"); t.exec(); } finally { if (jedis != null) { jedis.close(); } } /// ... when closing your application: pool.destroy(); // Jedis jedis = new Jedis("localhost"); // //jedis.set("foo", "bar"); // String value = jedis.get("foo"); // System.out.println("value=" + value); }
public void delete(String id) { RedisCachePool pool = null; Jedis jedis = null; RedisDao rd = null; try { pool = redisCacheManager.getRedisPoolMap().get(RedisDataBaseType.defaultType.toString()); jedis = pool.getResource(); Object note = RedisDao.getBean("Note:" + id, Note.class, jedis); if (null != note) { // 查询之后开启事物 Transaction transation = jedis.multi(); rd = new RedisDao(transation); rd.delSingleDataFromRedis(note, rd.getBeanField(note)); /* 处理之后的数据库sql日志处理 */ String logs = "delete from tcnote where note_id=" + id; rd.pubishLog(logs); rd.log(logs); transation.exec(); } } catch (Exception e) { log.error(" delete(String id) 删除失败!" + e.getLocalizedMessage()); } finally { log.info("回收jedis连接"); pool.returnResource(jedis); } }
public void insert(Note note) { RedisCachePool pool = null; Jedis jedis = null; RedisDao rd = null; try { pool = redisCacheManager.getRedisPoolMap().get(RedisDataBaseType.defaultType.toString()); jedis = pool.getResource(); // 获取redis里面最大的主键值 Set<String> sortKey = RedisDao.getRevrangeSortSet("Note:sort:noteId", 0, 0, jedis); for (String id : sortKey) { // 新增的主键赋值 note.setNoteId(Integer.parseInt(id) + 1); break; } Transaction transation = jedis.multi(); rd = new RedisDao(transation); BeanField beanField = rd.getBeanField(note); // 插入新增的 rd.insertSingleDataToredis(note, beanField); /* 处理之后的数据库sql日志处理 */ String logs = insertSql(note); rd.pubishLog(logs); rd.log(logs); transation.exec(); } catch (Exception e) { log.error(" insert(Note note) 失败!" + e.getLocalizedMessage()); } finally { log.info("回收insert==>jedis连接"); pool.returnResource(jedis); } }
public void transactionNoPipeline() { Jedis jedis = pool.getResource(); try { Transaction tx = jedis.multi(); for (int i = 0; i < rowCount; i++) { String key = RandomStringUtils.randomAlphabetic(8); tx.set(key, RandomStringUtils.randomNumeric(5)); tx.expire(key, 5 * 60); } tx.exec(); } catch (Exception e) { pool.returnResource(jedis); } }
protected <T> T withRedisTransaction(Function<Transaction, T> r) { Jedis redis = getRedis(); Transaction transaction = null; try { transaction = redis.multi(); T retval = r.apply(transaction); transaction.exec(); transaction = null; return retval; } finally { rollback(transaction); redis.close(); } }
protected void withRedisTransaction(Consumer<Transaction> r, Consumer<Jedis> onOk) { Jedis redis = getRedis(); Transaction transaction = null; try { transaction = redis.multi(); r.accept(transaction); transaction.exec(); transaction = null; if (onOk != null) { onOk.accept(redis); } } finally { rollback(transaction); redis.close(); } }
@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; } } }
/** 更新直接调用删除,然后再插入 */ public void update(Note newNote) { RedisCachePool pool = null; Jedis jedis = null; RedisDao rd = null; try { pool = redisCacheManager.getRedisPoolMap().get(RedisDataBaseType.defaultType.toString()); jedis = pool.getResource(); // 获取原来redis里面存储的note Object orldNote = RedisDao.getBean("Note:" + newNote.getNoteId(), Note.class, jedis); if (null != orldNote) { // 查询之后开启事物 Transaction transation = jedis.multi(); rd = new RedisDao(transation); BeanField beanField = rd.getBeanField(orldNote); // // 先删除原来的 // rd.delSingleDataFromRedis(orldNote, beanField); // // 再插入新修改的note // rd.insertSingleDataToredis(newNote, beanField); rd.updateSingleFromToredis(orldNote, newNote, beanField); /* 处理之后的数据库sql日志处理 */ String logs = genSql(newNote); rd.pubishLog(logs); rd.log(logs); transation.exec(); } } catch (Exception e) { log.error(" update(Note note) 失败!" + e.getLocalizedMessage()); } finally { // log.info("回收jedis连接"); pool.returnResource(jedis); } }
private void rollback(Transaction transaction) { if (transaction != null) { transaction.discard(); } }