標籤: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 彈出棧頂資料 5System.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開始,再賦值給list01System.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 5OK127.0.0.1:6380>
則程式運行結果為:
modify
main retValue--------- false
4.主從複製
6379
127.0.0.1:6379> info replication# Replicationrole:masterconnected_slaves:1slave0:ip=127.0.0.1,port=6380,state=online,offset=232636,lag=1master_repl_offset:232636repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:232609repl_backlog_histlen:28127.0.0.1:6379>
6380
127.0.0.1:6380> info replication# Replicationrole:slavemaster_host:127.0.0.1master_port:6379master_link_status:upmaster_last_io_seconds_ago:5master_sync_in_progress:0slave_repl_offset:232650slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:232718127.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" }}
Redis java操作用戶端