企業(yè)微信小程序如何開發(fā)青島seo推廣公司
文章目錄
- 1. String 命令
- 1.1 添加緩存
- 1.2 設(shè)置過期時間(單獨設(shè)置)
- 1.3 獲取緩存值
- 1.4 刪除key
- 1.5 順序遞增
- 1.6 順序遞減
- 1.7 常用的
- 2. Hash命令
- 2.1 添加緩存
- 2.2 設(shè)置過期時間(單獨設(shè)置)
- 2.3 添加一個Map集合
- 2.4 提取所有的小key
- 2.5 提取所有的value值
- 2.6 根據(jù)key提取value值
- 2.7 獲取所有的鍵值對集合
- 2.8 刪除
- 2.9 判斷Hash中是否含有該值
- 3. List命令
- 3.1 添加緩存
- 3.2 將List放入緩存
- 3.3 設(shè)置過期時間(單獨設(shè)置)
- 3.4 獲取List緩存全部內(nèi)容(起始索引,結(jié)束索引)
- 3.5 從左或從右彈出一個元素
- 3.6 根據(jù)索引查詢元素
- 3.7 獲取List緩存的長度
- 3.8 根據(jù)索引修改List中的某條數(shù)據(jù)(key,索引,值)
- 3.9 移除N個值為value(key,移除個數(shù),值)
- 4. Set命令
- 4.1 添加Set緩存(值可以是一個,也可是多個)
- 4.2 設(shè)置過期時間(單獨設(shè)置)
- 4.3 根據(jù)key獲取Set中的所有值
- 4.4 根據(jù)value從一個set中查詢,是否存在
- 4.5 獲取Set緩存的長度
- 4.6 移除指定的元素
- 4.7 移除指定的key
- 4.8 交集 并集 差集
- 5. ZSet命令
- 5.1 向集合中插入元素,并設(shè)置分數(shù)
- 5.2 向集合中插入多個元素,并設(shè)置分數(shù)
- 5.3 按照排名先后(從小到大)打印指定區(qū)間內(nèi)的元素, -1為打印全部
- 5.4 獲得指定元素的分數(shù)
- 5.5 返回集合內(nèi)的成員個數(shù)
- 5.6 返回集合內(nèi)指定分數(shù)范圍的成員個數(shù)(Double類型)
- 5.7 返回集合內(nèi)元素在指定分數(shù)范圍內(nèi)的排名(從小到大)
- 5.8 帶偏移量和個數(shù),(key,起始分數(shù),最大分數(shù),偏移量,個數(shù))
- 5.9 返回集合內(nèi)元素的排名,以及分數(shù)(從小到大)
- 5.10 返回指定成員的排名
- 5.11 從集合中刪除指定元素
- 5.12 刪除指定索引范圍的元素(Long類型)
- 5.13 刪除指定分數(shù)范圍內(nèi)的元素(Double類型)
- 5.14 為指定元素加分(Double類型)
RedisTemplate
是 Spring Framework
提供的用于操作 Redis
數(shù)據(jù)庫的模板類。通過 RedisTemplate
,開發(fā)人員可以方便地使用 Spring 提供的 API 來對 Redis 進行操作,比如設(shè)置值、獲取值、刪除值等。RedisTemplate 封裝了 Redis 連接、數(shù)據(jù)序列化、異常處理等操作,簡化了與 Redis 的交互過程。
一般情況下,開發(fā)人員需要配置 Redis 連接池、序列化器等信息,并將 RedisTemplate 注入到需要使用 Redis 的 Bean 中,然后就可以通過 RedisTemplate 來進行相應(yīng)的操作。
需要注意的是,RedisTemplate 是基于 Spring Data Redis 提供的操作 Redis 客戶端的功能而構(gòu)建的,可以通過 RedisTemplate 李進行操作 Redis 數(shù)據(jù)庫的常用數(shù)據(jù)結(jié)構(gòu),如字符串、列表、集合、有序集合等。
1. String 命令
1.1 添加緩存
//1、通過redisTemplate設(shè)置值
redisTemplate.boundValueOps("StringKey").set("StringValue");
redisTemplate.boundValueOps("StringKey").set("StringValue",1, TimeUnit.MINUTES);//2、通過BoundValueOperations設(shè)置值
BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
stringKey.set("StringVaule");
stringKey.set("StringValue",1, TimeUnit.MINUTES);//3、通過ValueOperations設(shè)置值
ValueOperations ops = redisTemplate.opsForValue();
ops.set("StringKey", "StringVaule");
ops.set("StringValue","StringVaule",1, TimeUnit.MINUTES);//4(SETNX + SETEX):這個key不存在執(zhí)行 存在則不執(zhí)行,多用于互斥鎖
ops.setIfAbsent("key", "value", 10, TimeUnit.SECONDS)
1.2 設(shè)置過期時間(單獨設(shè)置)
redisTemplate.boundValueOps("StringKey").expire(1,TimeUnit.MINUTES);
redisTemplate.expire("StringKey",1,TimeUnit.MINUTES);
不建議使用單獨設(shè)置過期時間的API, 可以使用 1.1 中的第一個演示,在設(shè)置值的同時設(shè)置過期時間.
1.3 獲取緩存值
//1、通過redisTemplate設(shè)置值
String str1 = (String) redisTemplate.boundValueOps("StringKey").get();//2、通過BoundValueOperations獲取值
BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
String str2 = (String) stringKey.get();//3、通過ValueOperations獲取值
ValueOperations ops = redisTemplate.opsForValue();
String str3 = (String) ops.get("StringKey");
1.4 刪除key
Boolean result = redisTemplate.delete("key");
1.5 順序遞增
redisTemplate.boundValueOps("key").increment(1L);
該API會返回遞增后的值. 如果KEY對應(yīng)的值不存在會創(chuàng)建之并返回1
1.6 順序遞減
redisTemplate.boundValueOps("StringKey").increment(-3L);
1.7 常用的
ValueOperations ops = redisTemplate.opsForValue();// 單獨設(shè)置有效期(不推薦單獨用)
ops.expire("StringKey",1,TimeUnit.MINUTES);// 設(shè)置值 and 有效期(推薦這種)
ops.set("key", "value", 1, TimeUnit.MINUTES);// 操作數(shù)值 增加 減少(INCR INCRBY)
ops.increment("key", 1);
ops.increment("key", -1);// (SETNX + SETEX):這個key不存在執(zhí)行 存在則不執(zhí)行,多用于互斥鎖
ops.setIfAbsent("key", "value", 10, TimeUnit.SECONDS)// 獲取緩存值
ops.get("StringKey");
2. Hash命令
2.1 添加緩存
//1、通過redisTemplate設(shè)置值
redisTemplate.boundHashOps("HashKey").put("SmallKey", "HashVaue");//2、通過BoundValueOperations設(shè)置值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
hashKey.put("SmallKey", "HashVaue");//3、通過ValueOperations設(shè)置值
HashOperations hashOps = redisTemplate.opsForHash();
hashOps.put("HashKey", "SmallKey", "HashVaue");
2.2 設(shè)置過期時間(單獨設(shè)置)
HashMap<String, String> hashMap = new HashMap<>();
redisTemplate.boundHashOps("HashKey").putAll(hashMap );
注意:只能給大 KEY 設(shè)置過期時間, 小 KEY 不能擁有獨立的過期時間.
2.3 添加一個Map集合
HashMap<String, String> hashMap = new HashMap<>();
redisTemplate.boundHashOps("HashKey").putAll(hashMap );
2.4 提取所有的小key
//1、通過redisTemplate獲取值
Set keys1 = redisTemplate.boundHashOps("HashKey").keys();//2、通過BoundValueOperations獲取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
Set keys2 = hashKey.keys();//3、通過ValueOperations獲取值
HashOperations hashOps = redisTemplate.opsForHash();
Set keys3 = hashOps.keys("HashKey");
2.5 提取所有的value值
//1、通過redisTemplate獲取值
List values1 = redisTemplate.boundHashOps("HashKey").values();//2、通過BoundValueOperations獲取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
List values2 = hashKey.values();//3、通過ValueOperations獲取值
HashOperations hashOps = redisTemplate.opsForHash();
List values3 = hashOps.values("HashKey");
2.6 根據(jù)key提取value值
//1、通過redisTemplate獲取
String value1 = (String) redisTemplate.boundHashOps("HashKey").get("SmallKey");//2、通過BoundValueOperations獲取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
String value2 = (String) hashKey.get("SmallKey");//3、通過ValueOperations獲取值
HashOperations hashOps = redisTemplate.opsForHash();
String value3 = (String) hashOps.get("HashKey", "SmallKey");
2.7 獲取所有的鍵值對集合
//1、通過redisTemplate獲取
Map entries = redisTemplate.boundHashOps("HashKey").entries();//2、通過BoundValueOperations獲取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
Map entries1 = hashKey.entries();//3、通過ValueOperations獲取值
HashOperations hashOps = redisTemplate.opsForHash();
Map entries2 = hashOps.entries("HashKey");
2.8 刪除
//刪除小key
redisTemplate.boundHashOps("HashKey").delete("SmallKey");
//刪除大key
redisTemplate.delete("HashKey");
2.9 判斷Hash中是否含有該值
Boolean isEmpty = redisTemplate.boundHashOps("HashKey").hasKey("SmallKey");
3. List命令
3.1 添加緩存
//1、通過redisTemplate設(shè)置值
redisTemplate.boundListOps("listKey").leftPush("listLeftValue1");
redisTemplate.boundListOps("listKey").rightPush("listRightValue2");//2、通過BoundValueOperations設(shè)置值
BoundListOperations listKey = redisTemplate.boundListOps("listKey");
listKey.leftPush("listLeftValue3");
listKey.rightPush("listRightValue4");//3、通過ValueOperations設(shè)置值
ListOperations opsList = redisTemplate.opsForList();
opsList.leftPush("listKey", "listLeftValue5");
opsList.rightPush("listKey", "listRightValue6");
3.2 將List放入緩存
ArrayList<String> list = new ArrayList<>();
// left
redisTemplate.boundListOps("listKey").leftPushAll(list);
// right
redisTemplate.boundListOps("listKey").rightPushAll(list);
3.3 設(shè)置過期時間(單獨設(shè)置)
redisTemplate.boundValueOps("listKey").expire(1,TimeUnit.MINUTES);
redisTemplate.expire("listKey",1,TimeUnit.MINUTES);
3.4 獲取List緩存全部內(nèi)容(起始索引,結(jié)束索引)
List listKey1 = redisTemplate.boundListOps("listKey").range(0, 10);
3.5 從左或從右彈出一個元素
//從左側(cè)彈出一個元素
String listKey2 = (String) redisTemplate.boundListOps("listKey").leftPop();
//從右側(cè)彈出一個元素
String listKey3 = (String) redisTemplate.boundListOps("listKey").rightPop();
3.6 根據(jù)索引查詢元素
String listKey4 = (String) redisTemplate.boundListOps("listKey").index(1);
3.7 獲取List緩存的長度
Long size = redisTemplate.boundListOps("listKey").size();
3.8 根據(jù)索引修改List中的某條數(shù)據(jù)(key,索引,值)
redisTemplate.boundListOps("listKey").set(3L,"listLeftValue3");
3.9 移除N個值為value(key,移除個數(shù),值)
redisTemplate.boundListOps("listKey").remove(3L,"value");
4. Set命令
4.1 添加Set緩存(值可以是一個,也可是多個)
//1、通過redisTemplate設(shè)置值
redisTemplate.boundSetOps("setKey").add("setValue1", "setValue2", "setValue3");//2、通過BoundValueOperations設(shè)置值
BoundSetOperations setKey = redisTemplate.boundSetOps("setKey");
setKey.add("setValue1", "setValue2", "setValue3");//3、通過ValueOperations設(shè)置值
SetOperations setOps = redisTemplate.opsForSet();
setOps.add("setKey", "SetValue1", "setValue2", "setValue3");
4.2 設(shè)置過期時間(單獨設(shè)置)
redisTemplate.boundValueOps("setKey").expire(1,TimeUnit.MINUTES);
redisTemplate.expire("setKey",1,TimeUnit.MINUTES);
4.3 根據(jù)key獲取Set中的所有值
//1、通過redisTemplate獲取值
Set set1 = redisTemplate.boundSetOps("setKey").members();//2、通過BoundValueOperations獲取值
BoundSetOperations setKey = redisTemplate.boundSetOps("setKey");
Set set2 = setKey.members();//3、通過ValueOperations獲取值
SetOperations setOps = redisTemplate.opsForSet();
Set set3 = setOps.members("setKey");
4.4 根據(jù)value從一個set中查詢,是否存在
Boolean isEmpty = redisTemplate.boundSetOps("setKey").isMember("setValue2");
4.5 獲取Set緩存的長度
Long size = redisTemplate.boundSetOps("setKey").size();
4.6 移除指定的元素
Long result1 = redisTemplate.boundSetOps("setKey").remove("setValue1");
4.7 移除指定的key
Boolean result2 = redisTemplate.delete("setKey");
4.8 交集 并集 差集
// 交集 返回存儲在 “key1” 和 “key2” 中的集合的交集。
Set<Object> intersect = redisTemplate.opsForSet().intersect("key1", "key2");
// 并集 返回存儲在 “key1” 和 “key2” 中的集合的并集。
Set<Object> union = redisTemplate.opsForSet().union("key1", "key2");
// 差集 返回存儲在 “key1” 中但不在 “key2” 中的集合,即差集。
Set<Object> difference = redisTemplate.opsForSet().difference("key1", "key2");
5. ZSet命令
5.1 向集合中插入元素,并設(shè)置分數(shù)
//1、通過redisTemplate設(shè)置值
redisTemplate.boundZSetOps("zSetKey").add("zSetVaule", 100D);//2、通過BoundValueOperations設(shè)置值
BoundZSetOperations zSetKey = redisTemplate.boundZSetOps("zSetKey");
zSetKey.add("zSetVaule", 100D);//3、通過ValueOperations設(shè)置值
ZSetOperations zSetOps = redisTemplate.opsForZSet();
zSetOps.add("zSetKey", "zSetVaule", 100D);
5.2 向集合中插入多個元素,并設(shè)置分數(shù)
DefaultTypedTuple<String> p1 = new DefaultTypedTuple<>("zSetVaule1", 2.1D);
DefaultTypedTuple<String> p2 = new DefaultTypedTuple<>("zSetVaule2", 3.3D);
redisTemplate.boundZSetOps("zSetKey").add(new HashSet<>(Arrays.asList(p1,p2)));
5.3 按照排名先后(從小到大)打印指定區(qū)間內(nèi)的元素, -1為打印全部
Set<String> range = redisTemplate.boundZSetOps("zSetKey").range(0, -1);
5.4 獲得指定元素的分數(shù)
Double score = redisTemplate.boundZSetOps("zSetKey").score("zSetVaule");
5.5 返回集合內(nèi)的成員個數(shù)
Long size = redisTemplate.boundZSetOps("zSetKey").size();
5.6 返回集合內(nèi)指定分數(shù)范圍的成員個數(shù)(Double類型)
Long COUNT = redisTemplate.boundZSetOps("zSetKey").count(0D, 2.2D);
5.7 返回集合內(nèi)元素在指定分數(shù)范圍內(nèi)的排名(從小到大)
Set byScore = redisTemplate.boundZSetOps("zSetKey").rangeByScore(0D, 2.2D);
5.8 帶偏移量和個數(shù),(key,起始分數(shù),最大分數(shù),偏移量,個數(shù))
Set<String> ranking2 = redisTemplate.opsForZSet().rangeByScore("zSetKey", 0D, 2.2D 1, 3);
5.9 返回集合內(nèi)元素的排名,以及分數(shù)(從小到大)
Set<TypedTuple<String>> tuples = redisTemplate.boundZSetOps("zSetKey").rangeWithScores(0L, 3L);for (TypedTuple<String> tuple : tuples) {System.out.println(tuple.getValue() + " : " + tuple.getScore());}
5.10 返回指定成員的排名
//從小到大
Long startRank = redisTemplate.boundZSetOps("zSetKey").rank("zSetVaule");
//從大到小
Long endRank = redisTemplate.boundZSetOps("zSetKey").reverseRank("zSetVaule");
5.11 從集合中刪除指定元素
redisTemplate.boundZSetOps("zSetKey").remove("zSetVaule");
5.12 刪除指定索引范圍的元素(Long類型)
redisTemplate.boundZSetOps("zSetKey").removeRange(0L,3L);
5.13 刪除指定分數(shù)范圍內(nèi)的元素(Double類型)
redisTemplate.boundZSetOps("zSetKey").removeRangeByScorssse(0D,2.2D);
5.14 為指定元素加分(Double類型)
Double score = redisTemplate.boundZSetOps("zSetKey").incrementScore("zSetVaule",1.1D);