使用RedisTemplate操作Redis數(shù)據(jù)結(jié)構(gòu)

Redis五種數(shù)據(jù)結(jié)構(gòu):

結(jié)構(gòu)類型 存儲(chǔ)的值 結(jié)構(gòu)的獨(dú)寫能力
String 可以是字符串、整數(shù)或者浮點(diǎn)數(shù) 對(duì)整個(gè)字符串或者字符串的其中一部分執(zhí)行操作剑勾;對(duì)象和浮點(diǎn)數(shù)執(zhí)行自增(increment)或者自減(decrement)
Hash 包含鍵值對(duì)的無序散列表 添加丛肮、獲取、移除單個(gè)鍵值對(duì);獲取所有鍵值對(duì)
List 一個(gè)鏈表丁眼,鏈表上的每個(gè)節(jié)點(diǎn)都包含了一個(gè)字符串 從鏈表的兩端推入或者彈出元素埠况;根據(jù)偏移量對(duì)鏈表進(jìn)行修剪(trim)耸携;讀取單個(gè)或者多個(gè)元素;根據(jù)值來查找或者移除元素
Set 包含字符串的無序收集器(unorderedcollection)辕翰,并且被包含的每個(gè)字符串都是獨(dú)一無二的夺衍、各不相同 添加、獲取喜命、移除單個(gè)元素沟沙;檢查一個(gè)元素是否存在于某個(gè)集合中;計(jì)算交集壁榕、并集矛紫、差集;從集合里賣弄隨機(jī)獲取元素
Zset 字符串成員(member)與浮點(diǎn)數(shù)分值(score)之間的有序映射牌里,元素的排列順序由分值的大小決定 添加颊咬、獲取、刪除單個(gè)元素;根據(jù)分值范圍(range)或者成員來獲取元素
image.png

RedisTemplate中定義了對(duì)5種數(shù)據(jù)結(jié)構(gòu)操作

  • redisTemplate.opsForValue();//操作字符串
  • redisTemplate.opsForHash();//操作hash
  • redisTemplate.opsForList();//操作list
  • redisTemplate.opsForSet();//操作set
  • redisTemplate.opsForZSet();//操作有序set

String

  • set void set(K key, V value);
使用:redisTemplate.opsForValue().set("name","tom");
結(jié)果:redisTemplate.opsForValue().get("name")  輸出結(jié)果為tom

  • set void set(K key, V value, long timeout, TimeUnit unit);
使用:redisTemplate.opsForValue().set("name","tom",10, TimeUnit.SECONDS);
結(jié)果:redisTemplate.opsForValue().get("name")由于設(shè)置的是10秒失效喳篇,十秒之內(nèi)查詢有結(jié)果缓呛,十秒之后返回為null

  • set void set(K key, V value, long offset);
    該方法是用 value 參數(shù)覆寫(overwrite)給定 key 所儲(chǔ)存的字符串值,從偏移量 offset 開始
使用:template.opsForValue().set("key","hello world");
        template.opsForValue().set("key","redis", 6);
        System.out.println("***************"+template.opsForValue().get("key"));
結(jié)果:***************hello redis

  • setIfAbsent Boolean setIfAbsent(K key, V value);
使用:System.out.println(template.opsForValue().setIfAbsent("multi1","multi1"));//false  multi1之前已經(jīng)存在
        System.out.println(template.opsForValue().setIfAbsent("multi111","multi111"));//true  multi111之前不存在
結(jié)果:false
true

  • multiSet void multiSet(Map<? extends K, ? extends V> m);
    為多個(gè)鍵分別設(shè)置它們的值
使用:Map<String,String> maps = new HashMap<String, String>();
        maps.put("multi1","multi1");
        maps.put("multi2","multi2");
        maps.put("multi3","multi3");
        template.opsForValue().multiSet(maps);
        List<String> keys = new ArrayList<String>();
        keys.add("multi1");
        keys.add("multi2");
        keys.add("multi3");
        System.out.println(template.opsForValue().multiGet(keys));
結(jié)果:[multi1, multi2, multi3]

  • multiSetIfAbsent Boolean multiSetIfAbsent(Map<? extends K, ? extends V> m);
    為多個(gè)鍵分別設(shè)置它們的值杭隙,如果存在則返回false哟绊,不存在返回true
使用:Map<String,String> maps = new HashMap<String, String>();
        maps.put("multi11","multi11");
        maps.put("multi22","multi22");
        maps.put("multi33","multi33");
        Map<String,String> maps2 = new HashMap<String, String>();
        maps2.put("multi1","multi1");
        maps2.put("multi2","multi2");
        maps2.put("multi3","multi3");
        System.out.println(template.opsForValue().multiSetIfAbsent(maps));
        System.out.println(template.opsForValue().multiSetIfAbsent(maps2));
結(jié)果:true
false

  • get V get(Object key);
使用:template.opsForValue().set("key","hello world");
        System.out.println("***************"+template.opsForValue().get("key"));
結(jié)果:***************hello world

  • getAndSet V getAndSet(K key, V value);
    設(shè)置鍵的字符串值并返回其舊值
使用:template.opsForValue().set("getSetTest","test");
        System.out.println(template.opsForValue().getAndSet("getSetTest","test2"));
結(jié)果:test

  • multiGet List<V> multiGet(Collection<K> keys);
    為多個(gè)鍵分別取出它們的值
使用:Map<String,String> maps = new HashMap<String, String>();
        maps.put("multi1","multi1");
        maps.put("multi2","multi2");
        maps.put("multi3","multi3");
        template.opsForValue().multiSet(maps);
        List<String> keys = new ArrayList<String>();
        keys.add("multi1");
        keys.add("multi2");
        keys.add("multi3");
        System.out.println(template.opsForValue().multiGet(keys));
結(jié)果:[multi1, multi2, multi3]

  • increment Long increment(K key, long delta);
    支持整數(shù)
使用:template.opsForValue().increment("increlong",1);
        System.out.println("***************"+template.opsForValue().get("increlong"));
結(jié)果:***************1

  • increment Double increment(K key, double delta);
    也支持浮點(diǎn)數(shù)
使用:template.opsForValue().increment("increlong",1.2);
        System.out.println("***************"+template.opsForValue().get("increlong"));
結(jié)果:***************2.2

  • append Integer append(K key, String value);
    如果key已經(jīng)存在并且是一個(gè)字符串,則該命令將該值追加到字符串的末尾痰憎。如果鍵不存在票髓,則它被創(chuàng)建并設(shè)置為空字符串,因此APPEND在這種特殊情況下將類似于SET铣耘。
使用:template.opsForValue().append("appendTest","Hello");
        System.out.println(template.opsForValue().get("appendTest"));
        template.opsForValue().append("appendTest","world");
        System.out.println(template.opsForValue().get("appendTest"));
