RedisTemplate與zset


 

Redis 數據結構簡介

Redis 可以存儲鍵與5種不同數據結構類型之間的映射,這5種數據結構類型分別為String(字符串)、List(列表)、Set(集合)、Hash(散列)和 Zset(有序集合)。

下面來對這5種數據結構類型作簡單的介紹:

結構類型 結構存儲的值 結構的讀寫能力
String 可以是字符串、整數或者浮點數 對整個字符串或者字符串的其中一部分執行操作;對象和浮點數執行自增(increment)或者自減(decrement)
List 一個鏈表,鏈表上的每個節點都包含了一個字符串 從鏈表的兩端推入或者彈出元素;根據偏移量對鏈表進行修剪(trim);讀取單個或者多個元素;根據值來查找或者移除元素
Set 包含字符串的無序收集器(unorderedcollection),並且被包含的每個字符串都是獨一無二的、各不相同 添加、獲取、移除單個元素;檢查一個元素是否存在於某個集合中;計算交集、並集、差集;從集合里賣弄隨機獲取元素
Hash 包含鍵值對的無序散列表 添加、獲取、移除單個鍵值對;獲取所有鍵值對
Zset 字符串成員(member)與浮點數分值(score)之間的有序映射,元素的排列順序由分值的大小決定 添加、獲取、刪除單個元素;根據分值范圍(range)或者成員來獲取元素

Redis 5種數據結構的概念大致介紹到這邊,下面將結合Spring封裝的RedisTemplate來對這5種數據結構的運用進行演示

RedisTemplate介紹

spring 封裝了 RedisTemplate 對象來進行對redis的各種操作,它支持所有的 redis 原生的 api。

RedisTemplate在spring代碼中的結構如下:

  1. org.springframework.data.redis.core
  2. Class RedisTemplate<K,V>
  3. java.lang.Object
  4. org.springframework.data.redis.core.RedisAccessor
  5. org.springframework.data.redis.core.RedisTemplate<K,V>

Type Parameters:
K

  • the Redis key type against which the template works (usually a String)
    模板中的Redis key的類型(通常為String)如:RedisTemplate<String, Object>
    注意:如果沒特殊情況,切勿定義成RedisTemplate<Object, Object>,否則根據里氏替換原則,使用的時候會造成類型錯誤 。
    V
  • the Redis value type against which the template works
    模板中的Redis value的類型

RedisTemplate中定義了對5種數據結構操作

  1. redisTemplate.opsForValue(); //操作字符串
  2. redisTemplate.opsForHash(); //操作hash
  3. redisTemplate.opsForList(); //操作list
  4. redisTemplate.opsForSet(); //操作set
  5. redisTemplate.opsForZSet(); //操作有序set

StringRedisTemplate與RedisTemplate

  • 兩者的關系是StringRedisTemplate繼承RedisTemplate。

  • 兩者的數據是不共通的;也就是說StringRedisTemplate只能管理StringRedisTemplate里面的數據,RedisTemplate只能管理RedisTemplate中的數據。

  • SDR默認采用的序列化策略有兩種,一種是String的序列化策略,一種是JDK的序列化策略。

    StringRedisTemplate默認采用的是String的序列化策略,保存的key和value都是采用此策略序列化保存的。

    RedisTemplate默認采用的是JDK的序列化策略,保存的key和value都是采用此策略序列化保存的。

RedisTemplate配置如下:

  1. @Bean
  2. public RedisTemplate< String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
  3. {
  4. Jackson2JsonRedisSerializer< Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
  5. ObjectMapper om = new ObjectMapper();
  6. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  7. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  8. jackson2JsonRedisSerializer.setObjectMapper(om);
  9. RedisTemplate< String, Object> template = new RedisTemplate<String, Object>();
  10. template.setConnectionFactory(redisConnectionFactory);
  11. template.setKeySerializer(jackson2JsonRedisSerializer);
  12. template.setValueSerializer(jackson2JsonRedisSerializer);
  13. template.setHashKeySerializer(jackson2JsonRedisSerializer);
  14. template.setHashValueSerializer(jackson2JsonRedisSerializer);
  15. template.afterPropertiesSet();
  16. return template;
  17. }

Redis的String數據結構 (推薦使用StringRedisTemplate)

注意:如果使用RedisTemplate需要更改序列化方式

  1. RedisSerializer<String> stringSerializer = new StringRedisSerializer();
  2. template.setKeySerializer(stringSerializer );
  3. template.setValueSerializer(stringSerializer );
  4. template.setHashKeySerializer(stringSerializer );
  5. template.setHashValueSerializer(stringSerializer );

