RedisTemplate用法詳解


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

Redis的String數據結構 

set void set(K key, V value);

  1.  redisTemplate.opsForValue().set("num","123");  存:set(key,value);
  2.  redisTemplate.opsForValue().get("num") 輸出結果為123 取:get(key)

set void set(K key, V value, long timeout, TimeUnit unit); 

  1.  redisTemplate.opsForValue().set("num","123",10, TimeUnit.SECONDS); // 設置過期時間 set(key,value,2*60,10, TimeUnit.SECONDS) ;//秒
  2.  redisTemplate.opsForValue().get("num")設置的是10秒失效,十秒之內查詢有結果,十秒之后返回為null

set void set(K key, V value, long offset);

覆寫(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

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

append Integer append(K key, String value);

如果key已經存在並且是一個字符串,則該命令將該值追加到字符串的末尾。如果鍵不存在,則它被創建並設置為空字符串,因此APPEND在這種特殊情況下將類似於SET。

  1.  template.opsForValue().append("test","Hello");
  2.  System.out.println(template.opsForValue().get("test"));
  3.  template.opsForValue().append("test","world");
  4.  System.out.println(template.opsForValue().get("test"));
  5.  Hello
  6.  Helloworld

size Long size(K key);
返回key所對應的value值得長度

  1.  template.opsForValue().set("key","hello world");
  2.  System.out.println("***************"+template.opsForValue().size("key"));
  3.  ***************11

Redis的List數據結構

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[] strs = new String[]{"1","2","3"};
  2.  template.opsForList().leftPushAll("list",strs);
  3.  System.out.println(template.opsForList().range("list",0,-1));
  4.  [3, 2, 1]

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.  1
  5.  2
  6.  3

Long rightPushAll(K key, V... values);

  1.  String[] strs = new String[]{"1","2","3"};
  2.  template.opsForList().rightPushAll("list",strs);
  3.  System.out.println(template.opsForList().range("list",0,-1));
  4.  [1, 2, 3]

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.  [c++, python, oc, java, c#, c#]
  5.  c++
  6.  [python, oc, java, c#, c#]

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#]

Redis的Hash數據機構

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","666"));
  2.  System.out.println(template.opsForHash().hasKey("redisHash","777"));
  3.  true
  4.  false

HV get(H key, Object hashKey);
從鍵中的哈希獲取給定hashKey的值

  1.  System.out.println(template.opsForHash().get("redisHash","age"));
  2.  26

 Set<HK> keys(H key);
獲取key所對應的散列表的key

  1.  System.out.println(template.opsForHash().keys("redisHash"));
  2.  //redisHash所對應的散列表為{class=1, name=666, age=27}
  3.  [name, class, age]

Long size(H key);
獲取key所對應的散列表的大小個數

  1.  System.out.println(template.opsForHash().size("redisHash"));
  2.  //redisHash所對應的散列表為{class=1, name=666, 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","666");
  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","666");
  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=666}

 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",
  2.  ScanOptions.ScanOptions.NONE);
  3.  while(curosr.hasNext()){
  4.  Map.Entry<Object, Object> entry = curosr.next();
  5.  System.out.println(entry.getKey()+":"+entry.getValue());
  6.  }
  7.  age:27
  8.  class:6
  9.  name:666

Redis的Set數據結構

Long add(K key, V... values);
無序集合中添加元素,返回添加個數
也可以直接在add里面添加多個值 如:template.opsForSet().add("setTest","aaa","bbb")

  1.  String[] strs= new String[]{"str1","str2"};
  2.  System.out.println(template.opsForSet().add("setTest", strs));
  3.  2

 Long remove(K key, Object... values);
移除集合中一個或多個成員

  1.  String[] strs = new String[]{"str1","str2"};
  2.  System.out.println(template.opsForSet().remove("setTest",strs));
  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

Set<V> members(K key);
返回集合中的所有成員

  1.  
    System.out.println(template.opsForSet().members("setTest"));
  2.  
    [ddd, bbb, aaa, ccc]

 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數據結構 

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<>("zset-5",9.6);
  2.  ZSetOperations.TypedTuple<Object> objectTypedTuple2 = new DefaultTypedTuple<>("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]

 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 //表明排名第一

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]

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

 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]

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

-------------------------------------

  • //向redis里存入數據和設置緩存時間
  •  stringRedisTemplate.opsForValue(). set("baike", "100", 60 * 10, TimeUnit.SECONDS);
  •   //val做-1操作
  •  stringRedisTemplate.boundValueOps( "baike").increment(-1);
  •   //根據key獲取緩存中的val
  •  stringRedisTemplate.opsForValue(). get("baike")
  •   //val +1
  •  stringRedisTemplate.boundValueOps( "baike").increment(1);
  •   //根據key獲取過期時間
  •  stringRedisTemplate.getExpire( "baike");
  •   //根據key獲取過期時間並換算成指定單位
  •  stringRedisTemplate.getExpire( "baike",TimeUnit.SECONDS);
  •   //根據key刪除緩存
  •  stringRedisTemplate.delete( "baike");
  •   //檢查key是否存在,返回boolean值
  •  stringRedisTemplate.hasKey( "baike");
  •   //向指定key中存放set集合
  •  stringRedisTemplate.opsForSet().add( "baike", "1","2","3");
  •   //設置過期時間
  •  stringRedisTemplate.expire( "baike",1000 , TimeUnit.MILLISECONDS);
  •   //根據key查看集合中是否存在指定數據
  •  stringRedisTemplate.opsForSet().isMember( "baike", "1");
  •   //根據key獲取set集合
  •  stringRedisTemplate.opsForSet().members( "baike");
  •   //驗證有效時間
  •   Long expire = redisTemplate.boundHashOps("baike").getExpire();
  •  


免責聲明!

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



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