結(jié)果:Hello
        Helloworld

  • get String get(K key, long start, long end);
    截取key所對(duì)應(yīng)的value字符串
使用:appendTest對(duì)應(yīng)的value為Helloworld
System.out.println("*********"+template.opsForValue().get("appendTest",0,5));
結(jié)果:*********Hellow
使用:System.out.println("*********"+template.opsForValue().get("appendTest",0,-1));
結(jié)果:*********Helloworld
使用:System.out.println("*********"+template.opsForValue().get("appendTest",-3,-1));
結(jié)果:*********rld

  • size Long size(K key);
    返回key所對(duì)應(yīng)的value值得長(zhǎng)度
使用:template.opsForValue().set("key","hello world");
    System.out.println("***************"+template.opsForValue().size("key"));
結(jié)果:***************11

  • setBit Boolean setBit(K key, long offset, boolean value);
    對(duì) key 所儲(chǔ)存的字符串值洽沟,設(shè)置或清除指定偏移量上的位(bit)
    key鍵對(duì)應(yīng)的值value對(duì)應(yīng)的ascii碼,在offset的位置(從左向右數(shù))變?yōu)関alue
使用:template.opsForValue().set("bitTest","a");
        // 'a' 的ASCII碼是 97。轉(zhuǎn)換為二進(jìn)制是:01100001
        // 'b' 的ASCII碼是 98  轉(zhuǎn)換為二進(jìn)制是:01100010
        // 'c' 的ASCII碼是 99  轉(zhuǎn)換為二進(jìn)制是:01100011
        //因?yàn)槎M(jìn)制只有0和1蜗细,在setbit中true為1裆操,false為0,因此我要變?yōu)?b'的話第六位設(shè)置為1炉媒,第七位設(shè)置為0
        template.opsForValue().setBit("bitTest",6, true);
        template.opsForValue().setBit("bitTest",7, false);
        System.out.println(template.opsForValue().get("bitTest"));
結(jié)果:b

  • getBit Boolean getBit(K key, long offset);
    獲取鍵對(duì)應(yīng)值的ascii碼的在offset處位值
使用:System.out.println(template.opsForValue().getBit("bitTest",7));
結(jié)果:false

List

  • List<V> range(K key, long start, long end);
    返回存儲(chǔ)在鍵中的列表的指定元素踪区。偏移開始和停止是基于零的索引,其中0是列表的第一個(gè)元素(列表的頭部)吊骤,1是下一個(gè)元素