public interface ValueOperations<K,V>
Redis operations for simple (or in Redis terminology 'string') values.
ValueOperations可以對String數據結構進行操作:

  • set void set(K key, V value);
  1. 使用:redisTemplate.opsForValue(). set("name","tom");
  2. 結果:redisTemplate.opsForValue(). get("name") 輸出結果為tom
  • set void set(K key, V value, long timeout, TimeUnit unit);
  1. 使用:redisTemplate.opsForValue(). set("name","tom",10, TimeUnit.SECONDS);
  2. 結果:redisTemplate.opsForValue(). get("name")由於設置的是10秒失效,十秒之內查詢有結果,十秒之后返回為null
  • set void set(K key, V value, long offset);
    該方法是用 value 參數覆寫(overwrite)給定 key 所儲存的字符串值,從偏移量 offset 開始
  1. 使用: template.opsForValue().set("key","hello world");
  2. template.opsForValue().set("key","redis", 6);
  3. System.out.println( "***************"+template.opsForValue().get("key"));
  4. 結果:***************hello redis
  • setIfAbsent Boolean setIfAbsent(K key, V value);
  1. 使用:System.out. println(template.opsForValue().setIfAbsent("multi1","multi1"));//false multi1之前已經存在
  2. System.out. println(template.opsForValue().setIfAbsent("multi111","multi111"));//true multi111之前不存在
  3. 結果: false
  4. true
  • multiSet void multiSet(Map<? extends K, ? extends V> m);
    為多個鍵分別設置它們的值
  1. 使用: Map<String,String> maps = new HashMap<String, String>();
  2. maps.put( "multi1","multi1");
  3. maps.put( "multi2","multi2");
  4. maps.put( "multi3","multi3");
  5. template.opsForValue().multiSet(maps);
  6. List< String> keys = new ArrayList<String>();
  7. keys.add( "multi1");
  8. keys.add( "multi2");
  9. keys.add( "multi3");
  10. System.out.println(template.opsForValue().multiGet(keys));
  11. 結果:[multi1, multi2, multi3]
  • multiSetIfAbsent Boolean multiSetIfAbsent(Map<? extends K, ? extends V> m);
    為多個鍵分別設置它們的值,如果存在則返回false,不存在返回true
  1. 使用: Map<String,String> maps = new HashMap<String, String>();
  2. maps.put( "multi11","multi11");
  3. maps.put( "multi22","multi22");
  4. maps.put( "multi33","multi33");
  5. Map<String,String> maps2 = new HashMap<String, String>();
  6. maps2.put( "multi1","multi1");
  7. maps2.put( "multi2","multi2");
  8. maps2.put( "multi3","multi3");
  9. System.out.println(template.opsForValue().multiSetIfAbsent(maps));
  10. System.out.println(template.opsForValue().multiSetIfAbsent(maps2));
  11. 結果: true
  12. false
  • get V get(Object key);
  1. 使用:template.opsForValue(). set("key","hello world");
  2. System.out.println("***************"+template.opsForValue().get("key"));
  3. 結果:***************hello world
  • getAndSet V getAndSet(K key, V value);
    設置鍵的字符串值並返回其舊值
  1. 使用: template.opsForValue().set("getSetTest","test");
  2. System.out.println( template.opsForValue().getAndSet("getSetTest","test2"));
  3. 結果:test
  • multiGet List<V> multiGet(Collection<K> keys);
    為多個鍵分別取出它們的值
  1. 使用: Map<String,String> maps = new HashMap<String, String>();
  2. maps.put( "multi1","multi1");
  3. maps.put( "multi2","multi2");
  4. maps.put( "multi3","multi3");
  5. template.opsForValue().multiSet(maps);
  6. List< String> keys = new ArrayList<String>();
  7. keys.add( "multi1");
  8. keys.add( "multi2");
  9. keys.add( "multi3");
  10. System.out.println(template.opsForValue().multiGet(keys));
  11. 結果:[multi1, multi2, multi3]
  • increment Long increment(K key, long delta);
    支持整數
  1. 使用: template.opsForValue().increment("increlong",1);
  2. System.out.println("***************"+template.opsForValue().get("increlong"));
  3. 結果:***************1
  • increment Double increment(K key, double delta);
    也支持浮點數
  1. 使用: template.opsForValue().increment("increlong",1.2);
  2. System.out.println("***************"+template.opsForValue().get("increlong"));
  3. 結果:***************2 .2
  • append Integer append(K key, String value);
    如果key已經存在並且是一個字符串,則該命令將該值追加到字符串的末尾。如果鍵不存在,則它被創建並設置為空字符串,因此APPEND在這種特殊情況下將類似於SET。
  1. 使用: template.opsForValue().append("appendTest","Hello");
  2. System.out.println(template.opsForValue().get("appendTest"));
  3. template.opsForValue().append("appendTest","world");
  4. System.out.println(template.opsForValue().get("appendTest"));
  5. 結果: Hello
  6. Helloworld
  • get String get(K key, long start, long end);
    截取key所對應的value字符串
  1. 使用:appendTest對應的 value為Helloworld
  2. System. out.println("*********"+template.opsForValue().get("appendTest",0,5));
  3. 結果:*********Hellow
  4. 使用:System. out.println("*********"+template.opsForValue().get("appendTest",0,-1));
  5. 結果:*********Helloworld
  6. 使用:System. out.println("*********"+template.opsForValue().get("appendTest",-3,-1));
  7. 結果:*********rld
  • size Long size(K key);
    返回key所對應的value值得長度
  1. 使用: template.opsForValue().set("key","hello world");
  2. System.out.println( "***************"+template.opsForValue().size("key"));
  3. 結果:*************** 11
  • setBit Boolean setBit(K key, long offset, boolean value);
    對 key 所儲存的字符串值,設置或清除指定偏移量上的位(bit)
    key鍵對應的值value對應的ascii碼,在offset的位置(從左向右數)變為value
  1. 使用: template.opsForValue().set("bitTest","a");
  2. // 'a' 的ASCII碼是 97。轉換為二進制是:01100001
  3. // 'b' 的ASCII碼是 98 轉換為二進制是:01100010
  4. // 'c' 的ASCII碼是 99 轉換為二進制是:01100011
  5. //因為二進制只有0和1,在setbit中true為1,false為0,因此我要變為'b'的話第六位設置為1,第七位設置為0
  6. template.opsForValue().setBit("bitTest",6, true);
  7. template.opsForValue().setBit("bitTest",7, false);
  8. System.out.println( template.opsForValue().get("bitTest"));
  9. 結果:b
  • getBit Boolean getBit(K key, long offset);
    獲取鍵對應值的ascii碼的在offset處位值
  1. 使用: System.out.println(template.opsForValue().getBit("bitTest",7));
  2. 結果: false

Redis的List數據結構

這邊我們把RedisTemplate序列化方式改回之前的

  1. Jackson2JsonRedisSerializer< Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
  2. ObjectMapper om = new ObjectMapper();
  3. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  4. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  5. jackson2JsonRedisSerializer.setObjectMapper(om);
  6. RedisTemplate< String, Object> template = new RedisTemplate<String, Object>();
  7. template.setKeySerializer(jackson2JsonRedisSerializer);
  8. template.setValueSerializer(jackson2JsonRedisSerializer);
  9. template.setHashKeySerializer(jackson2JsonRedisSerializer);
  10. template.setHashValueSerializer(jackson2JsonRedisSerializer);

