Redis對(duì)List類型操作
1. rpush
lpush谐宙、rpush:添加值湃望,格式是:lpush list的key item項(xiàng)的值(值可以有多個(gè))
127.0.0.1:6379>rpush mylist v1 v2 v3
(integer)3
127.0.0.1:6379>lrange mylist 0-1
1)"v1"
2)"v2"
3)"v3"
127.0.0.1:6379>
Java代碼示例如下:
@Test
public void list(){
stringRedisTemplate.delete(KEY);
/**
* 示例1: 相當(dāng)于執(zhí)行 rpush mylist v1 v2 v3
*/
final Long rlong=stringRedisTemplate.opsForList().rightPushAll(KEY,"v1","v2","v3");
System.out.println(rlong);
}
2. lpush
lpush、rpush:添加值,格式是:lpush list的key item項(xiàng)的值(值可以有多個(gè))
127.0.0.1:6379>lpush mylist v11 v12 v13
(integer)6
127.0.0.1:6379>lrange mylist 0-1
1)"v13"
2)"v12"
3)"v11"
4)"v1"
5)"v2"
6)"v3"
127.0.0.1:6379>
Java代碼示例如下:
@Test
public void list(){
stringRedisTemplate.delete(KEY);
/**
* 示例2: 相當(dāng)于執(zhí)行 lpush mylist v11 v12 v13
*/
final Long llong=stringRedisTemplate.opsForList().leftPushAll(KEY,"v11","v12","v13");
System.out.println(llong);
}
3.range
lrange:按索引范圍獲取值据德,格式是:lrange list的key 起始索引 終止索引 (-1表示最后一個(gè)索引)
127.0.0.1:6379> lrange mylist 0 -1
1) "v13"
2) "v12"
3) "v11"
4) "v1"
5) "v2"
6) "v3"
127.0.0.1:6379> lindex mylist 0
"v13"
127.0.0.1:6379>
Java代碼示例如下:
@Test
public void range(){
/**
* 示例1: 相當(dāng)于執(zhí)行 lrange mylist 0 -1
*/
stringRedisTemplate.opsForList().range(KEY,0,-1).forEach(System.out::println);
System.out.println("---------");
/**
* 示例2: 相當(dāng)于執(zhí)行 lindex mylist 0
*/
stringRedisTemplate.opsForList().range(KEY,0,0).forEach(System.out::println);
}
4.lpop
lpop、rpop:彈出值府蔗,格式是:lpop list的key
127.0.0.1:6379>lrange mylist 0-1
1)"v13"
2)"v12"
3)"v11"
4)"v1"
5)"v2"
6)"v3"
127.0.0.1:6379>lindex mylist 0
"v13"
127.0.0.1:6379>lpop mylist
"v13"
127.0.0.1:6379>lrange mylist 0-1
1)"v12"
2)"v11"
3)"v1"
4)"v2"
5)"v3"
127.0.0.1:6379>
Java代碼示例如下:
@Test
public void pop(){
/**
* 示例1: 相當(dāng)于執(zhí)行 lpop mylist
*/
final String leftPop=stringRedisTemplate.opsForList().leftPop(KEY);
System.out.println(leftPop);
}
5.rpop
blpop晋控、brpop:彈出值,格式是:blpop list的key值 過(guò)期時(shí)間姓赤。(key可以是多個(gè)赡译,如果沒(méi)有值,會(huì)一直等到有值不铆,直到過(guò)期)
127.0.0.1:6379>lrange mylist 0-1
1)"v12"
2)"v11"
3)"v1"
4)"v2"
5)"v3"
127.0.0.1:6379>rpop mylist
"v3"
127.0.0.1:6379>lrange mylist 0-1
1)"v12"
2)"v11"
3)"v1"
4)"v2"
127.0.0.1:6379>
Java代碼示例如下:
@Test
public void pop(){
/**
* 示例2: 相當(dāng)于執(zhí)行 rpop mylist
*/
final String rightPop=stringRedisTemplate.opsForList().rightPop(KEY);
System.out.println(rightPop);
}
6.lle
llen:獲取list中元素的個(gè)數(shù)蝌焚,格式是:llen list的key
127.0.0.1:6379>llen mylist
(integer)4
127.0.0.1:6379>
Java代碼示例如下:
@Test
public void line(){
/**
* 獲取列表長(zhǎng)度
* 示例1: 相當(dāng)于執(zhí)行 llen mylist
*/
final Long size=stringRedisTemplate.opsForList().size(KEY);
System.out.println(size);
}
7. remove
lrem:刪除元素,格式是:lrem list的key 數(shù)量 item項(xiàng)的值誓斥,(數(shù)量可正負(fù)只洒,表示從左或從右刪除;如果數(shù)量為0,表示刪除全部與給定值相符的項(xiàng))
127.0.0.1:6379>lrange mylist 0-1
1)"v12"
2)"v11"
3)"v1"
4)"v2"
127.0.0.1:6379>lrem mylist-1v2
(integer)1
127.0.0.1:6379>lrange mylist 0-1
1)"v12"
2)"v11"
3)"v1"
127.0.0.1:6379>
Java代碼示例如下:
@Test
public void remove(){
/**
* 移除列表元素
*/
*示例1: 相當(dāng)于執(zhí)行 lrem mylist-1v2
*/
final Long index=stringRedisTemplate.opsForList().remove(KEY,-1,"v2");
System.out.println(index);
}
8.trim
ltrim:保留指定索引區(qū)間的元素劳坑,格式是:ltrim list的key 起始索引 結(jié)束索引
127.0.0.1:6379>ltrim mylist 0 1
OK
127.0.0.1:6379>lrange mylist 0-1
1)"v12"
2)"v11"
127.0.0.1:6379>
Java代碼示例如下:
@Test
public void trim(){
/**
* 示例1: 相當(dāng)于執(zhí)行 ltrim mylist 0 1
* 裁剪,void,刪除除了[start,end]以外的所有元素
*/
stringRedisTemplate.opsForList().trim(KEY,0,1);
}
9.set
lset:設(shè)置指定索引的值毕谴,格式是:lset list的key 索引 新的值
127.0.0.1:6379>lrange mylist 0-1
1)"v12"
2)"v11"
127.0.0.1:6379>lset mylist 1vip
OK
127.0.0.1:6379>lrange mylist 0-1
1)"v12"
2)"vip"
127.0.0.1:6379>
Java代碼示例如下:
@Test
public void set(){
/**
* 示例1: 相當(dāng)于執(zhí)行 lset mylist 1 vip
*/
stringRedisTemplate.opsForList().set(KEY,1,"vip");
}
Redis對(duì)Hash類型操作
1. hset
hset:設(shè)置值。格式是:hset hash的key 項(xiàng)的key 項(xiàng)的值
@Test
public void put(){
/**
* 示例1: 相當(dāng)于執(zhí)行 hset myhash name tom
*/
stringRedisTemplate.opsForHash().put(KEY,"name","tom");
/**
* 127.0.0.1:6379> hget myhash name
* "tom"
* 127.0.0.1:6379>
*/
}
2. hset
同時(shí)設(shè)置多對(duì)值距芬。格式是:hmset hash的key 項(xiàng)的key 項(xiàng)的值涝开。(項(xiàng)的key和項(xiàng)的值可以多對(duì))
@Test
public void putAll(){
/**
* 示例1: hset myhash id 1 name jack age 12
*/
Map<String, Object> map=new HashMap<>();
map.put("id","1");
map.put("name","jack");
map.put("age","12");
stringRedisTemplate.opsForHash().putAll(KEY,map);
/**
* 127.0.0.1:6379> hset myhash id 1 name jack age 12
* (integer) 0
* 127.0.0.1:6379> hget myhash id
* "1"
* 127.0.0.1:6379>
*
*/
}
3. hget
hget:獲取值。格式是:hget hash的key 項(xiàng)的key
@Test
public void hget(){
/**
* 示例1: hget myhash id
*/
String id=(String)stringRedisTemplate.opsForHash().get(KEY,"id");
String name=(String)stringRedisTemplate.opsForHash().get(KEY,"name");
String age=(String)stringRedisTemplate.opsForHash().get(KEY,"age");
System.out.println(String.format("id = %s, name = %s, age=%s",id,name,age));
}
4. hgetall
hgetall:獲取該key下所有的值框仔。格式是:hgetall hash的key
@Test
public void hmget(){
/**
* 示例1: hgetall myhash
*/
final Map<Object, Object> entries=stringRedisTemplate.opsForHash().entries(KEY);
entries.forEach((key,value)->System.out.println("key: "+key+" value: "+value));
for(Object value:entries.values()){
System.out.println("value: "+value);
}
entries.values().forEach(System.out::println);
for(Object key:entries.keySet()){
System.out.println("key: "+key+" value: "+entries.get(key));
}
for(Map.Entry<Object, Object> entry:entries.entrySet()){
System.out.println("key: "+entry.getKey()+" value: "+entry.getValue());
}
/**
* 127.0.0.1:6379> hgetall myhash
* 1) "name"
* 2) "jack"
* 3) "id"
* 4) "1"
* 5) "age"
* 6) "12"
* 127.0.0.1:6379>
*/
}
5. delete
hdel:刪除某個(gè)項(xiàng)舀武。格式是:hdel hash的key 項(xiàng)的key
@Test
public void delete(){
/**
* 示例1: hdel myhash id
*/
stringRedisTemplate.opsForHash().delete(KEY,"id");
/**
*127.0.0.1:6379> hdel myhash id
* (integer) 1
* 127.0.0.1:6379> hgetall myhash
* 1) "name"
* 2) "jack"
* 3) "age"
* 4) "12"
* 127.0.0.1:6379>
*/
}
6 hkeys
hkeys:獲取所有的item的key,格式是:hkeys hash的key
@Test
public void hkeys(){
/**
* 示例1: hdel myhash id
*/
final Set<Object> keys=stringRedisTemplate.opsForHash().keys(KEY);
keys.forEach(System.out::println);
/**
* 127.0.0.1:6379> hkeys myhash
* 1) "name"
* 2) "age"
* 127.0.0.1:6379>
*/
}
Redis對(duì)Set類型操作
1. sadd
sadd:添加元素,格式是:sadd set的key item的項(xiàng)值离斩,item項(xiàng)可以有多個(gè)银舱。
@Test
public void sadd(){
/**
* 示例1:相當(dāng)于執(zhí)行 sadd myset v1 v2 v3
*/
stringRedisTemplate.opsForSet().add(KEY,"v1","v2","v3");
/**
* 127.0.0.1:6379> smembers myset
* 1) "v3"
* 2) "v2"
* 3) "v1"
* 127.0.0.1:6379>
*/
}
2.smembers
smembers:獲取集合中所有元素瘪匿,格式是:smembers set的key
@Test
public void members(){
/**
* 示例1:相當(dāng)于執(zhí)行 smembers myset
*/
final Set<String> members=stringRedisTemplate.opsForSet().members(KEY);
members.forEach(System.out::println);
/**
* 127.0.0.1:6379> smembers myset
* 1) "v3"
* 2) "v2"
* 3) "v1"
* 127.0.0.1:6379>
*/
}
3.sismember
sismember:判斷元素是否在集合中,格式是:sismember set的key item的項(xiàng)值
@Test
public void sismember(){
/**
* 示例1:相當(dāng)于執(zhí)行 sismember myset v1
*/
final Boolean v1=stringRedisTemplate.opsForSet().isMember(KEY,"v1");
System.out.println(v1);
/**
* 127.0.0.1:6379> sismember myset v1
* (integer) 1
* 127.0.0.1:6379>
*/
}
4.srem
srem:刪除元素寻馏,格式是:srem set的key item項(xiàng)的值
@Test
public void srem(){
/**
* 示例1:相當(dāng)于執(zhí)行 sismember myset v1
*/
final Long v1=stringRedisTemplate.opsForSet().remove(KEY,"v1");
System.out.println(v1);
/**
* 127.0.0.1:6379> srem myset v1
* (integer) 1
* 127.0.0.1:6379> smembers myset
* 1) "v3"
* 2) "v2"
* 127.0.0.1:6379>
*/
}
5. scard
scard:獲取集合中元素的個(gè)數(shù)棋弥,格式是:scard set的key
@Test
public void scard(){
/**
* 示例1:相當(dāng)于執(zhí)行 sismember myset v1
*/
final Long size=stringRedisTemplate.opsForSet().size(KEY);
System.out.println(size);
/**
* 127.0.0.1:6379> scard myset
* (integer) 2
* 127.0.0.1:6379>
*/
}
6.srandmember
srandmember:隨機(jī)獲取集合中的元素,格式是:srandmember set的key[數(shù)量]操软。(數(shù)量為正數(shù)時(shí)嘁锯,會(huì)隨機(jī)獲取這么多個(gè)不重復(fù)的元素;如果數(shù)量大于集合元素個(gè)數(shù)聂薪,返回全部家乘;如果數(shù)量為負(fù),會(huì)隨機(jī)獲取這么多個(gè)元素藏澳,可能有重復(fù)仁锯。)
@Test
public void srandmember(){
/**
* 示例1:相當(dāng)于執(zhí)行 srandmember myset 1
*/
final String value=stringRedisTemplate.opsForSet().randomMember(KEY);
System.out.println(value);
/**
127.0.0.1:6379> sadd myset v1 v2 v3 v4 v4 v5 v6 v7 v8 v7
(integer) 6
127.0.0.1:6379> smembers myset
1) "v8"
2) "v2"
3) "v5"
4) "v7"
5) "v4"
6) "v3"
7) "v1"
8) "v6"
127.0.0.1:6379> srandmember myset 1
1) "v3"
127.0.0.1:6379>
*/
final List<String> values=stringRedisTemplate.opsForSet().randomMembers(KEY,2);
System.out.println(values);
/**
* 127.0.0.1:6379> srandmember myset 2
* 1) "v8"
* 2) "v2"
* 127.0.0.1:6379>
*/
}
7. spop
spop:彈出元素,格式是:spop set的key
@Test
public void spop(){
/**
* 示例1:相當(dāng)于執(zhí)行 sismember myset v1
*/
final String value=stringRedisTemplate.opsForSet().pop(KEY);
// final List<String> values = stringRedisTemplate.opsForSet().pop(KEY, 2);
System.out.println(value);
/**
* 127.0.0.1:6379> spop myset
* 127.0.0.1:6379> smembers myset
* 1) "v8"
* 2) "v2"
* 3) "v3"
* 4) "v1"
* 5) "v6"
* 6) "v5"
* 7) "v4"
* 8) "v7"
* 127.0.0.1:6379> smembers myset
* 1) "v8"
* 2) "v2"
* 3) "v3"
* 4) "v1"
* 5) "v6"
* 6) "v4"
* 7) "v7"
* 127.0.0.1:6379>
*/
}
8. sdiff
sdiff:差集翔悠,返回在第一個(gè)set里面而不在后面任何一個(gè)set里面的項(xiàng)业崖。格式是:sdiff set的key 用來(lái)比較的多個(gè)set的key
@Test
public void sdiff(){
/**
* 示例1:相當(dāng)于執(zhí)行 sismember myset v1
*/
final Set<String> difference=stringRedisTemplate.opsForSet().difference(KEY,"myset1");
difference.forEach(System.out::println);
/**
* 127.0.0.1:6379> sadd myset1 1 2 3 v3 v6 v6 v8
* (integer) 6
* 127.0.0.1:6379> smembers myset1
* 1) "v8"
* 2) "2"
* 3) "3"
* 4) "v3"
* 5) "1"
* 6) "v6"
* 127.0.0.1:6379> sdiff myset myst1
* 1) "v8"
* 2) "v2"
* 3) "v7"
* 4) "v4"
* 5) "v3"
* 6) "v1"
* 7) "v6"
* 127.0.0.1:6379>
*/
}
9.sinter
sinter:交集,返回多個(gè)set里面都有的項(xiàng)蓄愁。格式是:sinter 多個(gè)set的key
@Test
public void sinter(){
/**
* 示例1:相當(dāng)于執(zhí)行 sinter myset myset1
*/
final Set<String> myset1=stringRedisTemplate.opsForSet().intersect(KEY,"myset1");
myset1.forEach(System.out::println);
/**
* 127.0.0.1:6379> sinter myset myset1
* 1) "v8"
* 2) "v3"
* 3) "v6"
* 127.0.0.1:6379>
*/
}
10. sinterstore
sinterstore:交集并保留結(jié)果双炕。格式是:sinterstore 存放結(jié)果的key 多個(gè)set的key
@Test
public void sinterstore(){
/**
* 示例1:相當(dāng)于執(zhí)行 sinterstore myset2 myset myset1
*/
stringRedisTemplate.opsForSet().differenceAndStore(KEY,"myset1","myset3");
/**
* 127.0.0.1:6379> sinterstore myset2 myset myset1
* (integer) 3
* 127.0.0.1:6379> smembers myset2
* 1) "v3"
* 2) "v8"
* 3) "v6"
* 127.0.0.1:6379>
*/
}
11.sunion
sunion:并集。格式是:sunion 多個(gè)set的key
@Test
public void sunion(){
/**
* 示例1:相當(dāng)于執(zhí)行 sunion myset myset1
*/
final Set<String> union=stringRedisTemplate.opsForSet().union(KEY,"myset1");
union.forEach(System.out::println);
/**
* 127.0.0.1:6379> sunion myset myset1
* 1) "v3"
* 2) "v1"
* 3) "v6"
* 4) "v8"
* 5) "v2"
* 6) "2"
* 7) "3"
* 8) "v4"
* 9) "v7"
* 10) "1"
* 127.0.0.1:6379>
*/
}
12.sunionstore
sunionstore:并集并保留結(jié)果撮抓。格式是:sunionstore 存放結(jié)果的set的key 多個(gè)set的key
@Test
public void sunionstore(){
/**
* 示例1:相當(dāng)于執(zhí)行 sunionstore myset4 myset myset1
*/
final Long sunionstore=stringRedisTemplate.opsForSet().unionAndStore(KEY,"myset1","myset4");
System.out.println(sunionstore);
/**
* 127.0.0.1:6379> smembers myset4
* 1) "v3"
* 2) "v1"
* 3) "v6"
* 4) "v8"
* 5) "v2"
* 6) "2"
* 7) "3"
* 8) "v4"
* 9) "v7"
* 10) "1"
* 127.0.0.1:6379>
*/
}
Redis對(duì)ZSet類型操作
1. zadd
zadd :添加元素妇斤,格式是:zadd zset的key score值 項(xiàng)的值, Score和項(xiàng)可以是多對(duì)丹拯,score可以是整數(shù)站超,也可以是浮點(diǎn)數(shù),還可以是+inf表示無(wú)窮大乖酬,-inf表示負(fù)無(wú)窮大
@Test
public void zadd() {
/**
* 示例1:相當(dāng)于執(zhí)行 127.0.0.1:6379> zadd myzset 10 v1
*/
final Boolean add = stringRedisTemplate.opsForZSet().add(KEY, "v4", 40);
System.out.println(add);
Set<ZSetOperations.TypedTuple<String>> values = new HashSet<>();
values.add(new DefaultTypedTuple("v5", 50.0));
values.add(new DefaultTypedTuple("v6", 60.0));
final Long add1 = stringRedisTemplate.opsForZSet().add(KEY, values);
System.out.println(add1);
/**
* 127.0.0.1:6379> zadd myzset 10 v1
* (integer) 1
* 127.0.0.1:6379> zadd myzset 10 v1 20 v2 30 v3
* (integer) 2
* 127.0.0.1:6379> zrange myzset 0 -1
* 1) "v1"
* 2) "v2"
* 3) "v3"
* 4) "v4"
* 5) "v5"
* 6) "v6"
* 127.0.0.1:6379> zrange myzset 0 -1 withscores
* 1) "v1"
* 2) "10"
* 3) "v2"
* 4) "20"
* 5) "v3"
* 6) "30"
* 7) "v4"
* 8) "40"
* 9) "v5"
* 10) "50"
* 11) "v6"
* 12) "60"
*/
}
2. zrange
zrange :獲取索引區(qū)間內(nèi)的元素死相,格式是:zrange zset的key 起始索引 終止索引 (withscore)
@Test
public void zrange() {
/**
* 示例1:相當(dāng)于執(zhí)行 zrange myzset 0 -1 withscores
* 示例2:相當(dāng)于執(zhí)行 zzrange myzset 0 -1
*/
final Set<String> range = stringRedisTemplate.opsForZSet().range(KEY, 0, -1);
range.forEach(System.out::println);
final Set<ZSetOperations.TypedTuple<String>> v2 = stringRedisTemplate.opsForZSet().differenceWithScores(KEY, "otherKey");
System.out.println(JSONObject.toJSON(v2));
/**
* v1
* v2
* v3
* v4
* v5
* v6
* [{"score":10.0,"value":"v1"},{"score":20.0,"value":"v2"},{"score":30.0,"value":"v3"},{"score":40.0,"value":"v4"},{"score":50.0,"value":"v5"},{"score":60.0,"value":"v6"}]
* 127.0.0.1:6379> zrange myzset 0 -1
* 1) "v1"
* 2) "v2"
* 3) "v3"
* 4) "v4"
* 5) "v5"
* 6) "v6"
* 127.0.0.1:6379> zrange myzset 0 -1 withscores
* 1) "v1"
* 2) "10"
* 3) "v2"
* 4) "20"
* 5) "v3"
* 6) "30"
* 7) "v4"
* 8) "40"
* 9) "v5"
* 10) "50"
* 11) "v6"
* 12) "60"
*/
}
3. zrangebyscore
// zrangebyscore :獲取分?jǐn)?shù)區(qū)間內(nèi)的元素,格式是:zrangebyscore zset的key 起始score 終止score (withscore),默認(rèn)是包含端點(diǎn)值的咬像,如果加上"("表示不包含算撮,后面還可以加上limit來(lái)限制。
@Test
public void score() {
/**
* 示例1:相當(dāng)于執(zhí)行 zrangebyscore myzset 10 30 默認(rèn)是包含端點(diǎn)值的县昂,如果加上"("表示不包含肮柜,后面還可以加上limit來(lái)限制。
*/
final Set<String> range = stringRedisTemplate.opsForZSet().rangeByScore(KEY, 10, 30);
range.forEach(System.out::println);
/**
* 127.0.0.1:6379> zrangebyscore myzset 10 30
* 1) "v1"
* 2) "v2"
* 3) "v3"
* 127.0.0.1:6379>
*/
}
4. zrem
//zrem 刪除元素七芭,格式是:zrem zset的key 項(xiàng)的值素挽,項(xiàng)的值可以是多個(gè)
@Test
public void zrem() {
/**
* 示例1:相當(dāng)于執(zhí)行 zrem myzset v1
*/
final Long zremove = stringRedisTemplate.opsForZSet().remove(KEY, "v1");
System.out.println(zremove);
/**
* 127.0.0.1:6379> zrange myzset 0 -1 withscores
* 1) "v1"
* 2) "10"
* 3) "v2"
* 4) "20"
* 5) "v3"
* 6) "30"
* 7) "v4"
* 8) "40"
* 9) "v5"
* 10) "50"
* 11) "v6"
* 12) "60"
* 127.0.0.1:6379> zrem myzset v1
* (integer) 1
* 127.0.0.1:6379> zrange myzset 0 -1 withscores
* 1) "v2"
* 2) "20"
* 3) "v3"
* 4) "30"
* 5) "v4"
* 6) "40"
* 7) "v5"
* 8) "50"
* 9) "v6"
* 10) "60"
* 127.0.0.1:6379>
*/
}
5. zcard
獲取集合中元素個(gè)數(shù)蔑赘,格式是:zcard zset的key
@Test
public void zcard() {
/**
* 示例1:相當(dāng)于執(zhí)行 zcard myzset
*/
final Long zcard = stringRedisTemplate.opsForZSet().size(KEY);
System.out.println(zcard);
/**
* 127.0.0.1:6379> zcard myzset
* (integer) 5
* 127.0.0.1:6379>
*/
}
6.zincrby
zincrby :增減元素的score狸驳,格式是:zincrby zset的key 正負(fù)數(shù)字 項(xiàng)的值
@Test
public void zincrby() {
/**
* 示例1:相當(dāng)于執(zhí)行 zincrby myzset 21 v1
*/
final Double aDouble = stringRedisTemplate.opsForZSet().incrementScore(KEY, "v1", 21);
System.out.println(aDouble);
/**
* 127.0.0.1:6379> zincrby myzset 0 v0
* "0"
* 127.0.0.1:6379> zrange myzset 0 -1 withscores
* 1) "v0"
* 2) "0"
* 3) "v2"
* 4) "20"
* 5) "v1"
* 6) "21"
* 7) "v3"
* 8) "30"
* 9) "v4"
* 10) "40"
* 11) "v5"
* 12) "50"
* 13) "v6"
* 14) "60"
* 127.0.0.1:6379>
*/
}
7.zrank
zrank : 獲取項(xiàng)在zset中的索引预明,格式是:zrank zset的key 項(xiàng)的值
@Test
public void zrank() {
/**
* 示例1:相當(dāng)于執(zhí)行 zrank myzset v2
*/
final Long rank = stringRedisTemplate.opsForZSet().rank(KEY,"v2");
System.out.println(rank);
/**
* 127.0.0.1:6379> zrank myzset v2
* (integer) 1
*/
}
8.zscore
zscore :獲取元素的分?jǐn)?shù),格式是:zscore zset的key 項(xiàng)的值耙箍,返回項(xiàng)在zset中的score
@Test
public void zscore() {
/**
* 示例1:相當(dāng)于執(zhí)行 zscore myzset v2
*/
final Double zscore = stringRedisTemplate.opsForZSet().score(KEY,"v2");
System.out.println(zscore);
/**
* 127.0.0.1:6379> zscore myzset v2
* "20"
* 127.0.0.1:6379>
*/
}
String類:
1.redisTemplate.opsForValue().set(key,value)); //設(shè)置指定鍵的值
2.redisTemplate.opsForValue().get(key)); //獲取指定鍵的值撰糠。
3.redisTemplate.opsForValue().get(key, start, end);//獲取存儲(chǔ)在鍵上的字符串的子字符串。
4.redisTemplate.opsForValue().getAndSet(key, value);//設(shè)置鍵的字符串值并返回其舊值辩昆。
5.redisTemplate.opsForValue().getBit(key, offset);//返回在鍵處存儲(chǔ)的字符串值中偏移處的位值阅酪。
6.redisTemplate.opsForValue().multiGet(keys);//獲取所有給定鍵的值
7.redisTemplate.opsForValue().setBit(key, offset, value);//存儲(chǔ)在鍵上的字符串值中設(shè)置或清除偏移處的位
8.redisTemplate.opsForValue().set(K key, V value, long timeout, TimeUnit unit);//使用鍵和到期時(shí)間來(lái)設(shè)置值
9.redisTemplate.opsForValue().setIfAbsent(key, value);//設(shè)置鍵的值,僅當(dāng)鍵不存在時(shí)
10.redisTemplate.opsForValue().set(K key, V value, long offset);//在指定偏移處開(kāi)始的鍵處覆蓋字符串的一部分
11.redisTemplate.opsForValue().size(key));//獲取存儲(chǔ)在鍵中的值的長(zhǎng)度
12.redisTemplate.opsForValue().multiGet(Collection<K> keys);//為多個(gè)鍵分別設(shè)置它們的值
13.redisTemplate.opsForValue().multiSetIfAbsent(Map<? extends K, ? extends V> m);// 為多個(gè)鍵分別設(shè)置它們的值汁针,僅當(dāng)鍵不存在時(shí)
Hash類:
1.redisTemplate.opsForHash().delete(H key, Object... hashKeys);//刪除一個(gè)或多個(gè)哈希字段术辐。
2.redisTemplate.opsForHash().hasKey(key, hashKey);//判斷是否存在散列字段施无。
3.redisTemplate.opsForHash().get(key, hashKey)辉词;// 獲取存儲(chǔ)在指定鍵的哈希字段的值。
4.redisTemplate.opsForHash().entries(key);//返回map集合
5.redisTemplate.opsForHash().increment(H key, HK hashKey, long delta);//獲取存儲(chǔ)在指定鍵的哈希中的所有字段和值
6.redisTemplate.opsForHash().keys(key)猾骡;//返回map的key集合Set
7.redisTemplate.opsForHash().size(key)瑞躺; //獲取散列中的字段數(shù)量
8.redisTemplate.opsForHash().multiGet(H key, Collection<HK> hashKeys);//獲取所有給定哈希字段的值
9.redisTemplate.opsForHash().putAll(H key, Map<? extends HK, ? extends HV> m);//為多個(gè)哈希字段分別設(shè)置它們的值
10.redisTemplate.opsForHash().put(key, hashKey, value);//設(shè)置散列字段的字符串值
11.redisTemplate.opsForHash().putIfAbsent(key, hashKey, value)兴想;//僅當(dāng)字段不存在時(shí)幢哨,才設(shè)置散列字段的值
12.redisTemplate.opsForHash().values(key);// 獲取哈希中的所有值
list類:
1.redisTemplate.opsForList().leftPush(key, value);//從左向右存壓棧
2.redisTemplate.opsForList().rightPush(key, value);//從右向左存壓棧
3.redisTemplate.opsForList().size(key);//獲取大小
4.redisTemplate.opsForList().range(key, start, end);//范圍檢索,根據(jù)下標(biāo),返回[start,end]的List
5.redisTemplate.opsForList().remove(key, i, value);//移除key中值為value的i個(gè),返回刪除的個(gè)數(shù)嫂便;如果沒(méi)有這個(gè)元素則返回0
6.redisTemplate.opsForList().index(key, index);//檢索
7.redisTemplate.opsForList().set(key, index, value);//賦值
8.redisTemplate.opsForList().trim(key, start, end);//裁剪,void,刪除除了[start,end]以外的所有元素
9.redisTemplate.opsForList().rightPopAndLeftPush(String sourceKey, String destinationKey);//將源key的隊(duì)列的右邊的一個(gè)值刪除捞镰,然后塞入目標(biāo)key的隊(duì)列的左邊,返回這個(gè)值
set類:
1.redisTemplate.opsForSet().add(key, value); ;//如果不存在這個(gè)字符串顽悼,將字符串存入set集合曼振,返回存入元素的個(gè)數(shù);如果存在這個(gè)字符串就不操作蔚龙,返回0冰评;
2.redisTemplate.opsForSet().members(key);//列出key的所有set集合
3.redisTemplate.opsForSet().randomMember(key);//隨機(jī)取key的一個(gè)set元素
4.redisTemplate.opsForSet().distinctRandomMembers(key,n);//隨機(jī)取N次key的元素,組成一個(gè)set集合木羹,不可以重復(fù)取出
5.redisTemplate.opsForSet().size(key);//返回set長(zhǎng)度
6.redisTemplate.opsForSet().difference(key, otherkey);//比較key與otherKey的set集合甲雅,取出與otherKey的set集合不一樣的set集合
7.redisTemplate.opsForSet().intersect(key, otherkey);//比較key與otherKey的set集合,取出二者交集坑填,返回set交集合
8.redisTemplate.opsForSet().union(key, otherkey);比較key與otherKey的set集合抛人,取出二者并集,返回set并集合
9.redisTemplate.opsForSet().unionAndStore(key, otherkey,destkey);比較key與otherKey的set集合脐瑰,取出二者并集妖枚,并存入destKey集合,返回destKey集合個(gè)數(shù)