使用:System.out.println(template.opsForList().range("list",0,-1));
結(jié)果:[c#, c++, python, java, c#, c#]

  • void trim(K key, long start, long end);
    修剪現(xiàn)有列表缎岗,使其只包含指定的指定范圍的元素,起始和停止都是基于0的索引
使用:System.out.println(template.opsForList().range("list",0,-1));
template.opsForList().trim("list",1,-1);//裁剪第一個(gè)元素
System.out.println(template.opsForList().range("list",0,-1));
結(jié)果:[c#, c++, python, java, c#, c#]
[c++, python, java, c#, c#]

  • Long size(K key);
    返回存儲(chǔ)在鍵中的列表的長(zhǎng)度白粉。如果鍵不存在传泊,則將其解釋為空列表,并返回0鸭巴。當(dāng)key存儲(chǔ)的值不是列表時(shí)返回錯(cuò)誤眷细。
使用:System.out.println(template.opsForList().size("list"));
結(jié)果:6

  • Long leftPush(K key, V value);
    將所有指定的值插入存儲(chǔ)在鍵的列表的頭部。如果鍵不存在鹃祖,則在執(zhí)行推送操作之前將其創(chuàng)建為空列表溪椎。(從左邊插入)
使用:template.opsForList().leftPush("list","java");
        template.opsForList().leftPush("list","python");
        template.opsForList().leftPush("list","c++");
結(jié)果:返回的結(jié)果為推送操作后的列表的長(zhǎng)度
1
2
3

  • Long leftPushAll(K key, V... values);
    批量把一個(gè)數(shù)組插入到列表中
使用:String[] stringarrays = new String[]{"1","2","3"};
        template.opsForList().leftPushAll("listarray",stringarrays);
        System.out.println(template.opsForList().range("listarray",0,-1));
結(jié)果:[3, 2, 1]

  • Long leftPushAll(K key, Collection<V> values);
    批量把一個(gè)集合插入到列表中
使用:List<Object> strings = new ArrayList<Object>();
        strings.add("1");
        strings.add("2");
        strings.add("3");
        template.opsForList().leftPushAll("listcollection4", strings);
        System.out.println(template.opsForList().range("listcollection4",0,-1));
結(jié)果:[3, 2, 1]

  • Long leftPushIfPresent(K key, V value);
    只有存在key對(duì)應(yīng)的列表才能將這個(gè)value值插入到key所對(duì)應(yīng)的列表中
使用:System.out.println(template.opsForList().leftPushIfPresent("leftPushIfPresent","aa"));
        System.out.println(template.opsForList().leftPushIfPresent("leftPushIfPresent","bb"));
==========分割線===========
System.out.println(template.opsForList().leftPush("leftPushIfPresent","aa"));
        System.out.println(template.opsForList().leftPushIfPresent("leftPushIfPresent","bb"));
結(jié)果:
0
0
==========分割線===========
1
2

  • Long leftPush(K key, V pivot, V value);
    把value值放到key對(duì)應(yīng)列表中pivot值的左面,如果pivot值存在的話
使用:template.opsForList().leftPush("list","java","oc");
        System.out.print(template.opsForList().range("list",0,-1));
結(jié)果:[c++, python, oc, java, c#, c#]

  • Long rightPush(K key, V value);
    將所有指定的值插入存儲(chǔ)在鍵的列表的頭部惯豆。如果鍵不存在池磁,則在執(zhí)行推送操作之前將其創(chuàng)建為空列表奔害。(從右邊插入)
使用:template.opsForList().rightPush("listRight","java");
        template.opsForList().rightPush("listRight","python");
        template.opsForList().rightPush("listRight","c++");
結(jié)果:
1
2
3

  • Long rightPushAll(K key, V... values);
使用:String[] stringarrays = new String[]{"1","2","3"};
        template.opsForList().rightPushAll("listarrayright",stringarrays);
        System.out.println(template.opsForList().range("listarrayright",0,-1));
結(jié)果:[1, 2, 3]

  • Long rightPushAll(K key, Collection<V> values);
使用:List<Object> strings = new ArrayList<Object>();
        strings.add("1");
        strings.add("2");
        strings.add("3");
        template.opsForList().rightPushAll("listcollectionright", strings);
        System.out.println(template.opsForList().range("listcollectionright",0,-1));
結(jié)果:[1, 2, 3]

  • Long rightPushIfPresent(K key, V value);
    只有存在key對(duì)應(yīng)的列表才能將這個(gè)value值插入到key所對(duì)應(yīng)的列表中
使用:System.out.println(template.opsForList().rightPushIfPresent("rightPushIfPresent","aa"));
        System.out.println(template.opsForList().rightPushIfPresent("rightPushIfPresent","bb"));
        System.out.println("==========分割線===========");
        System.out.println(template.opsForList().rightPush("rightPushIfPresent","aa"));
        System.out.println(template.opsForList().rightPushIfPresent("rightPushIfPresent","bb"));
結(jié)果:0
0
==========分割線===========
1
2

  • Long rightPush(K key, V pivot, V value);
    把value值放到key對(duì)應(yīng)列表中pivot值的右面楷兽,如果pivot值存在的話
使用:System.out.println(template.opsForList().range("listRight",0,-1));
        template.opsForList().rightPush("listRight","python","oc");
        System.out.println(template.opsForList().range("listRight",0,-1));
結(jié)果:[java, python, c++]
[java, python, oc, c++]

  • void set(K key, long index, V value);
    在列表中index的位置設(shè)置value值
使用:System.out.println(template.opsForList().range("listRight",0,-1));
        template.opsForList().set("listRight",1,"setValue");
        System.out.println(template.opsForList().range("listRight",0,-1));
結(jié)果:[java, python, oc, c++]
[java, setValue, oc, c++]

  • Long remove(K key, long count, Object value);
    從存儲(chǔ)在鍵中的列表中刪除等于值的元素的第一個(gè)計(jì)數(shù)事件。
    計(jì)數(shù)參數(shù)以下列方式影響操作:
    count> 0:刪除等于從頭到尾移動(dòng)的值的元素华临。
    count <0:刪除等于從尾到頭移動(dòng)的值的元素芯杀。
    count = 0:刪除等于value的所有元素。
使用:System.out.println(template.opsForList().range("listRight",0,-1));
        template.opsForList().remove("listRight",1,"setValue");//將刪除列表中存儲(chǔ)的列表中第一次次出現(xiàn)的“setValue”。
        System.out.println(template.opsForList().range("listRight",0,-1));
結(jié)果:[java, setValue, oc, c++]
[java, oc, c++]

  • V index(K key, long index);
    根據(jù)下表獲取列表中的值揭厚,下標(biāo)是從0開始的
使用:System.out.println(template.opsForList().range("listRight",0,-1));
System.out.println(template.opsForList().index("listRight",2));
結(jié)果:[java, oc, c++]
c++

  • V leftPop(K key);
    彈出最左邊的元素却特,彈出之后該值在列表中將不復(fù)存在
使用:System.out.println(template.opsForList().range("list",0,-1));
        System.out.println(template.opsForList().leftPop("list"));
        System.out.println(template.opsForList().range("list",0,-1));
結(jié)果:
[c++, python, oc, java, c#, c#]
c++
[python, oc, java, c#, c#]

  • V leftPop(K key, long timeout, TimeUnit unit);
    移出并獲取列表的第一個(gè)元素, 如果列表沒有元素會(huì)阻塞列表直到等待超時(shí)或發(fā)現(xiàn)可彈出元素為止筛圆。
使用:用法與 leftPop(K key);一樣

  • V rightPop(K key);
    彈出最右邊的元素裂明,彈出之后該值在列表中將不復(fù)存在
使用: System.out.println(template.opsForList().range("list",0,-1));
        System.out.println(template.opsForList().rightPop("list"));
        System.out.println(template.opsForList().range("list",0,-1));
結(jié)果:[python, oc, java, c#, c#]
c#
[python, oc, java, c#]

  • V rightPop(K key, long timeout, TimeUnit unit);
    移出并獲取列表的最后一個(gè)元素, 如果列表沒有元素會(huì)阻塞列表直到等待超時(shí)或發(fā)現(xiàn)可彈出元素為止太援。
使用:用法與 rightPop(K key);一樣

  • V rightPopAndLeftPush(K sourceKey, K destinationKey);
    用于移除列表的最后一個(gè)元素闽晦,并將該元素添加到另一個(gè)列表并返回。
使用:System.out.println(template.opsForList().range("list",0,-1));
template.opsForList().rightPopAndLeftPush("list","rightPopAndLeftPush");
    System.out.println(template.opsForList().range("list",0,-1));
    System.out.println(template.opsForList().range("rightPopAndLeftPush",0,-1));
結(jié)果:[oc, java,c#]
[oc, java]
[c#]

  • V rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit);
    用于移除列表的最后一個(gè)元素提岔,并將該元素添加到另一個(gè)列表并返回仙蛉,如果列表沒有元素會(huì)阻塞列表直到等待超時(shí)或發(fā)現(xiàn)可彈出元素為止。
使用:用法與rightPopAndLeftPush(K sourceKey, K destinationKey)一樣

Hash

Redis的散列可以讓用戶將多個(gè)鍵值對(duì)存儲(chǔ)到一個(gè)Redis鍵里面碱蒙。
public interface HashOperations<H,HK,HV>
HashOperations提供一系列方法操作hash:

初始數(shù)據(jù):
//template.opsForHash().put("redisHash","name","tom");
        //template.opsForHash().put("redisHash","age",26);
        //template.opsForHash().put("redisHash","class","6");

//Map<String,Object> testMap = new HashMap();
        //testMap.put("name","jack");
        //testMap.put("age",27);
        //testMap.put("class","1");
        //template.opsForHash().putAll("redisHash1",testMap);

  • Long delete(H key, Object... hashKeys);
    刪除給定的哈希hashKeys
使用:System.out.println(template.opsForHash().delete("redisHash","name"));
        System.out.println(template.opsForHash().entries("redisHash"));
結(jié)果:1
{class=6, age=28.1}

  • Boolean hasKey(H key, Object hashKey);
    確定哈希hashKey是否存在
使用:System.out.println(template.opsForHash().hasKey("redisHash","age"));
        System.out.println(template.opsForHash().hasKey("redisHash","ttt"));
結(jié)果:true
false

  • HV get(H key, Object hashKey);
    從鍵中的哈希獲取給定hashKey的值
使用:System.out.println(template.opsForHash().get("redisHash","age"));
結(jié)果:26

  • List<HV> multiGet(H key, Collection<HK> hashKeys);
    從哈希中獲取給定hashKey的值
使用:List<Object> kes = new ArrayList<Object>();
        kes.add("name");
        kes.add("age");
        System.out.println(template.opsForHash().multiGet("redisHash",kes));
結(jié)果:[jack, 28.1]

  • Long increment(H key, HK hashKey, long delta);
    通過給定的delta增加散列hashKey的值(整型)
使用:System.out.println(template.opsForHash().get("redisHash","age"));
    System.out.println(template.opsForHash().increment("redisHash","age",1));
結(jié)果:26
27

  • Double increment(H key, HK hashKey, double delta);
    通過給定的delta增加散列hashKey的值(浮點(diǎn)數(shù))
使用:System.out.println(template.opsForHash().get("redisHash","age"));
    System.out.println(template.opsForHash().increment("redisHash","age",1.1));
結(jié)果:27
28.1

  • Set<HK> keys(H key);
    獲取key所對(duì)應(yīng)的散列表的key
使用:System.out.println(template.opsForHash().keys("redisHash1"));
//redisHash1所對(duì)應(yīng)的散列表為{class=1, name=jack, age=27}
結(jié)果:[name, class, age]

  • Long size(H key);
    獲取key所對(duì)應(yīng)的散列表的大小個(gè)數(shù)
使用:System.out.println(template.opsForHash().size("redisHash1"));
//redisHash1所對(duì)應(yīng)的散列表為{class=1, name=jack, age=27}
結(jié)果:3

  • void putAll(H key, Map<? extends HK, ? extends HV> m);
    使用m中提供的多個(gè)散列字段設(shè)置到key對(duì)應(yīng)的散列表中
使用:Map<String,Object> testMap = new HashMap();
        testMap.put("name","jack");
        testMap.put("age",27);
        testMap.put("class","1");
        template.opsForHash().putAll("redisHash1",testMap);
        System.out.println(template.opsForHash().entries("redisHash1"));
結(jié)果:{class=1, name=jack, age=27}

  • void put(H key, HK hashKey, HV value);
    設(shè)置散列hashKey的值
使用:template.opsForHash().put("redisHash","name","tom");
        template.opsForHash().put("redisHash","age",26);
        template.opsForHash().put("redisHash","class","6");
System.out.println(template.opsForHash().entries("redisHash"));
結(jié)果:{age=26, class=6, name=tom}

  • Boolean putIfAbsent(H key, HK hashKey, HV value);
    僅當(dāng)hashKey不存在時(shí)才設(shè)置散列hashKey的值荠瘪。
使用:System.out.println(template.opsForHash().putIfAbsent("redisHash","age",30));
System.out.println(template.opsForHash().putIfAbsent("redisHash","kkk","kkk"));
結(jié)果:false
true

  • List<HV> values(H key);
    獲取整個(gè)哈希存儲(chǔ)的值根據(jù)密鑰
使用:System.out.println(template.opsForHash().values("redisHash"));
結(jié)果:[tom, 26, 6]

  • Map<HK, HV> entries(H key);
    獲取整個(gè)哈希存儲(chǔ)根據(jù)密鑰
使用:System.out.println(template.opsForHash().entries("redisHash"));
結(jié)果:{age=26, class=6, name=tom}

  • Cursor<Map.Entry<HK, HV>> scan(H key, ScanOptions options);
    使用Cursor在key的hash中迭代,相當(dāng)于迭代器赛惩。
使用:Cursor<Map.Entry<Object, Object>> curosr = template.opsForHash().scan("redisHash", ScanOptions.ScanOptions.NONE);
        while(curosr.hasNext()){
            Map.Entry<Object, Object> entry = curosr.next();
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
結(jié)果:age:28.1
class:6
kkk:kkk

Set

Redis的Set是string類型的無序集合哀墓。集合成員是唯一的,這就意味著集合中不能出現(xiàn)重復(fù)的數(shù)據(jù)喷兼。
Redis 中 集合是通過哈希表實(shí)現(xiàn)的麸祷,所以添加,刪除褒搔,查找的復(fù)雜度都是O(1)阶牍。
public interface SetOperations<K,V>
SetOperations提供了對(duì)無序集合的一系列操作:

  • Long add(K key, V... values);
    無序集合中添加元素,返回添加個(gè)數(shù)
    也可以直接在add里面添加多個(gè)值 如:template.opsForSet().add("setTest","aaa","bbb")
使用:String[] strarrays = new String[]{"strarr1","sgtarr2"};
        System.out.println(template.opsForSet().add("setTest", strarrays));
結(jié)果:2

  • Long remove(K key, Object... values);
    移除集合中一個(gè)或多個(gè)成員
使用:String[] strarrays = new String[]{"strarr1","sgtarr2"};
System.out.println(template.opsForSet().remove("setTest",strarrays));
結(jié)果:2

  • V pop(K key);
    移除并返回集合中的一個(gè)隨機(jī)元素
使用:System.out.println(template.opsForSet().pop("setTest"));
System.out.println(template.opsForSet().members("setTest"));
結(jié)果:bbb
[aaa, ccc]

  • Boolean move(K key, V value, K destKey);
    將 member 元素從 source 集合移動(dòng)到 destination 集合
使用:template.opsForSet().move("setTest","aaa","setTest2");
        System.out.println(template.opsForSet().members("setTest"));
        System.out.println(template.opsForSet().members("setTest2"));
結(jié)果:[ccc]
[aaa]

  • Long size(K key);
    無序集合的大小長(zhǎng)度
使用:System.out.println(template.opsForSet().size("setTest"));
結(jié)果:1

  • Boolean isMember(K key, Object o);
    判斷 member 元素是否是集合 key 的成員
使用:System.out.println(template.opsForSet().isMember("setTest","ccc"));
        System.out.println(template.opsForSet().isMember("setTest","asd"));
結(jié)果:true
false

  • Set<V> intersect(K key, K otherKey);
    key對(duì)應(yīng)的無序集合與otherKey對(duì)應(yīng)的無序集合求交集
使用:System.out.println(template.opsForSet().members("setTest"));
        System.out.println(template.opsForSet().members("setTest2"));
        System.out.println(template.opsForSet().intersect("setTest","setTest2"));
結(jié)果:[aaa, ccc]
[aaa]
[aaa]

  • Set<V> intersect(K key, Collection<K> otherKeys);
    key對(duì)應(yīng)的無序集合與多個(gè)otherKey對(duì)應(yīng)的無序集合求交集
使用:System.out.println(template.opsForSet().members("setTest"));
        System.out.println(template.opsForSet().members("setTest2"));
        System.out.println(template.opsForSet().members("setTest3"));
        List<String> strlist = new ArrayList<String>();
        strlist.add("setTest2");
        strlist.add("setTest3");
        System.out.println(template.opsForSet().intersect("setTest",strlist));
結(jié)果:[aaa, ccc]
[aaa]
[ccc, aaa]
[aaa]

  • Long intersectAndStore(K key, K otherKey, K destKey);
    key無序集合與otherkey無序集合的交集存儲(chǔ)到destKey無序集合中
使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
System.out.println(template.opsForSet().intersectAndStore("setTest","setTest2","destKey1"));
System.out.println(template.opsForSet().members("destKey1"));
結(jié)果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
2
[aaa, ccc]

  • Long intersectAndStore(K key, Collection<K> otherKeys, K destKey);
    key對(duì)應(yīng)的無序集合與多個(gè)otherKey對(duì)應(yīng)的無序集合求交集存儲(chǔ)到destKey無序集合中
使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println("setTest3:" + template.opsForSet().members("setTest3"));
        List<String> strlist = new ArrayList<String>();
        strlist.add("setTest2");
        strlist.add("setTest3");
        System.out.println(template.opsForSet().intersectAndStore("setTest",strlist,"destKey2"));
        System.out.println(template.opsForSet().members("destKey2"));
結(jié)果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[ccc, aaa]
2
[aaa, ccc]

  • Set<V> union(K key, K otherKey);
    key無序集合與otherKey無序集合的并集
使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println(template.opsForSet().union("setTest","setTest2"));
結(jié)果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
[ccc, aaa, ddd, bbb]

  • Set<V> union(K key, Collection<K> otherKeys);
    key無序集合與多個(gè)otherKey無序集合的并集
使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println("setTest3:" + template.opsForSet().members("setTest3"));
        List<String> strlist = new ArrayList<String>();
        strlist.add("setTest2");
        strlist.add("setTest3");
        System.out.println(template.opsForSet().union("setTest",strlist));
結(jié)果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[xxx, ccc, aaa]
[ddd, xxx, bbb, aaa, ccc]

  • Long unionAndStore(K key, K otherKey, K destKey);
    key無序集合與otherkey無序集合的并集存儲(chǔ)到destKey無序集合中
使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println(template.opsForSet().unionAndStore("setTest","setTest2","unionAndStoreTest1"));
        System.out.println("unionAndStoreTest1:" + template.opsForSet().members("unionAndStoreTest1"));
結(jié)果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
4
unionAndStoreTest1:[ccc, aaa, ddd, bbb]

  • Long unionAndStore(K key, Collection<K> otherKeys, K destKey);
    key無序集合與多個(gè)otherkey無序集合的并集存儲(chǔ)到destKey無序集合中
使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println("setTest3:" + template.opsForSet().members("setTest3"));
        List<String> strlist = new ArrayList<String>();
        strlist.add("setTest2");
        strlist.add("setTest3");
        System.out.println(template.opsForSet().unionAndStore("setTest",strlist,"unionAndStoreTest2"));
        System.out.println("unionAndStoreTest2:" + template.opsForSet().members("unionAndStoreTest2"));
結(jié)果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[xxx, ccc, aaa]
5
unionAndStoreTest2:[ddd, xxx, bbb, aaa, ccc]

  • Set<V> difference(K key, K otherKey);
    key無序集合與otherKey無序集合的差集
使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println(template.opsForSet().difference("setTest","setTest2"));
結(jié)果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
[bbb, ddd]

  • Set<V> difference(K key, Collection<K> otherKeys);
    key無序集合與多個(gè)otherKey無序集合的差集
使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println("setTest3:" + template.opsForSet().members("setTest3"));
        List<String> strlist = new ArrayList<String>();
        strlist.add("setTest2");
        strlist.add("setTest3");
        System.out.println(template.opsForSet().difference("setTest",strlist));
結(jié)果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[xxx, ccc, aaa]
[bbb, ddd]

  • Long differenceAndStore(K key, K otherKey, K destKey);
    key無序集合與otherkey無序集合的差集存儲(chǔ)到destKey無序集合中
使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println(template.opsForSet().differenceAndStore("setTest","setTest2","differenceAndStore1"));
        System.out.println("differenceAndStore1:" + template.opsForSet().members("differenceAndStore1"));
結(jié)果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
2
differenceAndStore1:[bbb, ddd]

  • Long differenceAndStore(K key, Collection<K> otherKeys, K destKey);
    key無序集合與多個(gè)otherkey無序集合的差集存儲(chǔ)到destKey無序集合中
使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println("setTest3:" + template.opsForSet().members("setTest3"));
        List<String> strlist = new ArrayList<String>();
        strlist.add("setTest2");
        strlist.add("setTest3");
        System.out.println(template.opsForSet().differenceAndStore("setTest",strlist,"differenceAndStore2"));
        System.out.println("differenceAndStore2:" + template.opsForSet().members("differenceAndStore2"));
結(jié)果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[xxx, ccc, aaa]
2
differenceAndStore2:[bbb, ddd]

  • Set<V> members(K key);
    返回集合中的所有成員
使用:System.out.println(template.opsForSet().members("setTest"));
結(jié)果:[ddd, bbb, aaa, ccc]

  • V randomMember(K key);
    隨機(jī)獲取key無序集合中的一個(gè)元素
使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTestrandomMember:" + template.opsForSet().randomMember("setTest"));
        System.out.println("setTestrandomMember:" + template.opsForSet().randomMember("setTest"));
        System.out.println("setTestrandomMember:" + template.opsForSet().randomMember("setTest"));
        System.out.println("setTestrandomMember:" + template.opsForSet().randomMember("setTest"));
結(jié)果:setTest:[ddd, bbb, aaa, ccc]
setTestrandomMember:aaa
setTestrandomMember:bbb
setTestrandomMember:aaa
setTestrandomMember:ddd

  • Set<V> distinctRandomMembers(K key, long count);
    獲取多個(gè)key無序集合中的元素(去重)星瘾,count表示個(gè)數(shù)
使用:System.out.println("randomMembers:" + template.opsForSet().distinctRandomMembers("setTest",5));
結(jié)果:randomMembers:[aaa, bbb, ddd, ccc]

  • List<V> randomMembers(K key, long count);
    獲取多個(gè)key無序集合中的元素走孽,count表示個(gè)數(shù)
使用:System.out.println("randomMembers:" + template.opsForSet().randomMembers("setTest",5));
結(jié)果:randomMembers:[ccc, ddd, ddd, ddd, aaa]

  • Cursor<V> scan(K key, ScanOptions options);
    遍歷set
使用: Cursor<Object> curosr = template.opsForSet().scan("setTest", ScanOptions.NONE);
        while(curosr.hasNext()){
            System.out.println(curosr.next());
        }
結(jié)果:ddd
bbb
aaa
ccc

ZSet

Redis 有序集合和無序集合一樣也是string類型元素的集合,且不允許重復(fù)的成員。
不同的是每個(gè)元素都會(huì)關(guān)聯(lián)一個(gè)double類型的分?jǐn)?shù)琳状。redis正是通過分?jǐn)?shù)來為集合中的成員進(jìn)行從小到大的排序磕瓷。
有序集合的成員是唯一的,但分?jǐn)?shù)(score)卻可以重復(fù)。
public interface ZSetOperations<K,V>
ZSetOperations提供了一系列方法對(duì)有序集合進(jìn)行操作:

  • Boolean add(K key, V value, double score);
    新增一個(gè)有序集合念逞,存在的話為false困食,不存在的話為true
使用:System.out.println(template.opsForZSet().add("zset1","zset-1",1.0));
結(jié)果:true

  • Long add(K key, Set<TypedTuple<V>> tuples);
    新增一個(gè)有序集合
使用:ZSetOperations.TypedTuple<Object> objectTypedTuple1 = new DefaultTypedTuple<Object>("zset-5",9.6);
        ZSetOperations.TypedTuple<Object> objectTypedTuple2 = new DefaultTypedTuple<Object>("zset-6",9.9);
        Set<ZSetOperations.TypedTuple<Object>> tuples = new HashSet<ZSetOperations.TypedTuple<Object>>();
        tuples.add(objectTypedTuple1);
        tuples.add(objectTypedTuple2);
        System.out.println(template.opsForZSet().add("zset1",tuples));
        System.out.println(template.opsForZSet().range("zset1",0,-1));
結(jié)果:[zset-1, zset-2, zset-3, zset-4, zset-5, zset-6]

  • Long remove(K key, Object... values);
    從有序集合中移除一個(gè)或者多個(gè)元素
使用:System.out.println(template.opsForZSet().range("zset1",0,-1));
        System.out.println(template.opsForZSet().remove("zset1","zset-6"));
        System.out.println(template.opsForZSet().range("zset1",0,-1));
結(jié)果:[zset-1, zset-2, zset-3, zset-4, zset-5, zset-6]
1
[zset-1, zset-2, zset-3, zset-4, zset-5]

  • Double incrementScore(K key, V value, double delta);
    增加元素的score值,并返回增加后的值
使用:System.out.println(template.opsForZSet().incrementScore("zset1","zset-1",1.1));  //原為1.1
結(jié)果:2.2

  • Long rank(K key, Object o);
    返回有序集中指定成員的排名翎承,其中有序集成員按分?jǐn)?shù)值遞增(從小到大)順序排列
使用:System.out.println(template.opsForZSet().range("zset1",0,-1));
        System.out.println(template.opsForZSet().rank("zset1","zset-2"));
結(jié)果:[zset-2, zset-1, zset-3, zset-4, zset-5]
0   //表明排名第一

  • Long reverseRank(K key, Object o);
    返回有序集中指定成員的排名硕盹,其中有序集成員按分?jǐn)?shù)值遞減(從大到小)順序排列