public interface ListOperations<K,V>
Redis列表是簡單的字符串列表,按照插入順序排序。你可以添加一個元素導列表的頭部(左邊)或者尾部(右邊)
ListOperations專門操作list列表:

  • List<V> range(K key, long start, long end);
    返回存儲在鍵中的列表的指定元素。偏移開始和停止是基於零的索引,其中0是列表的第一個元素(列表的頭部),1是下一個元素
  1. 使用: System.out.println(template.opsForList().range("list",0,-1));
  2. 結果:[ c#, c++, python, java, c#, c#]
  • void trim(K key, long start, long end);
    修剪現有列表,使其只包含指定的指定范圍的元素,起始和停止都是基於0的索引
  1. 使用: System.out.println(template.opsForList().range("list",0,-1));
  2. template.opsForList().trim( "list",1,-1);//裁剪第一個元素
  3. System.out.println(template.opsForList().range("list",0,-1));
  4. 結果:[ c#, c++, python, java, c#, c#]
  5. [ c++, python, java, c#, c#]
  • Long size(K key);
    返回存儲在鍵中的列表的長度。如果鍵不存在,則將其解釋為空列表,並返回0。當key存儲的值不是列表時返回錯誤。
  1. 使用: System.out.println(template.opsForList().size("list"));
  2. 結果 :6
  • Long leftPush(K key, V value);
    將所有指定的值插入存儲在鍵的列表的頭部。如果鍵不存在,則在執行推送操作之前將其創建為空列表。(從左邊插入)
  1. 使用: template.opsForList().leftPush("list","java");
  2. template.opsForList().leftPush("list","python");
  3. template.opsForList().leftPush("list","c++");
  4. 結果:返回的結果為推送操作后的列表的長度
  5. 1
  6. 2
  7. 3
  • Long leftPushAll(K key, V... values);
    批量把一個數組插入到列表中
  1. 使用: String[] stringarrays = new String[]{"1","2","3"};
  2. template.opsForList().leftPushAll( "listarray",stringarrays);
  3. System.out.println(template.opsForList().range( "listarray",0,-1));
  4. 結果:[ 3, 2, 1]
  • Long leftPushAll(K key, Collection<V> values);
    批量把一個集合插入到列表中
  1. 使用:List<Object> strings = new ArrayList<Object>();
  2. strings. add("1");
  3. strings. add("2");
  4. strings. add("3");
  5. template.opsForList().leftPushAll( "listcollection4", strings);
  6. System. out.println(template.opsForList().range("listcollection4",0,-1));
  7. 結果:[ 3, 2, 1]
  • Long leftPushIfPresent(K key, V value);
    只有存在key對應的列表才能將這個value值插入到key所對應的列表中
  1. 使用:System.out. println(template.opsForList().leftPushIfPresent("leftPushIfPresent","aa"));
  2. System.out. println(template.opsForList().leftPushIfPresent("leftPushIfPresent","bb"));
  3. ==========分割線===========
  4. System.out. println(template.opsForList().leftPush("leftPushIfPresent","aa"));
  5. System.out. println(template.opsForList().leftPushIfPresent("leftPushIfPresent","bb"));
  6. 結果:
  7. 0
  8. 0
  9. ==========分割線===========
  10. 1
  11. 2
  • Long leftPush(K key, V pivot, V value);
    把value值放到key對應列表中pivot值的左面,如果pivot值存在的話
  1. 使用:template.opsForList().leftPush( "list","java","oc");
  2. System.out.print(template.opsForList().range("list",0,-1));
  3. 結果:[ c++, python, oc, java, c#, c#]
  • Long rightPush(K key, V value);
    將所有指定的值插入存儲在鍵的列表的頭部。如果鍵不存在,則在執行推送操作之前將其創建為空列表。(從右邊插入)
  1. 使用: template.opsForList().rightPush("listRight","java");
  2. template.opsForList().rightPush("listRight","python");
  3. template.opsForList().rightPush("listRight","c++");
  4. 結果:
  5. 1
  6. 2
  7. 3
  • Long rightPushAll(K key, V... values);
  1. 使用: String[] stringarrays = new String[]{"1","2","3"};
  2. template.opsForList().rightPushAll( "listarrayright",stringarrays);
  3. System.out.println(template.opsForList().range( "listarrayright",0,-1));
  4. 結果:[ 1, 2, 3]
  • Long rightPushAll(K key, Collection<V> values);
  1. 使用:List<Object> strings = new ArrayList<Object>();
  2. strings. add("1");
  3. strings. add("2");
  4. strings. add("3");
  5. template.opsForList().rightPushAll( "listcollectionright", strings);
  6. System. out.println(template.opsForList().range("listcollectionright",0,-1));
  7. 結果:[ 1, 2, 3]
  • Long rightPushIfPresent(K key, V value);
    只有存在key對應的列表才能將這個value值插入到key所對應的列表中
  1. 使用:System.out. println(template.opsForList().rightPushIfPresent("rightPushIfPresent","aa"));
  2. System.out. println(template.opsForList().rightPushIfPresent("rightPushIfPresent","bb"));
  3. System.out. println("==========分割線===========");
  4. System.out. println(template.opsForList().rightPush("rightPushIfPresent","aa"));
  5. System.out. println(template.opsForList().rightPushIfPresent("rightPushIfPresent","bb"));
  6. 結果: 0
  7. 0
  8. ==========分割線===========
  9. 1
  10. 2
  • Long rightPush(K key, V pivot, V value);
    把value值放到key對應列表中pivot值的右面,如果pivot值存在的話
  1. 使用: System.out.println(template.opsForList().range("listRight",0,-1));
  2. template.opsForList().rightPush("listRight","python","oc");
  3. System.out.println(template.opsForList().range("listRight",0,-1));
  4. 結果: [java, python, c++]
  5. [java, python, oc, c++]
  • void set(K key, long index, V value);
    在列表中index的位置設置value值
  1. 使用: System.out.println(template.opsForList().range("listRight",0,-1));
  2. template.opsForList().set("listRight",1,"setValue");
  3. System.out.println(template.opsForList().range("listRight",0,-1));
  4. 結果: [java, python, oc, c++]
  5. [java, setValue, oc, c++]
  • Long remove(K key, long count, Object value);
    從存儲在鍵中的列表中刪除等於值的元素的第一個計數事件。
    計數參數以下列方式影響操作:
    count> 0:刪除等於從頭到尾移動的值的元素。
    count <0:刪除等於從尾到頭移動的值的元素。
    count = 0:刪除等於value的所有元素。
  1. 使用:System.out. println(template.opsForList().range("listRight",0,-1));
  2. template.opsForList().remove( "listRight",1,"setValue");//將刪除列表中存儲的列表中第一次次出現的“setValue”。
  3. System.out. println(template.opsForList().range("listRight",0,-1));
  4. 結果:[java, setValue, oc, c++]
  5. [java, oc, c++]
  • V index(K key, long index);
    根據下表獲取列表中的值,下標是從0開始的
  1. 使用: System.out.println(template.opsForList().range("listRight",0,-1));
  2. System.out.println(template.opsForList().index("listRight",2));
  3. 結果: [java, oc, c++]
  4. c++
  • V leftPop(K key);
    彈出最左邊的元素,彈出之后該值在列表中將不復存在
  1. 使用: System.out.println(template.opsForList().range("list",0,-1));
  2. System.out.println(template.opsForList().leftPop("list"));
  3. System.out.println(template.opsForList().range("list",0,-1));
  4. 結果:
  5. [c++, python, oc, java, c#, c#]
  6. c++
  7. [python, oc, java, c#, c#]
  • V leftPop(K key, long timeout, TimeUnit unit);
    移出並獲取列表的第一個元素, 如果列表沒有元素會阻塞列表直到等待超時或發現可彈出元素為止。
使用:用法與 leftPop(K key);一樣 
  • V rightPop(K key);
    彈出最右邊的元素,彈出之后該值在列表中將不復存在
  1. 使用: System.out.println(template.opsForList().range("list",0,-1));
  2. System.out.println(template.opsForList().rightPop("list"));
  3. System.out.println(template.opsForList().range("list",0,-1));
  4. 結果: [python, oc, java, c#, c#]
  5. c#
  6. [python, oc, java, c#]
  • V rightPop(K key, long timeout, TimeUnit unit);
    移出並獲取列表的最后一個元素, 如果列表沒有元素會阻塞列表直到等待超時或發現可彈出元素為止。
使用:用法與 rightPop(K key);一樣 
  • V rightPopAndLeftPush(K sourceKey, K destinationKey);
    用於移除列表的最后一個元素,並將該元素添加到另一個列表並返回。
  1. 使用: System.out.println(template.opsForList().range("list",0,-1));
  2. template.opsForList().rightPopAndLeftPush("list","rightPopAndLeftPush");
  3. System.out.println(template.opsForList().range("list",0,-1));
  4. System.out.println(template.opsForList().range("rightPopAndLeftPush",0,-1));
  5. 結果: [oc, java,c#]
  6. [oc, java]
  7. [c#]
  • V rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit);
    用於移除列表的最后一個元素,並將該元素添加到另一個列表並返回,如果列表沒有元素會阻塞列表直到等待超時或發現可彈出元素為止。
使用:用法與rightPopAndLeftPush(K sourceKey, K destinationKey)一樣

Redis的Hash數據機構

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

  1. 初始數據:
  2. //template.opsForHash().put("redisHash","name","tom");
  3. //template.opsForHash().put("redisHash","age",26);
  4. //template.opsForHash().put("redisHash","class","6");
  5.  
  6. //Map<String,Object> testMap = new HashMap();
  7. //testMap.put("name","jack");
  8. //testMap.put("age",27);
  9. //testMap.put("class","1");
  10. //template.opsForHash().putAll("redisHash1",testMap);
  • Long delete(H key, Object... hashKeys);
    刪除給定的哈希hashKeys
  1. 使用:System. out.println(template.opsForHash().delete("redisHash","name"));
  2. System. out.println(template.opsForHash().entries("redisHash"));
  3. 結果: 1
  4. { class=6, age=28.1}
  • Boolean hasKey(H key, Object hashKey);
    確定哈希hashKey是否存在
  1. 使用: System.out.println(template.opsForHash().hasKey("redisHash","age"));
  2. System.out.println(template.opsForHash().hasKey("redisHash","ttt"));
  3. 結果: true
  4. false
  • HV get(H key, Object hashKey);
    從鍵中的哈希獲取給定hashKey的值
  1. 使用: System.out.println(template.opsForHash().get("redisHash","age"));
  2. 結果:26
  • List<HV> multiGet(H key, Collection<HK> hashKeys);
    從哈希中獲取給定hashKey的值
  1. 使用:List<Object> kes = new ArrayList<Object>();
  2. kes. add("name");
  3. kes. add("age");
  4. System. out.println(template.opsForHash().multiGet("redisHash",kes));
  5. 結果:[jack, 28.1]
  • Long increment(H key, HK hashKey, long delta);
    通過給定的delta增加散列hashKey的值(整型)
  1. 使用: System.out.println(template.opsForHash().get("redisHash","age"));
  2. System.out.println(template.opsForHash().increment("redisHash","age",1));
  3. 結果:26
  4. 27
  • Double increment(H key, HK hashKey, double delta);
    通過給定的delta增加散列hashKey的值(浮點數)
  1. 使用: System.out.println(template.opsForHash().get("redisHash","age"));
  2. System.out.println(template.opsForHash().increment("redisHash","age",1.1));
  3. 結果:27
  4. 28 .1
  • Set<HK> keys(H key);
    獲取key所對應的散列表的key
  1. 使用:System. out.println(template.opsForHash().keys("redisHash1"));
  2. //redisHash1所對應的散列表為{class=1, name=jack, age=27}
  3. 結果:[name, class, age]
  • Long size(H key);
    獲取key所對應的散列表的大小個數
  1. 使用:System.out. println(template.opsForHash().size("redisHash1"));
  2. //redisHash1所對應的散列表為{class=1, name=jack, age=27}
  3. 結果: 3
  • void putAll(H key, Map<? extends HK, ? extends HV> m);
    使用m中提供的多個散列字段設置到key對應的散列表中
  1. 使用: Map<String,Object> testMap = new HashMap();
  2. testMap.put( "name","jack");
  3. testMap.put( "age",27);
  4. testMap.put( "class","1");
  5. template.opsForHash().putAll( "redisHash1",testMap);
  6. System.out.println(template.opsForHash().entries( "redisHash1"));
  7. 結果:{ class=1, name=jack, age=27}
  • void put(H key, HK hashKey, HV value);
    設置散列hashKey的值
  1. 使用:template.opsForHash().put( "redisHash","name","tom");
  2. template.opsForHash().put( "redisHash","age",26);
  3. template.opsForHash().put( "redisHash","class","6");
  4. System. out.println(template.opsForHash().entries("redisHash"));
  5. 結果:{age= 26, class=6, name=tom}
  • Boolean putIfAbsent(H key, HK hashKey, HV value);
    僅當hashKey不存在時才設置散列hashKey的值。
  1. 使用:System.out. println(template.opsForHash().putIfAbsent("redisHash","age",30));
  2. System.out. println(template.opsForHash().putIfAbsent("redisHash","kkk","kkk"));
  3. 結果: false
  4. true
  • List<HV> values(H key);
    獲取整個哈希存儲的值根據密鑰
  1. 使用: System.out.println(template.opsForHash().values("redisHash"));
  2. 結果: [tom, 26, 6]
  • Map<HK, HV> entries(H key);
    獲取整個哈希存儲根據密鑰
  1. 使用:System. out.println(template.opsForHash().entries("redisHash"));
  2. 結果:{age= 26, class=6, name=tom}
  • Cursor<Map.Entry<HK, HV>> scan(H key, ScanOptions options);
    使用Cursor在key的hash中迭代,相當於迭代器。
  1. 使用:Cursor<Map.Entry<Object, Object>> curosr = template.opsForHash().scan( "redisHash", ScanOptions.ScanOptions.NONE);
  2. while(curosr.hasNext()){
  3. Map.Entry<Object, Object> entry = curosr.next();
  4. System. out.println(entry.getKey()+":"+entry.getValue());
  5. }
  6. 結果:age: 28.1
  7. class:6
  8. kkk:kkk

Redis的Set數據結構

Redis的Set是string類型的無序集合。集合成員是唯一的,這就意味着集合中不能出現重復的數據。
Redis 中 集合是通過哈希表實現的,所以添加,刪除,查找的復雜度都是O(1)。
public interface SetOperations<K,V>
SetOperations提供了對無序集合的一系列操作:

  • Long add(K key, V... values);
    無序集合中添加元素,返回添加個數
    也可以直接在add里面添加多個值 如:template.opsForSet().add("setTest","aaa","bbb")
  1. 使用: String[] strarrays = new String[]{"strarr1","sgtarr2"};
  2. System.out.println(template.opsForSet().add( "setTest", strarrays));
  3. 結果: 2
  • Long remove(K key, Object... values);
    移除集合中一個或多個成員
  1. 使用: String[] strarrays = new String[]{"strarr1","sgtarr2"};
  2. System.out.println(template.opsForSet().remove( "setTest",strarrays));
  3. 結果: 2
  • V pop(K key);
    移除並返回集合中的一個隨機元素
  1. 使用: System.out.println(template.opsForSet().pop("setTest"));
  2. System.out.println(template.opsForSet().members("setTest"));
  3. 結果: bbb
  4. [aaa, ccc]
  • Boolean move(K key, V value, K destKey);
    將 member 元素從 source 集合移動到 destination 集合
  1. 使用: template.opsForSet().move("setTest","aaa","setTest2");
  2. System.out.println(template.opsForSet().members("setTest"));
  3. System.out.println(template.opsForSet().members("setTest2"));
  4. 結果: [ccc]
  5. [aaa]
  • Long size(K key);
    無序集合的大小長度
  1. 使用: System.out.println(template.opsForSet().size("setTest"));
  2. 結果:1
  • Boolean isMember(K key, Object o);
    判斷 member 元素是否是集合 key 的成員
  1. 使用: System.out.println(template.opsForSet().isMember("setTest","ccc"));
  2. System.out.println(template.opsForSet().isMember("setTest","asd"));
  3. 結果: true
  4. false
  • Set<V> intersect(K key, K otherKey);
    key對應的無序集合與otherKey對應的無序集合求交集
  1. 使用: System.out.println(template.opsForSet().members("setTest"));
  2. System.out.println(template.opsForSet().members("setTest2"));
  3. System.out.println(template.opsForSet().intersect("setTest","setTest2"));
  4. 結果: [aaa, ccc]
  5. [aaa]
  6. [aaa]
  • Set<V> intersect(K key, Collection<K> otherKeys);
    key對應的無序集合與多個otherKey對應的無序集合求交集
  1. 使用:System. out.println(template.opsForSet().members("setTest"));
  2. System. out.println(template.opsForSet().members("setTest2"));
  3. System. out.println(template.opsForSet().members("setTest3"));
  4. List<String> strlist = new ArrayList<String>();
  5. strlist. add("setTest2");
  6. strlist. add("setTest3");
  7. System. out.println(template.opsForSet().intersect("setTest",strlist));
  8. 結果:[aaa, ccc]
  9. [ aaa]
  10. [ ccc, aaa]
  11. [ aaa]
  • Long intersectAndStore(K key, K otherKey, K destKey);
    key無序集合與otherkey無序集合的交集存儲到destKey無序集合中
  1. 使用: System.out.println("setTest:" + template.opsForSet().members("setTest"));
  2. System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
  3. System.out.println(template.opsForSet().intersectAndStore("setTest","setTest2","destKey1"));
  4. System.out.println(template.opsForSet().members("destKey1"));
  5. 結果: setTest:[ddd, bbb, aaa, ccc]
  6. setTest2:[ccc, aaa]
  7. 2
  8. [aaa, ccc]
  • Long intersectAndStore(K key, Collection<K> otherKeys, K destKey);
    key對應的無序集合與多個otherKey對應的無序集合求交集存儲到destKey無序集合中
  1. 使用:System. out.println("setTest:" + template.opsForSet().members("setTest"));
  2. System. out.println("setTest2:" + template.opsForSet().members("setTest2"));
  3. System. out.println("setTest3:" + template.opsForSet().members("setTest3"));
  4. List<String> strlist = new ArrayList<String>();
  5. strlist. add("setTest2");
  6. strlist. add("setTest3");
  7. System. out.println(template.opsForSet().intersectAndStore("setTest",strlist,"destKey2"));
  8. System. out.println(template.opsForSet().members("destKey2"));
  9. 結果:setTest:[ddd, bbb, aaa, ccc]
  10. setTest2:[ccc, aaa]
  11. setTest3:[ccc, aaa]
  12. 2
  13. [ aaa, ccc]
  • Set<V> union(K key, K otherKey);
    key無序集合與otherKey無序集合的並集
  1. 使用: System.out.println("setTest:" + template.opsForSet().members("setTest"));
  2. System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
  3. System.out.println(template.opsForSet().union("setTest","setTest2"));
  4. 結果: setTest:[ddd, bbb, aaa, ccc]
  5. setTest2:[ccc, aaa]
  6. [ccc, aaa, ddd, bbb]
  • Set<V> union(K key, Collection<K> otherKeys);
    key無序集合與多個otherKey無序集合的並集
  1. 使用:System. out.println("setTest:" + template.opsForSet().members("setTest"));
  2. System. out.println("setTest2:" + template.opsForSet().members("setTest2"));
  3. System. out.println("setTest3:" + template.opsForSet().members("setTest3"));
  4. List<String> strlist = new ArrayList<String>();
  5. strlist. add("setTest2");
  6. strlist. add("setTest3");
  7. System. out.println(template.opsForSet().union("setTest",strlist));
  8. 結果:setTest:[ddd, bbb, aaa, ccc]
  9. setTest2:[ccc, aaa]
  10. setTest3:[xxx, ccc, aaa]
  11. [ ddd, xxx, bbb, aaa, ccc]
  • Long unionAndStore(K key, K otherKey, K destKey);
    key無序集合與otherkey無序集合的並集存儲到destKey無序集合中
  1. 使用: System.out.println("setTest:" + template.opsForSet().members("setTest"));
  2. System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
  3. System.out.println(template.opsForSet().unionAndStore("setTest","setTest2","unionAndStoreTest1"));
  4. System.out.println("unionAndStoreTest1:" + template.opsForSet().members("unionAndStoreTest1"));
  5. 結果: setTest:[ddd, bbb, aaa, ccc]
  6. setTest2:[ccc, aaa]
  7. 4
  8. unionAndStoreTest1:[ccc, aaa, ddd, bbb]
  • Long unionAndStore(K key, Collection<K> otherKeys, K destKey);
    key無序集合與多個otherkey無序集合的並集存儲到destKey無序集合中
  1. 使用:System. out.println("setTest:" + template.opsForSet().members("setTest"));
  2. System. out.println("setTest2:" + template.opsForSet().members("setTest2"));
  3. System. out.println("setTest3:" + template.opsForSet().members("setTest3"));
  4. List<String> strlist = new ArrayList<String>();
  5. strlist. add("setTest2");
  6. strlist. add("setTest3");
  7. System. out.println(template.opsForSet().unionAndStore("setTest",strlist,"unionAndStoreTest2"));
  8. System. out.println("unionAndStoreTest2:" + template.opsForSet().members("unionAndStoreTest2"));
  9. 結果:setTest:[ddd, bbb, aaa, ccc]
  10. setTest2:[ccc, aaa]
  11. setTest3:[xxx, ccc, aaa]
  12. 5
  13. unionAndStoreTest2:[ddd, xxx, bbb, aaa, ccc]
  • Set<V> difference(K key, K otherKey);
    key無序集合與otherKey無序集合的差集
  1. 使用: System.out.println("setTest:" + template.opsForSet().members("setTest"));
  2. System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
  3. System.out.println(template.opsForSet().difference("setTest","setTest2"));
  4. 結果: setTest:[ddd, bbb, aaa, ccc]
  5. setTest2:[ccc, aaa]
  6. [bbb, ddd]
  • Set<V> difference(K key, Collection<K> otherKeys);
    key無序集合與多個otherKey無序集合的差集
  1. 使用:System. out.println("setTest:" + template.opsForSet().members("setTest"));
  2. System. out.println("setTest2:" + template.opsForSet().members("setTest2"));
  3. System. out.println("setTest3:" + template.opsForSet().members("setTest3"));
  4. List<String> strlist = new ArrayList<String>();
  5. strlist. add("setTest2");
  6. strlist. add("setTest3");
  7. System. out.println(template.opsForSet().difference("setTest",strlist));
  8. 結果:setTest:[ddd, bbb, aaa, ccc]
  9. setTest2:[ccc, aaa]
  10. setTest3:[xxx, ccc, aaa]
  11. [ bbb, ddd]
  • Long differenceAndStore(K key, K otherKey, K destKey);
    key無序集合與otherkey無序集合的差集存儲到destKey無序集合中
  1. 使用: System.out.println("setTest:" + template.opsForSet().members("setTest"));
  2. System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
  3. System.out.println(template.opsForSet().differenceAndStore("setTest","setTest2","differenceAndStore1"));
  4. System.out.println("differenceAndStore1:" + template.opsForSet().members("differenceAndStore1"));
  5. 結果: setTest:[ddd, bbb, aaa, ccc]
  6. setTest2:[ccc, aaa]
  7. 2
  8. differenceAndStore1:[bbb, ddd]
  • Long differenceAndStore(K key, Collection<K> otherKeys, K destKey);
    key無序集合與多個otherkey無序集合的差集存儲到destKey無序集合中
  1. 使用:System. out.println("setTest:" + template.opsForSet().members("setTest"));
  2. System. out.println("setTest2:" + template.opsForSet().members("setTest2"));
  3. System. out.println("setTest3:" + template.opsForSet().members("setTest3"));
  4. List<String> strlist = new ArrayList<String>();
  5. strlist. add("setTest2");
  6. strlist. add("setTest3");
  7. System. out.println(template.opsForSet().differenceAndStore("setTest",strlist,"differenceAndStore2"));
  8. System. out.println("differenceAndStore2:" + template.opsForSet().members("differenceAndStore2"));
  9. 結果:setTest:[ddd, bbb, aaa, ccc]
  10. setTest2:[ccc, aaa]
  11. setTest3:[xxx, ccc, aaa]
  12. 2
  13. differenceAndStore2:[bbb, ddd]
  • Set<V> members(K key);
    返回集合中的所有成員
  1. 使用: System.out.println(template.opsForSet().members("setTest"));
  2. 結果: [ddd, bbb, aaa, ccc]
  • V randomMember(K key);
    隨機獲取key無序集合中的一個元素
  1. 使用: System.out.println("setTest:" + template.opsForSet().members("setTest"));
  2. System.out.println("setTestrandomMember:" + template.opsForSet().randomMember("setTest"));
  3. System.out.println("setTestrandomMember:" + template.opsForSet().randomMember("setTest"));
  4. System.out.println("setTestrandomMember:" + template.opsForSet().randomMember("setTest"));
  5. System.out.println("setTestrandomMember:" + template.opsForSet().randomMember("setTest"));
  6. 結果: setTest:[ddd, bbb, aaa, ccc]
  7. setTestrandomMember:aaa
  8. setTestrandomMember:bbb
  9. setTestrandomMember:aaa
  10. setTestrandomMember:ddd
  • Set<V> distinctRandomMembers(K key, long count);
    獲取多個key無序集合中的元素(去重),count表示個數
  1. 使用: System.out.println("randomMembers:" + template.opsForSet().distinctRandomMembers("setTest",5));
  2. 結果: randomMembers:[aaa, bbb, ddd, ccc]
  • List<V> randomMembers(K key, long count);
    獲取多個key無序集合中的元素,count表示個數
  1. 使用: System.out.println("randomMembers:" + template.opsForSet().randomMembers("setTest",5));
  2. 結果: randomMembers:[ccc, ddd, ddd, ddd, aaa]
  • Cursor<V> scan(K key, ScanOptions options);
    遍歷set
  1. 使用: Cursor<Object> curosr = template.opsForSet().scan( "setTest", ScanOptions.NONE);
  2. while(curosr.hasNext()){
  3. System.out.println(curosr. next());
  4. }
  5. 結果:ddd
  6. bbb
  7. aaa
  8. ccc

Redis的ZSet數據結構

Redis 有序集合和無序集合一樣也是string類型元素的集合,且不允許重復的成員。
不同的是每個元素都會關聯一個double類型的分數。redis正是通過分數來為集合中的成員進行從小到大的排序。
有序集合的成員是唯一的,但分數(score)卻可以重復。
public interface ZSetOperations<K,V>
ZSetOperations提供了一系列方法對有序集合進行操作:

  • Boolean add(K key, V value, double score);
    新增一個有序集合,存在的話為false,不存在的話為true
  1. 使用: System.out.println(template.opsForZSet().add("zset1","zset-1",1.0));
  2. 結果: true
  • Long add(K key, Set<TypedTuple<V>> tuples);
    新增一個有序集合
  1. 使用:ZSetOperations.TypedTuple< Object> objectTypedTuple1 = new DefaultTypedTuple<Object>("zset-5",9.6);
  2. ZSetOperations.TypedTuple< Object> objectTypedTuple2 = new DefaultTypedTuple<Object>("zset-6",9.9);
  3. Set<ZSetOperations.TypedTuple<Object>> tuples = new HashSet<ZSetOperations.TypedTuple<Object>>();
  4. tuples.add(objectTypedTuple1);
  5. tuples.add(objectTypedTuple2);
  6. System.out.println(template.opsForZSet().add( "zset1",tuples));
  7. System.out.println(template.opsForZSet().range( "zset1",0,-1));
  8. 結果:[zset -1, zset-2, zset-3, zset-4, zset-5, zset-6]
  • Long remove(K key, Object... values);
    從有序集合中移除一個或者多個元素
  1. 使用: System.out.println(template.opsForZSet().range("zset1",0,-1));
  2. System.out.println(template.opsForZSet().remove("zset1","zset-6"));
  3. System.out.println(template.opsForZSet().range("zset1",0,-1));
  4. 結果: [zset-1, zset-2, zset-3, zset-4, zset-5, zset-6]
  5. 1
  6. [zset-1, zset-2, zset-3, zset-4, zset-5]
  • Double incrementScore(K key, V value, double delta);
    增加元素的score值,並返回增加后的值
  1. 使用:System.out. println(template.opsForZSet().incrementScore("zset1","zset-1",1.1)); //原為1.1
  2. 結果: 2.2
  • Long rank(K key, Object o);
    返回有序集中指定成員的排名,其中有序集成員按分數值遞增(從小到大)順序排列
  1. 使用:System.out. println(template.opsForZSet().range("zset1",0,-1));
  2. System.out. println(template.opsForZSet().rank("zset1","zset-2"));
  3. 結果:[zset -2, zset-1, zset-3, zset-4, zset-5]
  4. 0 //表明排名第一
  • Long reverseRank(K key, Object o);
    返回有序集中指定成員的排名,其中有序集成員按分數值遞減(從大到小)順序排列
  1. 使用:System.out. println(template.opsForZSet().range("zset1",0,-1));
  2. System.out. println(template.opsForZSet().reverseRank("zset1","zset-2"));
  3. 結果:[zset -2, zset-1, zset-3, zset-4, zset-5]
  4. 4 //遞減之后排到第五位去了
  • Set<V> range(K key, long start, long end);
    通過索引區間返回有序集合成指定區間內的成員,其中有序集成員按分數值遞增(從小到大)順序排列
  1. 使用: System.out.println(template.opsForZSet().range("zset1",0,-1));
  2. 結果: [zset-2, zset-1, zset-3, zset-4, zset-5]
  • Set<TypedTuple<V>> rangeWithScores(K key, long start, long end);
    通過索引區間返回有序集合成指定區間內的成員對象,其中有序集成員按分數值遞增(從小到大)順序排列
  1. 使用:Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores( "zset1",0,-1);
  2. Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
  3. while (iterator.hasNext())
  4. {
  5. ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
  6. System. out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
  7. }
  8. 結果: value:zset-2score:1.2
  9. value:zset-1score:2.2
  10. value:zset-3score:2.3
  11. value:zset-4score:6.6
  12. value:zset-5score:9.6
  • Set<V> rangeByScore(K key, double min, double max);
    通過分數返回有序集合指定區間內的成員,其中有序集成員按分數值遞增(從小到大)順序排列
  1. 使用: System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));
  2. 結果: [zset-2, zset-1, zset-3]
  • Set<TypedTuple<V>> rangeByScoreWithScores(K key, double min, double max);
    通過分數返回有序集合指定區間內的成員對象,其中有序集成員按分數值遞增(從小到大)順序排列
  1. 使用: Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeByScoreWithScores("zset1",0,5);
  2. Iterator<ZSetOperations.TypedTuple< Object>> iterator = tuples.iterator();
  3. while (iterator.hasNext())
  4. {
  5. ZSetOperations.TypedTuple< Object> typedTuple = iterator.next();
  6. System.out.println( "value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
  7. }
  8. 結果:value:zset -2score:1.2
  9. value:zset -1score:2.2
  10. value:zset -3score:2.3
  • Set<V> rangeByScore(K key, double min, double max, long offset, long count);
    通過分數返回有序集合指定區間內的成員,並在索引范圍內,其中有序集成員按分數值遞增(從小到大)順序排列
  1. 使用: System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));
  2. System.out.println(template.opsForZSet().rangeByScore("zset1",0,5,1,2));
  3. 結果: [zset-2, zset-1, zset-3]
  4. [zset-1, zset-3]
  • Set<TypedTuple<V>> rangeByScoreWithScores(K key, double min, double max, long offset, long count);
    通過分數返回有序集合指定區間內的成員對象,並在索引范圍內,其中有序集成員按分數值遞增(從小到大)順序排列
  1. 使用: Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeByScoreWithScores("zset1",0,5,1,2);
  2. Iterator<ZSetOperations.TypedTuple< Object>> iterator = tuples.iterator();
  3. while (iterator.hasNext())
  4. {
  5. ZSetOperations.TypedTuple< Object> typedTuple = iterator.next();
  6. System.out.println( "value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
  7. }
  8. 結果:value:zset -1score:2.2
  9. value:zset -3score:2.3
  • Set<V> reverseRange(K key, long start, long end);
    通過索引區間返回有序集合成指定區間內的成員,其中有序集成員按分數值遞減(從大到小)順序排列
  1. 使用: System.out.println(template.opsForZSet().reverseRange("zset1",0,-1));
  2. 結果: [zset-5, zset-4, zset-3, zset-1, zset-2]
  • Set<TypedTuple<V>> reverseRangeWithScores(K key, long start, long end);
    通過索引區間返回有序集合成指定區間內的成員對象,其中有序集成員按分數值遞減(從大到小)順序排列
  1. 使用:Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().reverseRangeWithScores( "zset1",0,-1);
  2. Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
  3. while (iterator.hasNext())
  4. {
  5. ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
  6. System. out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
  7. }
  8. 結果: value:zset-5score:9.6
  9. value:zset-4score:6.6
  10. value:zset-3score:2.3
  11. value:zset-1score:2.2
  12. value:zset-2score:1.2
  • Set<V> reverseRangeByScore(K key, double min, double max);
使用:與rangeByScore調用方法一樣,其中有序集成員按分數值遞減(從大到小)順序排列
  • Set<TypedTuple<V>> reverseRangeByScoreWithScores(K key, double min, double max);
使用:與rangeByScoreWithScores調用方法一樣,其中有序集成員按分數值遞減(從大到小)順序排列
  • Set<V> reverseRangeByScore(K key, double min, double max, long offset, long count);
使用:與rangeByScore調用方法一樣,其中有序集成員按分數值遞減(從大到小)順序排列
  • Set<TypedTuple<V>> reverseRangeByScoreWithScores(K key, double min, double max, long offset, long count);
使用:與rangeByScoreWithScores調用方法一樣,其中有序集成員按分數值遞減(從大到小)順序排列
  • Long count(K key, double min, double max);
    通過分數返回有序集合指定區間內的成員個數
  1. 使用: System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));
  2. System.out.println(template.opsForZSet().count("zset1",0,5));
  3. 結果: [zset-2, zset-1, zset-3]
  4. 3
  • Long size(K key);
    獲取有序集合的成員數,內部調用的就是zCard方法
  1. 使用: System.out.println(template.opsForZSet().size("zset1"));
  2. 結果:6
  • Long zCard(K key);
    獲取有序集合的成員數
  1. 使用: System.out.println(template.opsForZSet().zCard("zset1"));
  2. 結果:6
  • Double score(K key, Object o);
    獲取指定成員的score值
  1. 使用: System.out.println(template.opsForZSet().score("zset1","zset-1"));
  2. 結果:2 .2
  • Long removeRange(K key, long start, long end);
    移除指定索引位置的成員,其中有序集成員按分數值遞增(從小到大)順序排列
  1. 使用: System.out.println(template.opsForZSet().range("zset2",0,-1));
  2. System.out.println(template.opsForZSet().removeRange("zset2",1,2));
  3. System.out.println(template.opsForZSet().range("zset2",0,-1));
  4. 結果: [zset-1, zset-2, zset-3, zset-4]
  5. 2
  6. [zset-1, zset-4]
  • Long removeRangeByScore(K key, double min, double max);
    根據指定的score值得范圍來移除成員
  1. 使用: //System.out.println(template.opsForZSet().add("zset2","zset-1",1.1));
  2. //System.out.println(template.opsForZSet().add("zset2","zset-2",1.2));
  3. //System.out.println(template.opsForZSet().add("zset2","zset-3",2.3));
  4. //System.out.println(template.opsForZSet().add("zset2","zset-4",6.6));
  5. System.out. println(template.opsForZSet().range("zset2",0,-1));
  6. System.out. println(template.opsForZSet().removeRangeByScore("zset2",2,3));
  7. System.out. println(template.opsForZSet().range("zset2",0,-1));
  8. 結果:[zset -1, zset-2, zset-3,zset-4]
  9. 1
  10. [zset -1, zset-2, zset-4]
  • Long unionAndStore(K key, K otherKey, K destKey);
    計算給定的一個有序集的並集,並存儲在新的 destKey中,key相同的話會把score值相加
  1. 使用:System. out.println(template.opsForZSet().add("zzset1","zset-1",1.0));
  2. System. out.println(template.opsForZSet().add("zzset1","zset-2",2.0));
  3. System. out.println(template.opsForZSet().add("zzset1","zset-3",3.0));
  4. System. out.println(template.opsForZSet().add("zzset1","zset-4",6.0));
  5.  
  6. System. out.println(template.opsForZSet().add("zzset2","zset-1",1.0));
  7. System. out.println(template.opsForZSet().add("zzset2","zset-2",2.0));
  8. System. out.println(template.opsForZSet().add("zzset2","zset-3",3.0));
  9. System. out.println(template.opsForZSet().add("zzset2","zset-4",6.0));
  10. System. out.println(template.opsForZSet().add("zzset2","zset-5",7.0));
  11. System. out.println(template.opsForZSet().unionAndStore("zzset1","zzset2","destZset11"));
  12.  
  13. Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores( "destZset11",0,-1);
  14. Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
  15. while (iterator.hasNext())
  16. {
  17. ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
  18. System. out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
  19. }
  20. 結果: value:zset-1score:2.0
  21. value:zset-2score:4.0
  22. value:zset-3score:6.0
  23. value:zset-5score:7.0
  24. value:zset-4score:12.0
  • Long unionAndStore(K key, Collection<K> otherKeys, K destKey);
    計算給定的多個有序集的並集,並存儲在新的 destKey中
  1. 使用: //System.out.println(template.opsForZSet().add("zzset1","zset-1",1.0));
  2. //System.out.println(template.opsForZSet().add("zzset1","zset-2",2.0));
  3. //System.out.println(template.opsForZSet().add("zzset1","zset-3",3.0));
  4. //System.out.println(template.opsForZSet().add("zzset1","zset-4",6.0));
  5. //
  6. //System.out.println(template.opsForZSet().add("zzset2","zset-1",1.0));
  7. //System.out.println(template.opsForZSet().add("zzset2","zset-2",2.0));
  8. //System.out.println(template.opsForZSet().add("zzset2","zset-3",3.0));
  9. //System.out.println(template.opsForZSet().add("zzset2","zset-4",6.0));
  10. //System.out.println(template.opsForZSet().add("zzset2","zset-5",7.0));
  11.  
  12. System. out.println(template.opsForZSet().add("zzset3","zset-1",1.0));
  13. System. out.println(template.opsForZSet().add("zzset3","zset-2",2.0));
  14. System. out.println(template.opsForZSet().add("zzset3","zset-3",3.0));
  15. System. out.println(template.opsForZSet().add("zzset3","zset-4",6.0));
  16. System. out.println(template.opsForZSet().add("zzset3","zset-5",7.0));
  17.  
  18. List<String> stringList = new ArrayList<String>();
  19. stringList. add("zzset2");
  20. stringList. add("zzset3");
  21. System. out.println(template.opsForZSet().unionAndStore("zzset1",stringList,"destZset22"));
  22.  
  23. Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores( "destZset22",0,-1);
  24. Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
  25. while (iterator.hasNext())
  26. {
  27. ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
  28. System. out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
  29. }
  30. 結果: value:zset-1score:3.0
  31. value:zset-2score:6.0
  32. value:zset-3score:9.0
  33. value:zset-5score:14.0
  34. value:zset-4score:18.0
  • Long intersectAndStore(K key, K otherKey, K destKey);
    計算給定的一個或多個有序集的交集並將結果集存儲在新的有序集合 key 中
  1. 使用:System. out.println(template.opsForZSet().intersectAndStore("zzset1","zzset2","destZset33"));
  2.  
  3. Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores( "destZset33",0,-1);
  4. Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
  5. while (iterator.hasNext())
  6. {
  7. ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
  8. System. out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
  9. }
  10. 結果: value:zset-1score:2.0
  11. value:zset-2score:4.0
  12. value:zset-3score:6.0
  13. value:zset-4score:12.0
  • Long intersectAndStore(K key, Collection<K> otherKeys, K destKey);
    計算給定的一個或多個有序集的交集並將結果集存儲在新的有序集合 key 中
  1. 使用:List<String> stringList = new ArrayList<String>();
  2. stringList. add("zzset2");
  3. stringList. add("zzset3");
  4. System. out.println(template.opsForZSet().intersectAndStore("zzset1",stringList,"destZset44"));
  5.  
  6. Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores( "destZset44",0,-1);
  7. Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
  8. while (iterator.hasNext())
  9. {
  10. ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
  11. System. out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
  12. }
  13. 結果: value:zset-1score:3.0
  14. value:zset-2score:6.0
  15. value:zset-3score:9.0
  16. value:zset-4score:18.0
  • Cursor<TypedTuple<V>> scan(K key, ScanOptions options);
    遍歷zset
  1. 使用: Cursor<ZSetOperations.TypedTuple< Object>> cursor = template.opsForZSet().scan("zzset1", ScanOptions.NONE);
  2. while (cursor.hasNext()){
  3. ZSetOperations.TypedTuple< Object> item = cursor.next();
  4. System.out.println(item.getValue() + ":" + item.getScore());
  5. }
  6. 結果:zset -1:1.0
  7. zset -2:2.0
  8. zset -3:3.0
  9. zset -4:6.0

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

參考文檔:Redis實戰[約西亞 L.卡爾森 (Josiah L.Carlson)]
Spring官方文檔





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM