标签:jdk framework 一个 继承 key template dex append 等于
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种数据结构的运用进行演示
spring 封装了 RedisTemplate 对象来进行对redis的各种操作,它支持所有的 redis 原生的 api。
RedisTemplate在spring代码中的结构如下:
org.springframework.data.redis.core
Class RedisTemplate<K,V>
java.lang.Object
org.springframework.data.redis.core.RedisAccessor
org.springframework.data.redis.core.RedisTemplate<K,V>
Type Parameters:
K
redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set
两者的关系是StringRedisTemplate继承RedisTemplate。
两者的数据是不共通的;也就是说StringRedisTemplate只能管理StringRedisTemplate里面的数据,RedisTemplate只能管理RedisTemplate中的数据。
SDR默认采用的序列化策略有两种,一种是String的序列化策略,一种是JDK的序列化策略。
StringRedisTemplate默认采用的是String的序列化策略,保存的key和value都是采用此策略序列化保存的。
RedisTemplate默认采用的是JDK的序列化策略,保存的key和value都是采用此策略序列化保存的。
RedisTemplate配置如下:
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
{
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(jackson2JsonRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
注意:如果使用RedisTemplate需要更改序列化方式
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
template.setKeySerializer(stringSerializer );
template.setValueSerializer(stringSerializer );
template.setHashKeySerializer(stringSerializer );
template.setHashValueSerializer(stringSerializer );
public interface ValueOperations<K,V>
Redis operations for simple (or in Redis terminology ‘string‘) values.
ValueOperations可以对String数据结构进行操作:
使用:redisTemplate.opsForValue().set("name","tom");
结果:redisTemplate.opsForValue().get("name") 输出结果为tom
使用:redisTemplate.opsForValue().set("name","tom",10, TimeUnit.SECONDS);
结果:redisTemplate.opsForValue().get("name")由于设置的是10秒失效,十秒之内查询有结果,十秒之后返回为null
使用:template.opsForValue().set("key","hello world");
template.opsForValue().set("key","redis", 6);
System.out.println("***************"+template.opsForValue().get("key"));
结果:***************hello redis
使用:System.out.println(template.opsForValue().setIfAbsent("multi1","multi1"));//false multi1之前已经存在
System.out.println(template.opsForValue().setIfAbsent("multi111","multi111"));//true multi111之前不存在
结果:false
true
使用: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));
结果:[multi1, multi2, multi3]
使用: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));
结果:true
false
使用:template.opsForValue().set("key","hello world");
System.out.println("***************"+template.opsForValue().get("key"));
结果:***************hello world
使用:template.opsForValue().set("getSetTest","test");
System.out.println(template.opsForValue().getAndSet("getSetTest","test2"));
结果:test
使用: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));
结果:[multi1, multi2, multi3]
使用:template.opsForValue().increment("increlong",1);
System.out.println("***************"+template.opsForValue().get("increlong"));
结果:***************1
使用:template.opsForValue().increment("increlong",1.2);
System.out.println("***************"+template.opsForValue().get("increlong"));
结果:***************2.2
使用:template.opsForValue().append("appendTest","Hello");
System.out.println(template.opsForValue().get("appendTest"));
template.opsForValue().append("appendTest","world");
System.out.println(template.opsForValue().get("appendTest"));
结果:Hello
Helloworld
使用:appendTest对应的value为Helloworld
System.out.println("*********"+template.opsForValue().get("appendTest",0,5));
结果:*********Hellow
使用:System.out.println("*********"+template.opsForValue().get("appendTest",0,-1));
结果:*********Helloworld
使用:System.out.println("*********"+template.opsForValue().get("appendTest",-3,-1));
结果:*********rld
使用:template.opsForValue().set("key","hello world");
System.out.println("***************"+template.opsForValue().size("key"));
结果:***************11
使用:template.opsForValue().set("bitTest","a");
// ‘a‘ 的ASCII码是 97。转换为二进制是:01100001
// ‘b‘ 的ASCII码是 98 转换为二进制是:01100010
// ‘c‘ 的ASCII码是 99 转换为二进制是:01100011
//因为二进制只有0和1,在setbit中true为1,false为0,因此我要变为‘b‘的话第六位设置为1,第七位设置为0
template.opsForValue().setBit("bitTest",6, true);
template.opsForValue().setBit("bitTest",7, false);
System.out.println(template.opsForValue().get("bitTest"));
结果:b
使用:System.out.println(template.opsForValue().getBit("bitTest",7));
结果:false
这边我们把RedisTemplate序列化方式改回之前的
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setKeySerializer(jackson2JsonRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
public interface ListOperations<K,V>
Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素导列表的头部(左边)或者尾部(右边)
ListOperations专门操作list列表:
使用:System.out.println(template.opsForList().range("list",0,-1));
结果:[c#, c++, python, java, c#, c#]
使用:System.out.println(template.opsForList().range("list",0,-1));
template.opsForList().trim("list",1,-1);//裁剪第一个元素
System.out.println(template.opsForList().range("list",0,-1));
结果:[c#, c++, python, java, c#, c#]
[c++, python, java, c#, c#]
使用:System.out.println(template.opsForList().size("list"));
结果:6
使用:template.opsForList().leftPush("list","java");
template.opsForList().leftPush("list","python");
template.opsForList().leftPush("list","c++");
结果:返回的结果为推送操作后的列表的长度
1
2
3
使用:String[] stringarrays = new String[]{"1","2","3"};
template.opsForList().leftPushAll("listarray",stringarrays);
System.out.println(template.opsForList().range("listarray",0,-1));
结果:[3, 2, 1]
使用: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));
结果:[3, 2, 1]
使用: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"));
结果:
0
0
==========分割线===========
1
2
使用:template.opsForList().leftPush("list","java","oc");
System.out.print(template.opsForList().range("list",0,-1));
结果:[c++, python, oc, java, c#, c#]
使用:template.opsForList().rightPush("listRight","java");
template.opsForList().rightPush("listRight","python");
template.opsForList().rightPush("listRight","c++");
结果:
1
2
3
使用:String[] stringarrays = new String[]{"1","2","3"};
template.opsForList().rightPushAll("listarrayright",stringarrays);
System.out.println(template.opsForList().range("listarrayright",0,-1));
结果:[1, 2, 3]
使用: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));
结果:[1, 2, 3]
使用: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"));
结果:0
0
==========分割线===========
1
2
使用:System.out.println(template.opsForList().range("listRight",0,-1));
template.opsForList().rightPush("listRight","python","oc");
System.out.println(template.opsForList().range("listRight",0,-1));
结果:[java, python, c++]
[java, python, oc, c++]
使用:System.out.println(template.opsForList().range("listRight",0,-1));
template.opsForList().set("listRight",1,"setValue");
System.out.println(template.opsForList().range("listRight",0,-1));
结果:[java, python, oc, c++]
[java, setValue, oc, c++]
使用:System.out.println(template.opsForList().range("listRight",0,-1));
template.opsForList().remove("listRight",1,"setValue");//将删除列表中存储的列表中第一次次出现的“setValue”。
System.out.println(template.opsForList().range("listRight",0,-1));
结果:[java, setValue, oc, c++]
[java, oc, c++]
使用:System.out.println(template.opsForList().range("listRight",0,-1));
System.out.println(template.opsForList().index("listRight",2));
结果:[java, oc, c++]
c++
使用: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));
结果:
[c++, python, oc, java, c#, c#]
c++
[python, oc, java, c#, c#]
使用:用法与 leftPop(K key);一样
使用: 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));
结果:[python, oc, java, c#, c#]
c#
[python, oc, java, c#]
使用:用法与 rightPop(K key);一样
使用: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));
结果:[oc, java,c#]
[oc, java]
[c#]
使用:用法与rightPopAndLeftPush(K sourceKey, K destinationKey)一样
Redis的散列可以让用户将多个键值对存储到一个Redis键里面。
public interface HashOperations<H,HK,HV>
HashOperations提供一系列方法操作hash:
初始数据:
//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);
使用:System.out.println(template.opsForHash().delete("redisHash","name"));
System.out.println(template.opsForHash().entries("redisHash"));
结果:1
{class=6, age=28.1}
使用:System.out.println(template.opsForHash().hasKey("redisHash","age"));
System.out.println(template.opsForHash().hasKey("redisHash","ttt"));
结果:true
false
使用:System.out.println(template.opsForHash().get("redisHash","age"));
结果:26
使用:List<Object> kes = new ArrayList<Object>();
kes.add("name");
kes.add("age");
System.out.println(template.opsForHash().multiGet("redisHash",kes));
结果:[jack, 28.1]
使用:System.out.println(template.opsForHash().get("redisHash","age"));
System.out.println(template.opsForHash().increment("redisHash","age",1));
结果:26
27
使用:System.out.println(template.opsForHash().get("redisHash","age"));
System.out.println(template.opsForHash().increment("redisHash","age",1.1));
结果:27
28.1
使用:System.out.println(template.opsForHash().keys("redisHash1"));
//redisHash1所对应的散列表为{class=1, name=jack, age=27}
结果:[name, class, age]
使用:System.out.println(template.opsForHash().size("redisHash1"));
//redisHash1所对应的散列表为{class=1, name=jack, age=27}
结果:3
使用: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"));
结果:{class=1, name=jack, age=27}
使用: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"));
结果:{age=26, class=6, name=tom}
使用:System.out.println(template.opsForHash().putIfAbsent("redisHash","age",30));
System.out.println(template.opsForHash().putIfAbsent("redisHash","kkk","kkk"));
结果:false
true
使用:System.out.println(template.opsForHash().values("redisHash"));
结果:[tom, 26, 6]
使用:System.out.println(template.opsForHash().entries("redisHash"));
结果:{age=26, class=6, name=tom}
使用: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());
}
结果:age:28.1
class:6
kkk:kkk
Redis的Set是string类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。
Redis 中 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。
public interface SetOperations<K,V>
SetOperations提供了对无序集合的一系列操作:
使用:String[] strarrays = new String[]{"strarr1","sgtarr2"};
System.out.println(template.opsForSet().add("setTest", strarrays));
结果:2
使用:String[] strarrays = new String[]{"strarr1","sgtarr2"};
System.out.println(template.opsForSet().remove("setTest",strarrays));
结果:2
使用:System.out.println(template.opsForSet().pop("setTest"));
System.out.println(template.opsForSet().members("setTest"));
结果:bbb
[aaa, ccc]
使用:template.opsForSet().move("setTest","aaa","setTest2");
System.out.println(template.opsForSet().members("setTest"));
System.out.println(template.opsForSet().members("setTest2"));
结果:[ccc]
[aaa]
使用:System.out.println(template.opsForSet().size("setTest"));
结果:1
使用:System.out.println(template.opsForSet().isMember("setTest","ccc"));
System.out.println(template.opsForSet().isMember("setTest","asd"));
结果:true
false
使用:System.out.println(template.opsForSet().members("setTest"));
System.out.println(template.opsForSet().members("setTest2"));
System.out.println(template.opsForSet().intersect("setTest","setTest2"));
结果:[aaa, ccc]
[aaa]
[aaa]
使用: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));
结果:[aaa, ccc]
[aaa]
[ccc, aaa]
[aaa]
使用: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"));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
2
[aaa, ccc]
使用: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"));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[ccc, aaa]
2
[aaa, ccc]
使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
System.out.println(template.opsForSet().union("setTest","setTest2"));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
[ccc, aaa, ddd, bbb]
使用: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));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[xxx, ccc, aaa]
[ddd, xxx, bbb, aaa, ccc]
使用: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"));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
4
unionAndStoreTest1:[ccc, aaa, ddd, bbb]
使用: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"));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[xxx, ccc, aaa]
5
unionAndStoreTest2:[ddd, xxx, bbb, aaa, ccc]
使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
System.out.println(template.opsForSet().difference("setTest","setTest2"));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
[bbb, ddd]
使用: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));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[xxx, ccc, aaa]
[bbb, ddd]
使用: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"));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
2
differenceAndStore1:[bbb, ddd]
使用: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"));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[xxx, ccc, aaa]
2
differenceAndStore2:[bbb, ddd]
使用:System.out.println(template.opsForSet().members("setTest"));
结果:[ddd, bbb, aaa, ccc]
使用: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"));
结果:setTest:[ddd, bbb, aaa, ccc]
setTestrandomMember:aaa
setTestrandomMember:bbb
setTestrandomMember:aaa
setTestrandomMember:ddd
使用:System.out.println("randomMembers:" + template.opsForSet().distinctRandomMembers("setTest",5));
结果:randomMembers:[aaa, bbb, ddd, ccc]
使用:System.out.println("randomMembers:" + template.opsForSet().randomMembers("setTest",5));
结果:randomMembers:[ccc, ddd, ddd, ddd, aaa]
使用: Cursor<Object> curosr = template.opsForSet().scan("setTest", ScanOptions.NONE);
while(curosr.hasNext()){
System.out.println(curosr.next());
}
结果:ddd
bbb
aaa
ccc
Redis 有序集合和无序集合一样也是string类型元素的集合,且不允许重复的成员。
不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。
有序集合的成员是唯一的,但分数(score)却可以重复。
public interface ZSetOperations<K,V>
ZSetOperations提供了一系列方法对有序集合进行操作:
使用:System.out.println(template.opsForZSet().add("zset1","zset-1",1.0));
结果:true
使用: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));
结果:[zset-1, zset-2, zset-3, zset-4, zset-5, zset-6]
使用: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));
结果:[zset-1, zset-2, zset-3, zset-4, zset-5, zset-6]
1
[zset-1, zset-2, zset-3, zset-4, zset-5]
使用:System.out.println(template.opsForZSet().incrementScore("zset1","zset-1",1.1)); //原为1.1
结果:2.2
使用:System.out.println(template.opsForZSet().range("zset1",0,-1));
System.out.println(template.opsForZSet().rank("zset1","zset-2"));
结果:[zset-2, zset-1, zset-3, zset-4, zset-5]
0 //表明排名第一
使用:System.out.println(template.opsForZSet().range("zset1",0,-1));
System.out.println(template.opsForZSet().reverseRank("zset1","zset-2"));
结果:[zset-2, zset-1, zset-3, zset-4, zset-5]
4 //递减之后排到第五位去了
使用:System.out.println(template.opsForZSet().range("zset1",0,-1));
结果:[zset-2, zset-1, zset-3, zset-4, zset-5]
使用: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());
}
结果: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
使用:System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));
结果:[zset-2, zset-1, zset-3]
使用: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());
}
结果:value:zset-2score:1.2
value:zset-1score:2.2
value:zset-3score:2.3
使用: System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));
System.out.println(template.opsForZSet().rangeByScore("zset1",0,5,1,2));
结果:[zset-2, zset-1, zset-3]
[zset-1, zset-3]
使用: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());
}
结果:value:zset-1score:2.2
value:zset-3score:2.3
使用:System.out.println(template.opsForZSet().reverseRange("zset1",0,-1));
结果:[zset-5, zset-4, zset-3, zset-1, zset-2]
使用: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());
}
结果: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
使用:与rangeByScore调用方法一样,其中有序集成员按分数值递减(从大到小)顺序排列
使用:与rangeByScoreWithScores调用方法一样,其中有序集成员按分数值递减(从大到小)顺序排列
使用:与rangeByScore调用方法一样,其中有序集成员按分数值递减(从大到小)顺序排列
使用:与rangeByScoreWithScores调用方法一样,其中有序集成员按分数值递减(从大到小)顺序排列
使用:System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));
System.out.println(template.opsForZSet().count("zset1",0,5));
结果:[zset-2, zset-1, zset-3]
3
使用:System.out.println(template.opsForZSet().size("zset1"));
结果:6
使用:System.out.println(template.opsForZSet().zCard("zset1"));
结果:6
使用:System.out.println(template.opsForZSet().score("zset1","zset-1"));
结果:2.2
使用: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));
结果:[zset-1, zset-2, zset-3, zset-4]
2
[zset-1, zset-4]
使用://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));
结果:[zset-1, zset-2, zset-3,zset-4]
1
[zset-1, zset-2, zset-4]
使用: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());
}
结果: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
使用://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());
}
结果: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
使用: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());
}
结果:value:zset-1score:2.0
value:zset-2score:4.0
value:zset-3score:6.0
value:zset-4score:12.0
使用: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());
}
结果:value:zset-1score:3.0
value:zset-2score:6.0
value:zset-3score:9.0
value:zset-4score:18.0
使用: 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());
}
结果:zset-1:1.0
zset-2:2.0
zset-3:3.0
zset-4:6.0
注:TimeUnit是java.util.concurrent包下面的一个类,表示给定单元粒度的时间段
常用的颗粒度
TimeUnit.DAYS //天
TimeUnit.HOURS //小时
TimeUnit.MINUTES //分钟
TimeUnit.SECONDS //秒
TimeUnit.MILLISECONDS //毫秒
标签:jdk framework 一个 继承 key template dex append 等于
原文地址:https://www.cnblogs.com/-flq/p/9066422.html