使用:System.out.println(template.opsForZSet().range("zset1",0,-1));
        System.out.println(template.opsForZSet().reverseRank("zset1","zset-2"));
結(jié)果:[zset-2, zset-1, zset-3, zset-4, zset-5]
4 //遞減之后排到第五位去了

  • Set<V> range(K key, long start, long end);
    通過索引區(qū)間返回有序集合成指定區(qū)間內(nèi)的成員,其中有序集成員按分?jǐn)?shù)值遞增(從小到大)順序排列
使用:System.out.println(template.opsForZSet().range("zset1",0,-1));
結(jié)果:[zset-2, zset-1, zset-3, zset-4, zset-5]

  • Set<TypedTuple<V>> rangeWithScores(K key, long start, long end);
    通過索引區(qū)間返回有序集合成指定區(qū)間內(nèi)的成員對(duì)象叨咖,其中有序集成員按分?jǐn)?shù)值遞增(從小到大)順序排列
使用:Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores("zset1",0,-1);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
        while (iterator.hasNext())
        {
            ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
            System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
        }
結(jié)果:value:zset-2score:1.2
value:zset-1score:2.2
value:zset-3score:2.3
value:zset-4score:6.6
value:zset-5score:9.6

  • Set<V> rangeByScore(K key, double min, double max);
    通過分?jǐn)?shù)返回有序集合指定區(qū)間內(nèi)的成員瘩例,其中有序集成員按分?jǐn)?shù)值遞增(從小到大)順序排列
