package yz20537.redis_test;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import redis.clients.jedis.Jedis;
public class App {
static Jedis jedis = new Jedis("localhost");
public static void main(String[] args) {
// connect();
// testKey();
// testString();
// testHash();
// testList();
// testSet();
testSortedSet();
jedis.close();
}
static void testKey() {
// set key value --set key and value
jedis.set("name", "wahaha");
// del key -- delete key
jedis.del("name");
// exists key -- if exists
jedis.set("name", "wahaha");
System.out.println(jedis.exists("name"));
// expire aaa 10 -- set the timeout 10s
jedis.expire("name", 10);
System.out.println(jedis.ttl("name"));
// pexpire aaa 5000 -- set timeout 5000mm
jedis.pexpire("name", 1000L);
System.out.println(jedis.ttl("name"));
// expireat aaa 1504771560 --set timeout at unixstamp
jedis.expireAt("name", new Date().getTime() / 1000 + 10L);
System.out.println(jedis.ttl("name"));
// set always live
jedis.persist("name");
System.out.println(jedis.ttl("name"));
// show all keys
System.out.println(jedis.keys("*"));
}
static void testString() {
jedis.flushAll();
// set name zouyufei
jedis.set("name", "wahaha");
// get name
System.out.println(jedis.get("name"));
// mset name zouyufei age 20 (multi set)
jedis.mset("name", "wahaha", "age", "20");
// mget
System.out.println(jedis.mget("name", "age"));
// getrange name 1 3 (index from 0)
System.out.println(jedis.getrange("name", 0, 2));
// setrange name 7 aa (name = name from 0-7 + aa)
jedis.setrange("name", 4, "!#");
System.out.println(jedis.get("name"));
// setex gender 10 male (set gender=male, and timeout=10s)
jedis.setex("gender", 10, "male");
System.out.println(jedis.get("gender") + "\t" + jedis.ttl("gender"));
// setnx name zouyf (set name=zouyf if name doesn't exist)
jedis.setnx("name", "abc");
jedis.setnx("height", "180cm");
System.out.println(jedis.mget("name", "height"));
// msetnx name zouyufei age 20 (multi set when all keys don't exist)
jedis.msetnx("food", "abc", "weight", "100kg");
System.out.println(jedis.mget("food", "weight"));
// strlen name (get name length)
System.out.println(jedis.strlen("name"));
// incr age (age ++)
jedis.incr("age");
System.out.println(jedis.get("age"));
// incrby age 10 (age+=10)
jedis.incrBy("age", 20);
System.out.println(jedis.get("age"));
// decr age (age--)
jedis.decr("age");
System.out.println(jedis.get("age"));
// decrby age 10 (age -=10)
jedis.decrBy("age", 20);
System.out.println(jedis.get("age"));
// append name dtc (name=name+dtc)
jedis.append("food", "fruit");
System.out.println(jedis.get("food"));
}
static void testHash() {
jedis.flushAll();
// hset mac cpu i7
jedis.hset("hash", "cpu", "i7");
// hmset mac cpu i5 memory 10G price 4999 (set multi)
Map<String, String> keyvalues = new HashMap<String, String>();
keyvalues.put("memory", "4GB");
keyvalues.put("disk", "400GB");
keyvalues.put("price", "400");
jedis.hmset("hash", keyvalues);
// hget mac cpu
System.out.println(jedis.hget("hash", "cpu"));
// hmget mac screen memory (get multi )
System.out.println(jedis.hmget("hash", "cpu", "disk"));
// hgetall mac
System.out.println(jedis.hgetAll("hash"));
// hdel mac address screen (delete address and screen from mac)
jedis.hdel("hash", "cpu", "disk");
// hexists mac name
System.out.println(jedis.hexists("hash", "cpu"));
// hincrby mac price 10 (mac's price +=10)
jedis.hincrBy("hash", "price", 100);
// hkeys mac (get all keys in mac)
System.out.println(jedis.hkeys("hash"));
// hlen mac (key count)
System.out.println(jedis.hlen("hash"));
// hsetnx mac cpu i8 (set value only key doesn't exist)
jedis.hsetnx("hash", "cpu", "i8");
// hvals mac (get all values)
System.out.println(jedis.hvals("hash"));
}
static void testList() {
jedis.flushAll();
// lpushx list 6 (if list doesn't exist, do nothing)
jedis.lpushx("list", "a");
System.out.println(jedis.lrange("list", 0, -1));
// lpush list 1 (if list doesn't exist, create the list)
jedis.lpush("list", "a", "b", "c", "d", "e", "f");
System.out.println(jedis.lrange("list", 0, -1));
// lindex list 2 (get the second element)
System.out.println(jedis.lindex("list", 1));
// llen list (get length)
System.out.println(jedis.llen("list"));
// lpop list (get first element from left - head)
System.out.println(jedis.lpop("list"));
System.out.println(jedis.lrange("list", 0, -1));
// lrem list 2 5 (remove 5 twice from head)
jedis.lrem("list", 1, "a");
System.out.println(jedis.lrange("list", 0, -1));
// lset list 0 100 (set the first element from left to 100)
jedis.lset("list", 0, "100");
System.out.println(jedis.lrange("list", 0, -1));
// ltrim list 0 4 (remain from [0,4] elements)
jedis.ltrim("list", 0, 2);
System.out.println(jedis.lrange("list", 0, -1));
// rpoplpush list list (pop first element from right and push to list
// from left)
jedis.rpoplpush("list", "list1");
System.out.println(jedis.lrange("list1", 0, -1));
// rpush list 2 3 4 (push the elements from right)
jedis.rpush("list", "1", "2", "3");
System.out.println(jedis.lrange("list", 0, -1));
// rpushx list 6 (if list doesn't exist, do nothing)
// rpop list (get first element from right - tail)
jedis.rpop("list");
System.out.println(jedis.lrange("list", 0, -1));
}
static void testSet() {
// sadd set 1 2 3 4
jedis.sadd("set", "1", "1", "2", "3", "4", "7", "8");
// smembers set (show all)
System.out.println(jedis.smembers("set"));
// srem set 4 (remove)
jedis.srem("set", "3");
System.out.println(jedis.smembers("set"));
// scard set (show size)
System.out.println(jedis.scard("set"));
// sdiff set set1 (get the difference between set and set1)
jedis.sadd("set1", "1", "5");
System.out.println(String.format("set:%s, set1:%s, diff:%s", jedis.smembers("set"), jedis.smembers("set1"), jedis.sdiff("set", "set1")));
System.out.println(String.format("set:%s, set1:%s, diff:%s", jedis.smembers("set"), jedis.smembers("set1"), jedis.sdiff("set1", "set")));
// sdiffstore diffset set set1
jedis.sdiffstore("setdiff", "set1", "set");
System.out.println(jedis.smembers("setdiff"));
// sinter set set1 (交集)
System.out.println(jedis.sinter("set", "set1"));
// sinterstore interset set set1
jedis.sinterstore("interset", "set", "set1");
System.out.println(jedis.smembers("interset"));
// sunion set set1 (并集)
System.out.println(jedis.sunion("set", "set1"));
// sunionstore unionset set set1
jedis.sunionstore("unionset", "set", "set1");
System.out.println(jedis.smembers("unionset"));
// sismember set 1 (if 1 belongs to set)
System.out.println(jedis.sismember("set", "3"));
// smove set set1 1 (move 1 from set to set1)
jedis.smove("set", "mset", "1");
System.out.println(jedis.smembers("set"));
System.out.println(jedis.smembers("mset"));
// spop set 2 (drop 2 elements randomly from set)
jedis.spop("set", 2);
System.out.println(jedis.smembers("set"));
// srandmember set 2 (get 2 elements randomly from set)
System.out.println(jedis.srandmember("set", 1));
}
static void testSortedSet() {
jedis.flushAll();
// zadd zet 1 a 2 b 3 c (add a,b,c with weight 1 2 3)
jedis.zadd("zet", 90, "cpu");
jedis.zadd("zet", 91, "memory");
jedis.zadd("zet", 92, "disk");
jedis.zadd("zet", 93, "screen");
System.out.println(jedis.zrange("zet", 0, -1));
// zrem zet a
jedis.zrem("zet", "disk");
System.out.println(jedis.zrange("zet", 0, -1));
// zrank zet a (show the index of a order by score asc- from 0)
System.out.println(jedis.zrank("zet", "disk"));
// zrevrank zet2 a (show the index of a order by score desc)
System.out.println(jedis.zrevrank("zet", "cpu"));
// zscore zet1 a(show the score of a)
System.out.println(jedis.zscore("zet", "cpu"));
// zcard zet (size)
System.out.println(jedis.zcard("zet"));
// zcount zet 1 2 (get the count whose score between 1 and 2)
System.out.println(jedis.zcount("zet", 90, 92));
// zincrby zet 10 a (add 10 to the weight of a)
jedis.zincrby("zet", 100, "cpu");
System.out.println(jedis.zscore("zet", "cpu"));
// zinterstore interzet 2 zet zet1 (交集)
jedis.zadd("zet1", 93, "screen");
jedis.zadd("zet1", 99, "mouse");
jedis.zinterstore("interZet", "zet", "zet1");
System.out.println(jedis.zrange("interZet", 0, -1));
// zunionstore unionzet 2 zet zet1(并集)
jedis.zunionstore("unionZet", "zet", "zet1");
System.out.println(jedis.zrange("unionZet", 0, -1));
// zremrangebyscore zet 0 4 (remove elements whose score between 0 and
// 4)
jedis.zremrangeByScore("zet", 0, 100);
// zrevrange zet1 0 1(get the last 2 elements)
System.out.println(jedis.zrevrange("zet", 0, 2));
System.out.println(jedis.zrange("zet", 0, -1));
// 當(dāng)有序集合的所有成員都具有相同的分值時贝咙, 有序集合的元素會根據(jù)成員的字典序(lexicographical ordering)來進(jìn)行排序平斩,
// 而這個命令則可以返回給定的有序集合鍵 key 中慰毅, 值介于 min 和 max 之間的成員惠呼。
// 如果有序集合里面的成員帶有不同的分值, 那么命令返回的結(jié)果是未指定的(unspecified)。
// zrangebylex zet1 [a [c (show the elements whose score between a and
// c's score)]
System.out.println(jedis.zrangeByLex("zet", "[cpu", "[disk"));
// zremrangebylex zet [a [b (remove elements whose score between a and
// b's)
System.out.println("before remove by lex:" + jedis.zrange("zet", 0, -1));
jedis.zremrangeByLex("zet", "[cpu", "[disk");
System.out.println("remove by lex:" + jedis.zrange("zet", 0, -1));
// zlexcount zet1 [a [b (get the count which weight between a and b
// weight )
System.out.println(jedis.zlexcount("zet", "[cpu", "[disk"));
}
}
Redis Java 基本操作
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來羔挡,“玉大人洁奈,你說我怎么就攤上這事〗首疲” “怎么了利术?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長低矮。 經(jīng)常有香客問我印叁,道長,這世上最難降的妖魔是什么军掂? 我笑而不...
- 正文 為了忘掉前任轮蜕,我火速辦了婚禮,結(jié)果婚禮上蝗锥,老公的妹妹穿的比我還像新娘跃洛。我一直安慰自己,他們只是感情好终议,可當(dāng)我...
- 文/花漫 我一把揭開白布汇竭。 她就那樣靜靜地躺著葱蝗,像睡著了一般。 火紅的嫁衣襯著肌膚如雪细燎。 梳的紋絲不亂的頭發(fā)上两曼,一...
- 文/蒼蘭香墨 我猛地睜開眼彪蓬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了捺萌?” 一聲冷哼從身側(cè)響起档冬,我...
- 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎桃纯,沒想到半個月后酷誓,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡态坦,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年盐数,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片伞梯。...
- 正文 年R本政府宣布喻旷,位于F島的核電站生逸,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏且预。R本人自食惡果不足惜槽袄,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望锋谐。 院中可真熱鬧遍尺,春花似錦、人聲如沸涮拗。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至歧蕉,卻和暖如春灾部,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背惯退。 一陣腳步聲響...
- 正文 我出身青樓锁蠕,卻偏偏與公主長得像,于是被迫代替她去往敵國和親懊蒸。 傳聞我的和親對象是個殘疾皇子荣倾,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 一骑丸、Redis 中的字符串 二舌仍、Redis 中的列表 備注:lrange 的首字母 l 是指 left, rpus...
- 啟動數(shù)據(jù)庫 1.打開redis-server2.再打開redis-cli 啟動redis輸入KEYS * 查看是否...