标签:ast 成功 127.0.0.1 set boolean 进程 inf rem for
Jedis常用操作
1.测试连通性
Jedis jedis = new Jedis("192.168.1.201",6380,10000); System.out.println(jedis.ping());
控制台打印
PONG
2. 5+1操作
5中数据类型的操作:string list set hash zset
String
String 没什么好说的,最长用的一种
Jedis jedis = new Jedis("192.168.1.201",6379); System.out.println(jedis.ping()); jedis.set("k1", "v1"); jedis.set("k2", "v2"); jedis.set("k3", "v3"); System.out.println(jedis.get("k1")); //"v1"
List
lpush
jedis.lpush("list01", "1","2","3","4","5"); //lpush为先进后出 可以这么理解,从左侧开始入栈 System.out.println(jedis.lrange("list01", 0, -1)); //[5, 4, 3, 2, 1]
rpush
jedis.rpush("list02", "1","2","3","4","5"); //rpush为先进先出 System.out.println(jedis.lrange("list02", 0, -1)); //[1, 2, 3, 4, 5]
lpop
System.out.println(jedis.lpop("list01")); //lpop 弹出栈顶数据 5 System.out.println(jedis.lrange("list01", 0, -1)); //[4, 3, 2, 1]
rpop
System.out.println(jedis.rpop("list02")); //rpop 弹出栈底数据 System.out.println(jedis.lrange("list02", 0, -1)); //[1, 2, 3, 4]
lindex
System.out.println(jedis.lrange("list02", 0, -1)); System.out.println(jedis.lindex("list02", 0)); //LINDEX 按照索引下标获得元素(从上到下)
llen
System.out.println(jedis.lrange("list02", 0, -1)); //[1, 2, 3, 4] System.out.println(jedis.llen("list02")); //LLEN 获取长度 4
lrem
jedis.rpush("list03", "1","1","1","2","2","2","3","3","3","4","4","4","5"); jedis.lrem("list03", 2, "3"); //删除 2 个 "3" System.out.println(jedis.lrange("list03", 0, -1)); //[1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 5]
ltrim
jedis.lpush("list04", "1","2","3","4","5","6","7","8"); System.out.println(jedis.lrange("list04", 0, -1)); //[8, 7, 6, 5, 4, 3, 2, 1] jedis.ltrim("list04", 3, 5); //截取下标为3-5的数值,下表从0开始,再赋值给list01 System.out.println(jedis.lrange("list04", 0, -1)); //[5, 4, 3]
rpoplpush
jedis.lpush("list01", "1","2","3","4","5"); System.out.println(jedis.lrange("list01", 0, -1)); jedis.lpush("list02", "1","2","3"); System.out.println(jedis.lrange("list02", 0, -1)); jedis.rpoplpush("list01", "list02");//将一个数据弹出,使用rpop弹出的底部数据,lpush放入另一个中 System.out.println(jedis.lrange("list01", 0, -1)); //[5, 4, 3, 2] System.out.println(jedis.lrange("list02", 0, -1)); //[1, 3, 2, 1]
lset
jedis.lset("list01", 1, "x"); //lset key index value 对index位置的进行赋值value [5, x, 3, 2] System.out.println(jedis.lrange("list01", 0, -1));
linsert
System.out.println(jedis.lrange("list01", 0, -1)); //[5, x, 3, 2] jedis.linsert("list01", LIST_POSITION.BEFORE, "x", "java"); //在 x 之前插入一个 java ,如果指定值有重复,则找到的是第一个 System.out.println(jedis.lrange("list01", 0, -1));
3.事务提交
日常:
Transaction transaction = jedis.multi(); transaction.set("k4", "v44"); transaction.set("k5", "v55"); // transaction.exec(); transaction.discard();
加锁:
public class TestTx { public boolean transMethord() { Jedis jedis = new Jedis("192.168.1.201", 6380); int balance; // 余额 int debt; // 欠额 int amtToSubtract = 10; // 实刷额度 jedis.set("balance", "100"); jedis.set("debt", "0"); jedis.watch("balance");
//Thread.sleep(7000); balance = Integer.parseInt(jedis.get("balance")); if (balance < amtToSubtract) { jedis.unwatch(); System.out.println("modify"); return false; } else { System.out.println("*********transaction***********"); Transaction transaction = jedis.multi(); transaction.decrBy("balance", amtToSubtract); transaction.incrBy("debt", amtToSubtract); transaction.exec(); balance = Integer.parseInt(jedis.get("balance")); debt = Integer.parseInt(jedis.get("debt")); System.out.println("*******************" + balance); System.out.println("*******************" + debt); return true; } } /** * 通俗的讲,watch命令就是标记一个键,如果标记了一个键, * 在提交事务前如果该键被别人修改过,那事务就会失败,这种情况通常可以在程序中重新再尝试一次。 * 首先标记了键balance,然后检查余额是否足够,不足就取消标记,并不做扣减; * 足够的话,就启动事务进行更新操作,如果在此期间键balance被其他人修改,那在提交事务(执行exec)时就会报错, * 程序中通常可以捕获这类错误在重新执行一次,直到成功。 * @param args */ public static void main(String[] args) { TestTx test = new TestTx(); boolean retValue = test.transMethord(); System.out.println("main retValue--------- " + retValue); } }
如果在事务执行期间有进程修改了watch 的key ,则回滚事务
修改程序 打开注释
Thread.sleep(7000);
在期间 修改balance的值
127.0.0.1:6380> set balance 5
OK
127.0.0.1:6380>
则程序运行结果为:
modify
main retValue--------- false
4.主从复制
6379
127.0.0.1:6379> info replication # Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6380,state=online,offset=232636,lag=1 master_repl_offset:232636 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:232609 repl_backlog_histlen:28 127.0.0.1:6379>
6380
127.0.0.1:6380> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:5 master_sync_in_progress:0 slave_repl_offset:232650 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:232718 127.0.0.1:6380>
public class TestAPI { public static void main(String[] args) { Jedis jedis_M = new Jedis("192.168.1.201", 6379); Jedis jedis_S = new Jedis("192.168.1.201", 6380); jedis_M.set("k1", "v1"); String v1 = jedis_S.get("k1"); System.out.println(v1); //"v1" } }
标签:ast 成功 127.0.0.1 set boolean 进程 inf rem for
原文地址:http://www.cnblogs.com/wq3435/p/6124034.html