使用:System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));
結(jié)果:[zset-2, zset-1, zset-3]

  • Set<TypedTuple<V>> rangeByScoreWithScores(K key, double min, double max);
    通過分?jǐn)?shù)返回有序集合指定區(qū)間內(nèi)的成員對(duì)象啊胶,其中有序集成員按分?jǐn)?shù)值遞增(從小到大)順序排列
使用:Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeByScoreWithScores("zset1",0,5);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
        while (iterator.hasNext())
        {
            ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
            System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
        }
結(jié)果:value:zset-2score:1.2
value:zset-1score:2.2
value:zset-3score:2.3

  • Set<V> rangeByScore(K key, double min, double max, long offset, long count);
    通過分?jǐn)?shù)返回有序集合指定區(qū)間內(nèi)的成員,并在索引范圍內(nèi)垛贤,其中有序集成員按分?jǐn)?shù)值遞增(從小到大)順序排列
使用: System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));
    System.out.println(template.opsForZSet().rangeByScore("zset1",0,5,1,2));
結(jié)果:[zset-2, zset-1, zset-3]
[zset-1, zset-3]

  • Set<TypedTuple<V>> rangeByScoreWithScores(K key, double min, double max, long offset, long count);
    通過分?jǐn)?shù)返回有序集合指定區(qū)間內(nèi)的成員對(duì)象焰坪,并在索引范圍內(nèi),其中有序集成員按分?jǐn)?shù)值遞增(從小到大)順序排列
