import java.util import java.util.Date import demo.spark.streaming.utils.RedisClient import org.junit.{After, Before, Test} import redis.clients.jedis.{Jedis, SortingParams} /** * Created by cao on 16-3-25. */ class RedisTest{ private var redis: Jedis = null @Before def init(): Unit ={ redis = RedisClient.pool.getResource } @Test def hello(): Unit ={ redis.set("name","cao") println(redis.get("name")) redis.append("name"," Xinyan") println(redis.get("name")) redis.del("name") println(redis.get("name")) redis.mset("name", "cao", "age", "14") print(redis.mget("name","age")) } @Test def testKey(): Unit ={ println(redis.exists("name")) } @Test def testString(): Unit ={ redis.set("cao","hello world") println(redis.get("cao")) redis.setex("cao",1,"nihao") redis.mset("lis","cao","hello","hee") println(redis.get("cao")) println(redis.pttl("cao")) println(redis.getrange("cao",0,1)) println(redis.del("cao","lis")) println(redis.exists("cao")) } @Test def testList(): Unit ={ redis.del("msg") redis.lpush("msg","this is test1") redis.lpush("msg","test2") redis.lpush("msg","test222") redis.lpush("msg", "vector"); redis.lpush("msg", "ArrayList"); redis.lpush("msg", "LinkedList"); println(redis.llen("msg")) // printList(redis.sort("msg")) val res = redis.lrange("msg",0,-1) printList(res) println(redis.lset("msg",2, "override")) println(redis.lindex("msg", 2)) println(redis.lrem("msg",2,"ok")) println(redis.ltrim("msg",0,1)) println(redis.lpop("msg")) printList(redis.lrange("msg",0,-1)) } @Test def testSet(): Unit ={ redis.sadd("myset","TreeSet1") redis.sadd("myset","TreeSet") redis.sadd("myset","55") printSet(redis.smembers("myset")) println(redis.sismember("myset","55")) // count println(redis.scard("myset")) println(redis.srem("myset","55")) println(redis.sismember("myset","55")) println(redis.spop("myset")) println(redis.smembers("myset")) redis.sadd("sets1", "HashSet1") redis.sadd("sets1", "SortedSet1") redis.sadd("sets1", "TreeSet") redis.sadd("sets2", "55") redis.sadd("sets2", "SortedSet1") redis.sadd("sets2", "TreeSet1") println(redis.sinter("myset","set1")) println(redis.sunion("myset","set2")) println(redis.sdiff("myset","set1")) } @Test def sortedSet()={ redis.zadd("ha",199,"ali") redis.zadd("ha",999,"wuha") redis.zadd("ha",666,"deff g") redis.zadd("ha",867,"Aming") redis.zadd("ha",67,"cao xinyan") printSet(redis.zrange("ha",0,-1)) println(redis.zrevrange("ha",0,-1)) redis.zadd("ha", 10.1, "hello") redis.zadd("ha", 10.0, ":") redis.zadd("ha", 9.0, "zset") redis.zadd("ha", 11.0, "zset!") println(redis.zcard("ha")) println(redis.zscore("ha","zset")) println(redis.zrem("ha","zset")) println(redis.zcount("ha",9.5,999)) println(redis.zrange("ha",0,-1)) } @Test def testHash(): Unit ={ val map:java.util.Map[String, String] = new util.HashMap[String, String]() map.put("name","cao") map.put("age","2") map.put("sex","male") redis.hmset("info",map) printList(redis.hmget("info","name","age")) println(redis.hlen("info")) println(redis.hexists("info","name")) printSet(redis.hkeys("info")) printList(redis.hvals("info")) printMap(redis.hgetAll("info")) } @Test def testOther(): Unit ={ printSet(redis.keys("*")) println(redis.del("set1")) redis.setex("set2",2,"ddd") println(redis.ttl("set2")) println(redis.rename("set2","set3")) println(redis.get("set2")) } @Test def testSort(): Unit ={ redis.del("a") redis.rpush("a", "1") redis.lpush("a", "6") redis.lpush("a", "3") redis.lpush("a", "9") printList(redis.sort("a")) } @Test def testUnUsePipeline(): Unit ={ val start = new Date().getTime for (i <- 0 until 10000) { redis.set("age1"+i,i+"") redis.get("age1"+i) } val end = new Date().getTime println("cost:"+(end - start)) } @Test def testSort1(): Unit ={ redis.lpush("mylist", "1") redis.lpush("mylist", "4") redis.lpush("mylist", "6") redis.lpush("mylist", "3") redis.lpush("mylist", "0") val sortingParameters = new SortingParams sortingParameters.desc() sortingParameters.limit(0,2) printList(redis.sort("mylist",sortingParameters)) } @Test def testSort2(): Unit ={ redis.del("user:66", "user:55", "user:33", "user:22", "user:11", "userlist") redis.lpush("userlist", "33") redis.lpush("userlist", "22") redis.lpush("userlist", "55") redis.lpush("userlist", "11") redis.hset("user:66", "name", "66") redis.hset("user:55", "name", "55") redis.hset("user:33", "name", "33") redis.hset("user:22", "name", "79") redis.hset("user:11", "name", "24") redis.hset("user:11", "add", "beijing") redis.hset("user:22", "add", "shanghai") redis.hset("user:33", "add", "guangzhou") redis.hset("user:55", "add", "chongqing") redis.hset("user:66", "add", "xi'an") val sortingParameters = new SortingParams sortingParameters.get("user:*->name") sortingParameters.get("user:*->add") sortingParameters.by("user:*->name") sortingParameters.get("#") printList(redis.sort("userlist", sortingParameters)) } @Test def testSort3(): Unit ={ redis.del("tom:friend:list", "score:uid:123", "score:uid:456", "score:uid:789", "score:uid:101", "uid:123", "uid:456", "uid:789", "uid:101") redis.sadd("tom:friend:list", "123") redis.sadd("tom:friend:list", "456") redis.sadd("tom:friend:list", "789") redis.sadd("tom:friend:list", "101") redis.set("score:uid:123", "1000") redis.set("score:uid:456", "6000") redis.set("score:uid:789", "100") redis.set("score:uid:101", "5999") redis.set("uid:123", "{'uid':123,'name':'lucy'}") redis.set("uid:456", "{'uid':456,'name':'jack'}") redis.set("uid:789", "{'uid':789,'name':'jay'}") redis.set("uid:101", "{'uid':101,'name':'jolin'}") val sortingParameters = new SortingParams sortingParameters.desc() sortingParameters.get("#") sortingParameters.get("uid:*") sortingParameters.get("score:uid:*") sortingParameters.by("score:uid:*") printList(redis.sort("tom:friend:list", sortingParameters)) } @Test def testSort5(): Unit ={ redis.lpush("mylist", "1") redis.lpush("mylist", "4") redis.lpush("mylist", "6") redis.lpush("mylist", "3") redis.lpush("mylist", "0") var sortingParameters = new SortingParams sortingParameters.desc() // sortingParameters.alpha() sortingParameters.limit(0,2) println(redis.sort("mylist",sortingParameters)) redis.sadd("tom:friend:list", "123") redis.sadd("tom:friend:list", "456") redis.sadd("tom:friend:list", "789") redis.sadd("tom:friend:list", "101") redis.set("score:uid:123", "1000") redis.set("score:uid:456", "6000") redis.set("score:uid:789", "100") redis.set("score:uid:101", "5999") redis.set("uid:123", "{'uid':123,'name':'lucy'}") redis.set("uid:456", "{'uid':456,'name':'jack'}") redis.set("uid:789", "{'uid':789,'name':'jay'}") redis.set("uid:101", "{'uid':101,'name':'jolin'}") sortingParameters = new SortingParams sortingParameters.desc() sortingParameters.get("#") sortingParameters.by("score:uid:*") println(redis.sort("tom:friend:list",sortingParameters,"tom:friend:list")) } @Test def testPage(): Unit ={ redis.lpush("mylist", "comment1") redis.lpush("mylist", "comment2") redis.lpush("mylist", "comment3") redis.lpush("mylist", "3") redis.lpush("mylist", "0") redis.lpush("mylist", "6") redis.lpush("mylist", "2") redis.lpush("mylist", "ff") var page = 0 val pageSize = 4 for (i <- 0 until pageSize) { val start = page * pageSize val end = start + pageSize println(redis.lrange("mylist",start,end)) page+=1 } } @Test def testDB(): Unit ={ println(redis.dbSize()) println(redis.randomKey()) } @After def close(): Unit ={ redis.close() } def printList(res: java.util.List[String]): Unit ={ for (i <- 0 until res.size) println(res.get(i)) } def printSet(res: java.util.Set[String])={ val int = res.iterator() while (int.hasNext){ println(int.next) } } def printMap(map: java.util.Map[String, String]): Unit ={ val kset = map.keySet().iterator() while (kset.hasNext){ val key = kset.next() println(s"${key} -> ${map.get(key)}") } } }
jedis usage examples
最新推荐文章于 2023-06-28 14:48:18 发布