使用:Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeByScoreWithScores("zset1",0,5,1,2);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
        while (iterator.hasNext())
        {
            ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
            System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
        }
結(jié)果:value:zset-1score:2.2
value:zset-3score:2.3

  • Set<V> reverseRange(K key, long start, long end);
    通過索引區(qū)間返回有序集合成指定區(qū)間內(nèi)的成員聘惦,其中有序集成員按分?jǐn)?shù)值遞減(從大到小)順序排列
使用:System.out.println(template.opsForZSet().reverseRange("zset1",0,-1));
結(jié)果:[zset-5, zset-4, zset-3, zset-1, zset-2]

  • Set<TypedTuple<V>> reverseRangeWithScores(K key, long start, long end);
    通過索引區(qū)間返回有序集合成指定區(qū)間內(nèi)的成員對(duì)象某饰,其中有序集成員按分?jǐn)?shù)值遞減(從大到小)順序排列
使用:Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().reverseRangeWithScores("zset1",0,-1);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
        while (iterator.hasNext())
        {
            ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
            System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
        }
結(jié)果:value:zset-5score:9.6
value:zset-4score:6.6
value:zset-3score:2.3
value:zset-1score:2.2
value:zset-2score:1.2

  • Set<V> reverseRangeByScore(K key, double min, double max);
使用:與rangeByScore調(diào)用方法一樣,其中有序集成員按分?jǐn)?shù)值遞減(從大到小)順序排列

  • Set<TypedTuple<V>> reverseRangeByScoreWithScores(K key, double min, double max);
使用:與rangeByScoreWithScores調(diào)用方法一樣善绎,其中有序集成員按分?jǐn)?shù)值遞減(從大到小)順序排列

  • Set<V> reverseRangeByScore(K key, double min, double max, long offset, long count);
使用:與rangeByScore調(diào)用方法一樣露乏,其中有序集成員按分?jǐn)?shù)值遞減(從大到小)順序排列

  • Set<TypedTuple<V>> reverseRangeByScoreWithScores(K key, double min, double max, long offset, long count);
使用:與rangeByScoreWithScores調(diào)用方法一樣,其中有序集成員按分?jǐn)?shù)值遞減(從大到小)順序排列

  • Long count(K key, double min, double max);
    通過分?jǐn)?shù)返回有序集合指定區(qū)間內(nèi)的成員個(gè)數(shù)
使用:System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));
        System.out.println(template.opsForZSet().count("zset1",0,5));
結(jié)果:[zset-2, zset-1, zset-3]
3

  • Long size(K key);
    獲取有序集合的成員數(shù)涂邀,內(nèi)部調(diào)用的就是zCard方法
使用:System.out.println(template.opsForZSet().size("zset1"));
結(jié)果:6

  • Long zCard(K key);
    獲取有序集合的成員數(shù)
使用:System.out.println(template.opsForZSet().zCard("zset1"));
結(jié)果:6

  • Double score(K key, Object o);
    獲取指定成員的score值
使用:System.out.println(template.opsForZSet().score("zset1","zset-1"));
結(jié)果:2.2

  • Long removeRange(K key, long start, long end);
    移除指定索引位置的成員瘟仿,其中有序集成員按分?jǐn)?shù)值遞增(從小到大)順序排列
使用:System.out.println(template.opsForZSet().range("zset2",0,-1));
        System.out.println(template.opsForZSet().removeRange("zset2",1,2));
        System.out.println(template.opsForZSet().range("zset2",0,-1));
結(jié)果:[zset-1, zset-2, zset-3, zset-4]
2
[zset-1, zset-4]

  • Long removeRangeByScore(K key, double min, double max);
    根據(jù)指定的score值得范圍來移除成員
使用://System.out.println(template.opsForZSet().add("zset2","zset-1",1.1));
        //System.out.println(template.opsForZSet().add("zset2","zset-2",1.2));
        //System.out.println(template.opsForZSet().add("zset2","zset-3",2.3));
        //System.out.println(template.opsForZSet().add("zset2","zset-4",6.6));
System.out.println(template.opsForZSet().range("zset2",0,-1));
System.out.println(template.opsForZSet().removeRangeByScore("zset2",2,3));
    System.out.println(template.opsForZSet().range("zset2",0,-1));
結(jié)果:[zset-1, zset-2, zset-3,zset-4]
1
[zset-1, zset-2, zset-4]

  • Long unionAndStore(K key, K otherKey, K destKey);
    計(jì)算給定的一個(gè)有序集的并集,并存儲(chǔ)在新的 destKey中比勉,key相同的話會(huì)把score值相加
使用:System.out.println(template.opsForZSet().add("zzset1","zset-1",1.0));
        System.out.println(template.opsForZSet().add("zzset1","zset-2",2.0));
        System.out.println(template.opsForZSet().add("zzset1","zset-3",3.0));
        System.out.println(template.opsForZSet().add("zzset1","zset-4",6.0));

        System.out.println(template.opsForZSet().add("zzset2","zset-1",1.0));
        System.out.println(template.opsForZSet().add("zzset2","zset-2",2.0));
        System.out.println(template.opsForZSet().add("zzset2","zset-3",3.0));
        System.out.println(template.opsForZSet().add("zzset2","zset-4",6.0));
        System.out.println(template.opsForZSet().add("zzset2","zset-5",7.0));
        System.out.println(template.opsForZSet().unionAndStore("zzset1","zzset2","destZset11"));

        Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores("destZset11",0,-1);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
        while (iterator.hasNext())
        {
            ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
            System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
        }
結(jié)果:value:zset-1score:2.0
value:zset-2score:4.0
value:zset-3score:6.0
value:zset-5score:7.0
value:zset-4score:12.0

  • Long unionAndStore(K key, Collection<K> otherKeys, K destKey);
    計(jì)算給定的多個(gè)有序集的并集劳较,并存儲(chǔ)在新的 destKey中
使用://System.out.println(template.opsForZSet().add("zzset1","zset-1",1.0));
        //System.out.println(template.opsForZSet().add("zzset1","zset-2",2.0));
        //System.out.println(template.opsForZSet().add("zzset1","zset-3",3.0));
        //System.out.println(template.opsForZSet().add("zzset1","zset-4",6.0));
        //
        //System.out.println(template.opsForZSet().add("zzset2","zset-1",1.0));
        //System.out.println(template.opsForZSet().add("zzset2","zset-2",2.0));
        //System.out.println(template.opsForZSet().add("zzset2","zset-3",3.0));
        //System.out.println(template.opsForZSet().add("zzset2","zset-4",6.0));
        //System.out.println(template.opsForZSet().add("zzset2","zset-5",7.0));

        System.out.println(template.opsForZSet().add("zzset3","zset-1",1.0));
        System.out.println(template.opsForZSet().add("zzset3","zset-2",2.0));
        System.out.println(template.opsForZSet().add("zzset3","zset-3",3.0));
        System.out.println(template.opsForZSet().add("zzset3","zset-4",6.0));
        System.out.println(template.opsForZSet().add("zzset3","zset-5",7.0));

        List<String> stringList = new ArrayList<String>();
        stringList.add("zzset2");
        stringList.add("zzset3");
        System.out.println(template.opsForZSet().unionAndStore("zzset1",stringList,"destZset22"));

        Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores("destZset22",0,-1);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
        while (iterator.hasNext())
        {
            ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
            System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
        }
結(jié)果:value:zset-1score:3.0
value:zset-2score:6.0
value:zset-3score:9.0
value:zset-5score:14.0
value:zset-4score:18.0

  • Long intersectAndStore(K key, K otherKey, K destKey);
    計(jì)算給定的一個(gè)或多個(gè)有序集的交集并將結(jié)果集存儲(chǔ)在新的有序集合 key 中
使用:System.out.println(template.opsForZSet().intersectAndStore("zzset1","zzset2","destZset33"));

        Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores("destZset33",0,-1);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
        while (iterator.hasNext())
        {
            ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
            System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
        }
結(jié)果:value:zset-1score:2.0
value:zset-2score:4.0
value:zset-3score:6.0
value:zset-4score:12.0

  • Long intersectAndStore(K key, Collection<K> otherKeys, K destKey);
    計(jì)算給定的一個(gè)或多個(gè)有序集的交集并將結(jié)果集存儲(chǔ)在新的有序集合 key 中
使用:List<String> stringList = new ArrayList<String>();
        stringList.add("zzset2");
        stringList.add("zzset3");
        System.out.println(template.opsForZSet().intersectAndStore("zzset1",stringList,"destZset44"));

        Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores("destZset44",0,-1);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
        while (iterator.hasNext())
        {
            ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
            System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
        }
結(jié)果:value:zset-1score:3.0
value:zset-2score:6.0
value:zset-3score:9.0
value:zset-4score:18.0

  • Cursor<TypedTuple<V>> scan(K key, ScanOptions options);
    遍歷zset
使用: Cursor<ZSetOperations.TypedTuple<Object>> cursor = template.opsForZSet().scan("zzset1", ScanOptions.NONE);
        while (cursor.hasNext()){
            ZSetOperations.TypedTuple<Object> item = cursor.next();
            System.out.println(item.getValue() + ":" + item.getScore());
        }
結(jié)果:zset-1:1.0
zset-2:2.0
zset-3:3.0
zset-4:6.0

注:TimeUnit是java.util.concurrent包下面的一個(gè)類,表示給定單元粒度的時(shí)間段
常用的顆粒度
TimeUnit.DAYS //天
TimeUnit.HOURS //小時(shí)
TimeUnit.MINUTES //分鐘
TimeUnit.SECONDS //秒
TimeUnit.MILLISECONDS //毫秒


使用場(chǎng)景

  • string
    此類型和memcache相似浩聋,作為常規(guī)的key-value緩存應(yīng)用观蜗。
    例如微博數(shù)、粉絲數(shù)等
    :一個(gè)鍵最大能存儲(chǔ)512MB

  • hash
    redis hash是一個(gè)string類型的field和value的映射表衣洁,hash特別適合用于存儲(chǔ)對(duì)象(應(yīng)為對(duì)象可能會(huì)包含很多屬性)
    常用命令:hget hset hgetall
    主要用來存儲(chǔ)對(duì)象信息

    image
  • list
    list列表是簡(jiǎn)單的字符串列表墓捻,按照插入順序排序(內(nèi)部實(shí)現(xiàn)為L(zhǎng)inkedList),可以選擇將一個(gè)鏈表插入到頭部或尾部
    常用命令 :lpush(添加左邊元素),rpush,lpop(移除左邊第一個(gè)元素),rpop,lrange(獲取列表片段坊夫,LRANGE key start stop)等砖第。
    應(yīng)用場(chǎng)景:Redis list的應(yīng)用場(chǎng)景非常多,也是Redis最重要的數(shù)據(jù)結(jié)構(gòu)之一环凿,比如twitter的關(guān)注列表梧兼,粉絲列表等都可以用Redis的list結(jié)構(gòu)來實(shí)現(xiàn)。

  • set
    案例:在微博中智听,可以將一個(gè)用戶所有的關(guān)注人存在一個(gè)集合中羽杰,將其所有粉絲存在一個(gè)集合。Redis還為集合提供了求交集到推、并集考赛、差集等操作,可以非常方便的實(shí)現(xiàn)如共同關(guān)注莉测、共同喜好颜骤、二度好友等功能,對(duì)上面的所有集合操作悔雹,你還可以使用不同的命令選擇將結(jié)果返回給客戶端還是存集到一個(gè)新的集合中复哆。

  • zset
    常用命令:zadd,zrange
    實(shí)現(xiàn)方式:Redis sorted set的內(nèi)部使用HashMap和跳躍表(SkipList)來保證數(shù)據(jù)的存儲(chǔ)和有序,HashMap里放的是成員到score的映射腌零,跳躍表按score從小到大保存所有集合元素梯找。使用跳躍表的結(jié)構(gòu)可以獲得比較高的查找效率,并且在實(shí)現(xiàn)上比較簡(jiǎn)單益涧。時(shí)間復(fù)雜度與紅黑樹相同锈锤,增加、刪除的操作較為簡(jiǎn)單闲询。
    輸入方式
    應(yīng)用場(chǎng)景:排行榜


http://www.reibang.com/p/7bf5dc61ca06
redisTemplate事務(wù)問題:http://www.reibang.com/p/c9f5718e58f0

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末久免,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子扭弧,更是在濱河造成了極大的恐慌阎姥,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,496評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鸽捻,死亡現(xiàn)場(chǎng)離奇詭異呼巴,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)御蒲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門衣赶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人厚满,你說我怎么就攤上這事府瞄。” “怎么了碘箍?”我有些...
    開封第一講書人閱讀 162,632評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵遵馆,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我丰榴,道長(zhǎng)团搞,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,180評(píng)論 1 292
  • 正文 為了忘掉前任多艇,我火速辦了婚禮逻恐,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘峻黍。我一直安慰自己复隆,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,198評(píng)論 6 388
  • 文/花漫 我一把揭開白布姆涩。 她就那樣靜靜地躺著挽拂,像睡著了一般。 火紅的嫁衣襯著肌膚如雪骨饿。 梳的紋絲不亂的頭發(fā)上亏栈,一...
    開封第一講書人閱讀 51,165評(píng)論 1 299
  • 那天台腥,我揣著相機(jī)與錄音,去河邊找鬼绒北。 笑死黎侈,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的闷游。 我是一名探鬼主播峻汉,決...
    沈念sama閱讀 40,052評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼脐往!你這毒婦竟也來了休吠?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,910評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤业簿,失蹤者是張志新(化名)和其女友劉穎瘤礁,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體梅尤,經(jīng)...
    沈念sama閱讀 45,324評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蔚携,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,542評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了克饶。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片酝蜒。...
    茶點(diǎn)故事閱讀 39,711評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖矾湃,靈堂內(nèi)的尸體忽然破棺而出亡脑,到底是詐尸還是另有隱情,我是刑警寧澤邀跃,帶...
    沈念sama閱讀 35,424評(píng)論 5 343
  • 正文 年R本政府宣布霉咨,位于F島的核電站,受9級(jí)特大地震影響拍屑,放射性物質(zhì)發(fā)生泄漏途戒。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,017評(píng)論 3 326
  • 文/蒙蒙 一僵驰、第九天 我趴在偏房一處隱蔽的房頂上張望喷斋。 院中可真熱鬧,春花似錦蒜茴、人聲如沸星爪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽顽腾。三九已至,卻和暖如春诺核,著一層夾襖步出監(jiān)牢的瞬間抄肖,已是汗流浹背久信。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留漓摩,地道東北人裙士。 一個(gè)月前我還...
    沈念sama閱讀 47,722評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像幌甘,于是被迫代替她去往敵國和親潮售。 傳聞我的和親對(duì)象是個(gè)殘疾皇子痊项,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,611評(píng)論 2 353