本文目录

  • 我只是一直菜鸟
    • Redis数据结构简介
    • RedisTemplate介绍
    • StringRedisTemplate与RedisTemplate
    • RedisTemplate配置如下:
    • Redis的String数据结构
    • Redis的List数据结构
    • Redis的Hash数据机构
    • Redis的Set数据结构
    • Redis的ZSet数据结构
    • 评论点赞排序

我只是一直菜鸟

最近做一个论坛项目,在评论方面涉及到了点赞排序,想用redis来实现
转载:https://www.jianshu.com/p/7bf5dc61ca06

Redis数据结构简介

Redis可以存储键与5中不同数据结构类型之间的映射,这5种数据结构类型分别为String、List、Set、Hash、Zset。

Redis数据结构简介:

结构类型 结构存储的值 结构的读写能力
String 可以是字符串 对整个字符串或者字符串的其中一部分执行操作;对象和浮点数执行自增(increment)或者自减(decrement)
List 一个链表,链表上的每个节点都包含了一个字符串 从链表的两端推入或弹出元素;根据偏移量对链表进行修剪(trim);读取单个或多个元素;根据值来查找或移除元素
Set 包含字符串的无序收集器,无重复 添加、获取、移除单个元素;检查一个元素是否存在与某个集合中;计算交集、并集、差集;从集合里随机取元素
Hash 包含键值的散列无序列表 添加、获取、移除单个键值对;获取所有的键值对
Zset 字符串成员与浮点数分值之间的有序映射,元素的排列顺序由分值决定 添加、获取、删除单个元素;根据分值范围或者成员来获取元素

spring的redisTemplate的Api: https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/RedisTemplate.html

RedisTemplate介绍

spring 封装了 RedisTemplate 对象来进行对redis的各种操作,它支持所有的 redis 原生的 api。
RedisTemplate在spring代码中的结构如下:

org.springframework.data.redis.core
Class RedisTemplate<K,V>
java.lang.Objectorg.springframework.data.redis.core.RedisAccessororg.springframework.data.redis.core.RedisTemplate<K,V>

K: 模板中的Redis key的类型(通常为String)如:RedisTemplate<String, Object>
(注意:如果没特殊情况,切勿定义成RedisTemplate<Object, Object>,否则根据里氏替换原则,使用的时候会造成类型错误 。)
V:模板中的Redis value的类型

StringRedisTemplate与RedisTemplate

1.两者的关系是StringRedisTemplate继承RedisTemplate。
2.两者的数据是不共通的;也就是说StringRedisTemplate只能管理StringRedisTemplate里面的数据,RedisTemplate只能管理RedisTemplate中的数据。
3.SDR默认采用的序列化策略有两种,一种是String的序列化策略,一种是JDK的序列化策略。
StringRedisTemplate默认采用的是String的序列化策略,保存的key和value都是采用此策略序列化保存的。

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

RedisTemplate配置如下:

package com.fx110.forexKnow.common.redis;import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;/*** Redis多数据源配置类* @author Wayne.M* 2019年2月15日*/
@Configuration
public class RedisConfig {private StringRedisTemplate getStringRedisTemplate(RedisConnectionFactory factory) {StringRedisTemplate template = new StringRedisTemplate(factory);template.afterPropertiesSet();return template;}//    private RedisTemplate<String, Object> getObjectRedisTemplate(RedisConnectionFactory factory) {//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(factory);
//        return redisTemplate;
//    }@Bean@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")public GenericObjectPoolConfig redisPool() {return new GenericObjectPoolConfig();}@Bean@ConfigurationProperties(prefix = "spring.redis.db3")public RedisStandaloneConfiguration commentRedisConfig() {return new RedisStandaloneConfiguration();}@Bean("commentFactory")public LettuceConnectionFactory commentFactory(GenericObjectPoolConfig config, RedisStandaloneConfiguration commentRedisConfig) {LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();return new LettuceConnectionFactory(commentRedisConfig, clientConfiguration);}@Bean("commentStringRedisTemplate")public StringRedisTemplate countryStringRedisTemplate(@Qualifier("commentFactory") RedisConnectionFactory factory) {return getStringRedisTemplate(factory);}
}

配置文件:

 redis:db3:database: 3         #redis缓存数据库下标hostName: *******      #redis缓存地址port: 6379          #redis缓存端口password: *********         #reids登录密码lettuce:pool:max-active: 8      #连接池最大连接数(使用负值表示没有限制) 默认 8max-idle: 8        #连接池中的最大空闲连接 默认 8max-wait: -1       #连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1min-idle: 0        #连接池中的最小空闲连接 默认 0

Redis的String数据结构

public interface ValueOperations<K,V>

推荐使用StringRedisTemplate
如果使用RedisTemplate需要更改序列化,详细连接: https://www.jianshu.com/p/7bf5dc61ca06
1.set void set(K key, V value);

使用:redisTemplate.opsForValue().set("name","tom");
结果:redisTemplate.opsForValue().get("name")  输出结果为tom

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

使用:redisTemplate.opsForValue().set("name","tom",10, TimeUnit.SECONDS);
结果:redisTemplate.opsForValue().get("name")由于设置的是10秒失效,十秒之内查询有结果,十秒之后返回为null

3.set void set(K key, V value, long offset);该方法是用 value 参数覆写(overwrite)给定 key 所储存的字符串值,从偏移量 offset 开始

使用:template.opsForValue().set("key","hello world");template.opsForValue().set("key","redis", 6);System.out.println("***************"+template.opsForValue().get("key"));
结果:***************hello redis

4.setIfAbsent Boolean setIfAbsent(K key, V value);
如果key已存在则返回false,如果key值不存在则返回true并将值写入。

使用:System.out.println(template.opsForValue().setIfAbsent("multi1","multi1"));//false  multi1之前已经存在System.out.println(template.opsForValue().setIfAbsent("multi111","multi111"));//true  multi111之前不存在
结果:false
true

5.multiSet void multiSet(Map<? extends K, ? extends V> m);
为多个键分别设置它们的值

使用: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]

6.multiSetIfAbsent Boolean multiSetIfAbsent(Map<? extends K, ? extends V> m);
为多个键分别设置它们的值,如果存在则返回false,不存在返回true

使用: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

7.get V get(Object key);

使用:template.opsForValue().set("key","hello world");System.out.println("***************"+template.opsForValue().get("key"));
结果:***************hello world

8.getAndSet V getAndSet(K key, V value);设置键的字符串值并返回其旧值

使用:template.opsForValue().set("getSetTest","test");System.out.println(template.opsForValue().getAndSet("getSetTest","test2"));
结果:test

9.multiGet List multiGet(Collection keys);为多个键分别取出它们的值

使用: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]

10.increment Long increment(K key, long delta);支持整数

使用:template.opsForValue().increment("increlong",1);System.out.println("***************"+template.opsForValue().get("increlong"));
结果:***************1

11.increment Double increment(K key, double delta);
也支持浮点数

使用:template.opsForValue().increment("increlong",1.2);System.out.println("***************"+template.opsForValue().get("increlong"));
结果:***************2.2

12.append Integer append(K key, String value);
如果key已经存在并且是一个字符串,则该命令将该值追加到字符串的末尾。如果键不存在,则它被创建并设置为空字符串,因此APPEND在这种特殊情况下将类似于SET。

使用:template.opsForValue().append("appendTest","Hello");System.out.println(template.opsForValue().get("appendTest"));template.opsForValue().append("appendTest","world");System.out.println(template.opsForValue().get("appendTest"));
结果:HelloHelloworld

13.get String get(K key, long start, long end);截取key所对应的value字符串

使用: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

14.size Long size(K key);
返回key所对应的value值得长度

使用:template.opsForValue().set("key","hello world");System.out.println("***************"+template.opsForValue().size("key"));
结果:***************11

15.setBit Boolean setBit(K key, long offset, boolean value);
对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit)
key键对应的值value对应的ascii码,在offset的位置(从左向右数)变为value

在这使用: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,第七位设置为0template.opsForValue().setBit("bitTest",6, true);template.opsForValue().setBit("bitTest",7, false);System.out.println(template.opsForValue().get("bitTest"));
结果:b

16.getBit Boolean getBit(K key, long offset);
获取键对应值的ascii码的在offset处位值

使用:System.out.println(template.opsForValue().getBit("bitTest",7));
结果:false

Redis的List数据结构

public interface ListOperations<K,V>

1.List range(K key, long start, long end);
返回存储在键中的列表的指定元素。偏移开始和停止是基于零的索引,其中0是列表的第一个元素(列表的头部),1是下一个元素

使用:System.out.println(template.opsForList().range("list",0,-1));
结果:[c#, c++, python, java, c#, c#]

2.void trim(K key, long start, long end);
修剪现有列表,使其只包含指定的指定范围的元素,起始和停止都是基于0的索引

使用: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#]

3.Long size(K key);
返回存储在键中的列表的长度。如果键不存在,则将其解释为空列表,并返回0。当key存储的值不是列表时返回错误。

使用:System.out.println(template.opsForList().size("list"));
结果:6

4.Long leftPush(K key, V value);
将所有指定的值插入存储在键的列表的头部。如果键不存在,则在执行推送操作之前将其创建为空列表。(从左边插入)

使用:template.opsForList().leftPush("list","java");template.opsForList().leftPush("list","python");template.opsForList().leftPush("list","c++");
结果:返回的结果为推送操作后的列表的长度
1
2
3

5.Long leftPushAll(K key, V… values);
批量把一个数组插入到列表中

使用:String[] stringarrays = new String[]{"1","2","3"};template.opsForList().leftPushAll("listarray",stringarrays);System.out.println(template.opsForList().range("listarray",0,-1));
结果:[3, 2, 1]

6.Long leftPushIfPresent(K key, V value);
只有存在key对应的列表才能将这个value值插入到key所对应的列表中

使用: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

7.Long leftPush(K key, V pivot, V value);
把value值放到key对应列表中pivot值的左面,如果pivot值存在的话

使用:template.opsForList().leftPush("list","java","oc");System.out.print(template.opsForList().range("list",0,-1));
结果:[c++, python, oc, java, c#, c#]

8.Long rightPush(K key, V value);
将所有指定的值插入存储在键的列表的头部。如果键不存在,则在执行推送操作之前将其创建为空列表。(从右边插入)

使用:template.opsForList().rightPush("listRight","java");template.opsForList().rightPush("listRight","python");template.opsForList().rightPush("listRight","c++");
结果:
1
2
3

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

使用:String[] stringarrays = new String[]{"1","2","3"};template.opsForList().rightPushAll("listarrayright",stringarrays);System.out.println(template.opsForList().range("listarrayright",0,-1));
结果:[1, 2, 3]

10.Long rightPushAll(K key, Collection values);

使用: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]

11.Long rightPushIfPresent(K key, V value);
只有存在key对应的列表才能将这个value值插入到key所对应的列表中

使用: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

12.Long rightPush(K key, V pivot, V value);
把value值放到key对应列表中pivot值的右面,如果pivot值存在的话

使用: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++]

13.void set(K key, long index, V value);
在列表中index的位置设置value值

使用: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++]

14.Long remove(K key, long count, Object value);
从存储在键中的列表中删除等于值的元素的第一个计数事件。
计数参数以下列方式影响操作:
count> 0:删除等于从头到尾移动的值的元素。
count <0:删除等于从尾到头移动的值的元素。
count = 0:删除等于value的所有元素

使用: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++]

15.V index(K key, long index);
根据下表获取列表中的值,下标是从0开始的

使用:System.out.println(template.opsForList().range("listRight",0,-1));
System.out.println(template.opsForList().index("listRight",2));
结果:[java, oc, c++]
c++

16.V leftPop(K key);
弹出最左边的元素,弹出之后该值在列表中将不复存在

使用: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#]

17.V leftPop(K key, long timeout, TimeUnit unit);
移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。

18.V rightPop(K key, long timeout, TimeUnit unit);
弹出最右边的元素,弹出之后该值在列表中将不复存在

19.V rightPop(K key, long timeout, TimeUnit unit);
移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。

20.V rightPopAndLeftPush(K sourceKey, K destinationKey);
用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回。

使用: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#]

21.V rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit);
用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回,如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。

在这里插入代码片

Redis的Hash数据机构

public interface HashOperations<H,HK,HV>

1.Long delete(H key, Object… hashKeys);
删除给定的哈希hashKeys

使用:System.out.println(template.opsForHash().delete("redisHash","name"));System.out.println(template.opsForHash().entries("redisHash"));
结果:1
{class=6, age=28.1}

2.Boolean hasKey(H key, Object hashKey);
确定哈希hashKey是否存在

使用:System.out.println(template.opsForHash().hasKey("redisHash","age"));System.out.println(template.opsForHash().hasKey("redisHash","ttt"));
结果:true
false

3.List multiGet(H key, Collection hashKeys);
从哈希中获取给定hashKey的值

使用:List<Object> kes = new ArrayList<Object>();kes.add("name");kes.add("age");System.out.println(template.opsForHash().multiGet("redisHash",kes));
结果:[jack, 28.1]

4.Long increment(H key, HK hashKey, long delta);
通过给定的delta增加散列hashKey的值(整型)

使用:System.out.println(template.opsForHash().get("redisHash","age"));System.out.println(template.opsForHash().increment("redisHash","age",1));
结果:26
27

5.Double increment(H key, HK hashKey, double delta);
通过给定的delta增加散列hashKey的值(浮点数)

使用:System.out.println(template.opsForHash().get("redisHash","age"));System.out.println(template.opsForHash().increment("redisHash","age",1.1));
结果:27
28.1

6.Set keys(H key);
获取key所对应的散列表的key

使用:System.out.println(template.opsForHash().keys("redisHash1"));
//redisHash1所对应的散列表为{class=1, name=jack, age=27}
结果:[name, class, age]

7.Long size(H key);
获取key所对应的散列表的大小个数
使用:System.out.println(template.opsForHash().size(“redisHash1”));
//redisHash1所对应的散列表为{class=1, name=jack, age=27}
结果:3
8.void putAll(H key, Map<? extends HK, ? extends HV> m);
使用m中提供的多个散列字段设置到key对应的散列表中

使用: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}

9.void put(H key, HK hashKey, HV value);
设置散列hashKey的值

使用: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}

10.Boolean putIfAbsent(H key, HK hashKey, HV value);
仅当hashKey不存在时才设置散列hashKey的值。

使用:System.out.println(template.opsForHash().putIfAbsent("redisHash","age",30));
System.out.println(template.opsForHash().putIfAbsent("redisHash","kkk","kkk"));
结果:false
true

11.List values(H key);
获取整个哈希存储的值

使用:System.out.println(template.opsForHash().values("redisHash"));
结果:[tom, 26, 6]

12.Map<HK, HV> entries(H key);
获取整个哈希存储

使用:System.out.println(template.opsForHash().entries("redisHash"));
结果:{age=26, class=6, name=tom}

13.Cursor<Map.Entry<HK, HV>> scan(H key, ScanOptions options);
使用Cursor在key的hash中迭代,相当于迭代器。

使用: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数据结构

public interface SetOperations<K,V>

1.Long add(K key, V… values);
无序集合中添加元素,返回添加个数
也可以直接在add里面添加多个值 如:template.opsForSet().add(“setTest”,“aaa”,“bbb”)

使用:String[] strarrays = new String[]{"strarr1","sgtarr2"};System.out.println(template.opsForSet().add("setTest", strarrays));
结果:2

2.Long remove(K key, Object… values);
移除集合中一个或多个成员

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

3.V pop(K key);
移除并返回集合中的一个随机元素
使用:System.out.println(template.opsForSet().pop(“setTest”));
System.out.println(template.opsForSet().members(“setTest”));
结果:bbb
[aaa, ccc]
4.Boolean move(K key, V value, K destKey);
将 member 元素从 source 集合移动到 destination 集合

使用:template.opsForSet().move("setTest","aaa","setTest2");System.out.println(template.opsForSet().members("setTest"));System.out.println(template.opsForSet().members("setTest2"));
结果:[ccc]
[aaa]

5.Long size(K key);
无序集合的大小长度

使用:System.out.println(template.opsForSet().size("setTest"));
结果:1

6.Boolean isMember(K key, Object o);
判断 member 元素是否是集合 key 的成员

使用:System.out.println(template.opsForSet().isMember("setTest","ccc"));System.out.println(template.opsForSet().isMember("setTest","asd"));
结果:true
false

7.Set intersect(K key, K otherKey);
key对应的无序集合与otherKey对应的无序集合求交集

使用: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]

8.Set intersect(K key, Collection otherKeys);
key对应的无序集合与多个otherKey对应的无序集合求交集

使用: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]

9.Long intersectAndStore(K key, K otherKey, K destKey);
key无序集合与otherkey无序集合的交集存储到destKey无序集合中

使用: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]

10.Long intersectAndStore(K key, Collection otherKeys, K destKey);
key对应的无序集合与多个otherKey对应的无序集合求交集存储到destKey无序集合中

使用: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]

11.Set union(K key, K otherKey);
key无序集合与otherKey无序集合的并集

使用: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]

12.Set union(K key, Collection otherKeys);
key无序集合与多个otherKey无序集合的并集

使用: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]

13.Long unionAndStore(K key, K otherKey, K destKey);
key无序集合与otherkey无序集合的并集存储到destKey无序集合中

使用: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]

14.Long unionAndStore(K key, Collection otherKeys, K destKey);
key无序集合与多个otherkey无序集合的并集存储到destKey无序集合中

使用: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]

15.Set difference(K key, K otherKey);
key无序集合与otherKey无序集合的差集

使用: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]

16.Set difference(K key, Collection otherKeys);
key无序集合与多个otherKey无序集合的差集

使用: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]

17.Long differenceAndStore(K key, K otherKey, K destKey);
key无序集合与otherkey无序集合的差集存储到destKey无序集合中
以上来自简书作者 DreamerRzc 链接: https://www.jianshu.com/p/7bf5dc61ca06

使用: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]

18.Long differenceAndStore(K key, Collection otherKeys, K destKey);
key无序集合与多个otherkey无序集合的差集存储到destKey无序集合中

使用: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]

19.Set members(K key);
返回集合中的所有成员

使用:System.out.println(template.opsForSet().members("setTest"));
结果:[ddd, bbb, aaa, ccc]

20.V randomMember(K key);
随机获取key无序集合中的一个元素

使用: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

21.Set distinctRandomMembers(K key, long count);
随机获取多个key无序集合中的元素(去重),count表示个数

使用:System.out.println("randomMembers:" + template.opsForSet().distinctRandomMembers("setTest",5));
结果:randomMembers:[aaa, bbb, ddd, ccc]

22.List randomMembers(K key, long count);
获取多个key无序集合中的元素,count表示个数

使用:System.out.println("randomMembers:" + template.opsForSet().randomMembers("setTest",5));
结果:randomMembers:[ccc, ddd, ddd, ddd, aaa]

23.Cursor scan(K key, ScanOptions options);
遍历set

使用: Cursor<Object> curosr = template.opsForSet().scan("setTest", ScanOptions.NONE);while(curosr.hasNext()){System.out.println(curosr.next());}
结果:ddd
bbb
aaa
ccc

Redis的ZSet数据结构

public interface ZSetOperations<K,V>

Redis 有序集合和无序集合一样也是string类型元素的集合,且不允许重复的成员。

1.Boolean add(K key, V value, double score);
新增一个有序集合,存在的话为false,不存在的话为true

使用:System.out.println(template.opsForZSet().add("zset1","zset-1",1.0));
结果:true

2.Long add(K key, Set<TypedTuple> tuples);
新增一个有序集合

使用: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]

3.Long remove(K key, Object… values);
从有序集合中移除一个或者多个元素

在使用: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]

4.Double incrementScore(K key, V value, double delta);
增加元素的score值,并返回增加后的值

使用:System.out.println(template.opsForZSet().incrementScore("zset1","zset-1",1.1));  //原为1.1
结果:2.2

5.Long rank(K key, Object o);
返回有序集中指定成员的排名,其中有序集成员按分数值递增(从小到大)顺序排列

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

6.Long reverseRank(K key, Object o);
返回有序集中指定成员的排名,其中有序集成员按分数值递减(从大到小)顺序排列

使用: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 //递减之后排到第五位去了

7.Set range(K key, long start, long end);
通过索引区间返回有序集合成指定区间内的成员,其中有序集成员按分数值递增(从小到大)顺序排列

使用:System.out.println(template.opsForZSet().range("zset1",0,-1));
结果:[zset-2, zset-1, zset-3, zset-4, zset-5]

8.Set<TypedTuple> rangeWithScores(K key, long start, long end);
通过索引区间返回有序集合成指定区间内的成员对象,其中有序集成员按分数值递增(从小到大)顺序排列

使用: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

9.Set rangeByScore(K key, double min, double max);
通过分数返回有序集合指定区间内的成员,其中有序集成员按分数值递增(从小到大)顺序排列

使用:System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));
结果:[zset-2, zset-1, zset-3]

10.Set<TypedTuple> rangeByScoreWithScores(K key, double min, double max);
通过分数返回有序集合指定区间内的成员对象,其中有序集成员按分数值递增(从小到大)顺序排列

使用: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

11.Set rangeByScore(K key, double min, double max, long offset, long count);
通过分数返回有序集合指定区间内的成员,并在索引范围内,其中有序集成员按分数值递增(从小到大)顺序排列

使用: 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]

12.Set<TypedTuple> rangeByScoreWithScores(K key, double min, double max, long offset, long count);
通过分数返回有序集合指定区间内的成员对象,并在索引范围内,其中有序集成员按分数值递增(从小到大)顺序排列

使用: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

13.Set reverseRange(K key, long start, long end);
通过索引区间返回有序集合成指定区间内的成员,其中有序集成员按分数值递减(从大到小)顺序排列

使用:System.out.println(template.opsForZSet().reverseRange("zset1",0,-1));
结果:[zset-5, zset-4, zset-3, zset-1, zset-2]

14.Set<TypedTuple> reverseRangeWithScores(K key, long start, long end);
通过索引区间返回有序集合成指定区间内的成员对象,其中有序集成员按分数值递减(从大到小)顺序排列

使用: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

15.Set reverseRangeByScore(K key, double min, double max);

16.Set<TypedTuple> reverseRangeByScoreWithScores(K key, double min, double max);

17.Set reverseRangeByScore(K key, double min, double max, long offset, long count);

18.Set<TypedTuple> reverseRangeByScoreWithScores(K key, double min, double max, long offset, long count);

19.Long count(K key, double min, double max);
通过分数返回有序集合指定区间内的成员个数

使用: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

20.Long size(K key);
获取有序集合的成员数,内部调用的就是zCard方法

使用:System.out.println(template.opsForZSet().size("zset1"));
结果:6

21.Long zCard(K key);
获取有序集合的成员数

使用:System.out.println(template.opsForZSet().zCard("zset1"));
结果:6

22.Double score(K key, Object o);
获取指定成员的score值

使用:System.out.println(template.opsForZSet().score("zset1","zset-1"));
结果:2.2

23.Long removeRange(K key, long start, long end);
移除指定索引位置的成员,其中有序集成员按分数值递增(从小到大)顺序排列

使用: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]

24.Long removeRangeByScore(K key, double min, double max);
根据指定的score值得范围来移除成员

使用://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]

25.Long unionAndStore(K key, K otherKey, K destKey);
计算给定的一个有序集的并集,并存储在新的 destKey中,key相同的话会把score值相加

使用: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

26.Long unionAndStore(K key, Collection otherKeys, K destKey);
计算给定的多个有序集的并集,并存储在新的 destKey中

使用://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

27.Long intersectAndStore(K key, K otherKey, K destKey);
计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中,score相加

使用: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

28.Long intersectAndStore(K key, Collection otherKeys, K destKey);
计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中

使用: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

29.Cursor<TypedTuple> scan(K key, ScanOptions options);
遍历zset

使用: 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

以上来源于简述 作者 DreamerRzc
链接:https://www.jianshu.com/p/7bf5dc61ca06

个人代码

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(masterRedisTemplate.opsForValue().multiSetIfAbsent(maps));System.out.println(masterRedisTemplate.opsForValue().multiSetIfAbsent(maps2));masterRedisTemplate.opsForZSet().add("一级评论id","comment1",2);masterRedisTemplate.opsForZSet().add("一级评论id","comment2",3);masterRedisTemplate.opsForZSet().add("一级评论id","comment3",4);masterRedisTemplate.opsForZSet().add("一级评论id","comment4",5);masterRedisTemplate.opsForZSet().add("一级评论id","comment5",6);masterRedisTemplate.opsForZSet().add("一级评论id","comment6",7);masterRedisTemplate.opsForZSet().add("一级评论id","comment7",8);System.out.println(slaveRedisTemplate.opsForZSet().range("一级评论id", 1, 6));
//      masterRedisTemplate.opsForZSet().removeRange("一级评论id",0,slaveRedisTemplate.opsForZSet().size("一级评论id"));
//      System.out.println(slaveRedisTemplate.opsForZSet().range("一级评论id", 1, 6));
//      System.out.println(slaveRedisTemplate.opsForZSet().range("一级评论id", 1, 6));
//      System.out.println();
//
        for (int i=0;i<1000;i++){            CommentModel commentModel1 = new CommentModel((long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),
                    "sfsdfsad fsdfsd",(long)(1+Math.random()*(100000-1+1)));
            CommentModel commentModel2= new CommentModel((long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),
                    "sfsdfsad fsdtfsd",(long)(1+Math.random()*(100000-1+1)));
            CommentModel commentModel3 = new CommentModel((long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),
                    "sfsdfsa fsdfsd",(long)(1+Math.random()*(100000-1+1)));
            CommentModel commentModel4 = new CommentModel((long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),
                    "sfsdfsad fsd1fsd",(long)(1+Math.random()*(100000-1+1)));
            CommentModel commentModel5 = new CommentModel((long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),
                    "sfsdfsad5 fs2dfsd",(long)(1+Math.random()*(100000-1+1)));
            CommentModel commentModel6 = new CommentModel((long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),
                    "sfsdfsad fswdfsd",(long)(1+Math.random()*(100000-1+1)));
            CommentModel commentModel7 = new CommentModel((long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),
                    "sfsdfsad fqsdfsd",(long)(1+Math.random()*(100000-1+1)));
            CommentModel commentModel8 = new CommentModel((long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),
                    "sfsdfsad fwsdfsd",(long)(1+Math.random()*(100000-1+1)));
            CommentModel commentModel9 = new CommentModel((long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),
                    "sfsdfsad fesdfsd",(long)(1+Math.random()*(100000-1+1)));
            CommentModel commentModel10 = new CommentModel((long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),
                    "sfsdfsad fssdfsd",(long)(1+Math.random()*(100000-1+1)));

            masterRedisTemplate.opsForHash().put("一级评论的id","comment一"+i,commentModel1);
            masterRedisTemplate.opsForHash().put("一级评论的id","comment二"+i,commentModel2);
            masterRedisTemplate.opsForHash().put("一级评论的id","comment三"+i,commentModel3);
            masterRedisTemplate.opsForHash().put("一级评论的id","comment四"+i,commentModel4);
            masterRedisTemplate.opsForHash().put("一级评论的id","comment五"+i,commentModel5);
            masterRedisTemplate.opsForHash().put("一级评论的id","comment六"+i,commentModel6);
            masterRedisTemplate.opsForHash().put("一级评论的id","comment七"+i,commentModel7);
            masterRedisTemplate.opsForHash().put("一级评论的id","comment八"+i,commentModel8);
            masterRedisTemplate.opsForHash().put("一级评论的id","comment九"+i,commentModel9);
            masterRedisTemplate.opsForHash().put("一级评论的id","comment十"+i,commentModel10);
        }
//      System.out.println("总条数------一共"+slaveRedisTemplate.opsForHash().size("一级评论的id"));
//      System.out.println("开始-------时间"+System.currentTimeMillis());
//      List values = slaveRedisTemplate.opsForHash().values("一级评论的id");
//
//
//      List<CommentModel> list = values;
//
//      List<CommentModel> historys = (List<CommentModel>) list.stream().sorted((r1, r2)->{//          if(r1.getIdeaId()==null) return 0;
//          if(r2.getIdeaId()==null) return 0;
//          if(r1.getIdeaId()<r2.getIdeaId()) return 1;
//          else return -1;
//      }).limit(20).collect(Collectors.toList());
//
//
//      System.out.println("结束-------时间"+System.currentTimeMillis());
//      System.out.println(historys.size());
//      System.out.println(historys.get(2));
//      System.out.println(historys.get(3));
//      System.out.println(historys.get(4));
//      System.out.println(historys.get(5));
//      slaveRedisTemplate.opsForHash().delete("一级评论的id",slaveRedisTemplate.opsForHash().keys("一级评论的id"));
//      slaveRedisTemplate.delete("一级评论的id");
//
        CommentModel commentModel12 = new CommentModel((long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),(long)(1+Math.random()*(100000-1+1)),
                    "sfsdfsad fsdfsd",(long)(1+Math.random()*(100000-1+1)));
        masterRedisTemplate.opsForHash().put("测试更新","id19",commentModel12);
        masterRedisTemplate.opsForHash().put("测试更新","id19",commentModel12);
//      Object o = slaveRedisTemplate.opsForHash().get("测试更新", "id19");
//
//      System.out.println((CommentModel)o);
//      masterRedisTemplate.opsForHash().delete("测试更新", "id19");
//      Object o2 = slaveRedisTemplate.opsForHash().get("测试更新", "id19");
//      System.out.println((CommentModel)o2);System.out.println(slaveRedisTemplate.hasKey("commentId2"));List values = slaveRedisTemplate.opsForHash().values("commentId_");
//
//          masterRedisTemplate.opsForZSet().add();
//          masterRedisTemplate.opsForZSet().remove()slaveRedisTemplate.keys("abc"+"*");System.out.println("00000000");List<RedisCommentModel> list = values;List<RedisCommentModel> historys = (List<RedisCommentModel>) list.stream().sorted((r1, r2)->{if(r1.getIdeaId()==null) return 0;if(r2.getIdeaId()==null) return 0;if(r1.getIdeaId()<r2.getIdeaId()) return 1;else return -1;}).limit(20).collect(Collectors.toList());System.out.println(historys);return null;//          masterRedisTemplate.opsForHash().increment()
//          masterRedisTemplate.expire()

评论点赞排序

使用Hash与Zset结合,点赞数作为分数存储。

如何使用RedisTemplate访问Redis数据结构相关推荐

  1. redistemplate怎么修改数据_如何使用RedisTemplate访问Redis数据结构?

    在springboot项目中,集成各种框架变得非常容易.下面简单介绍一下如何在springboot项目中集成单机模式redis.集群模式也差不多,这里就不过多介绍了. 首先你得安装redis服务,无论 ...

  2. 如何使用RedisTemplate访问Redis数据结构-记录

    Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序集 ...

  3. zset 怎么get_如何使用RedisTemplate访问Redis数据结构之Zset

    Redis的ZSet数据结构 Redis 有序集合和无序集合一样也是string类型元素的集合,且不允许重复的成员. 不同的是每个元素都会关联一个double类型的分数.redis正是通过分数来为集合 ...

  4. 【过程记录】springboot整合redis/分别用redisRepository和redistemplate操作redis

    导入依赖 基本配置 使用RedisTemplate访问redis 使用Redisrepository访问redis 实例: 导入依赖 菜单大部分情况下不会出现变化,我们可以将其放入Redis 加快加载 ...

  5. Springboot2.0访问Redis集群

    Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作高性能的key-value数据库.缓存和消息中间件,掌握它是程序员的必备技能,下面是一个springboot访问redis的 ...

  6. Redis数据结构Hash应用场景-存储商品、购物车、淘宝短链接、分布式Session、用户注册、发微博功能

    Hash应用场景 Hash Hash应用场景 redis存储java对象常用String,那为什么还要用hash来存储? SpringBoot+redis+hash存储商品数据 短链接 场景1:淘宝短 ...

  7. redis数据结构及使用场景

    redis数据结构及使用场景 1.字符串(String) String是最常用的一种数据类型,普通的k/v存储都可以归为此类.redis是使用C语言开发,但C中并没有字符串类型,只能使用指针或符数组的 ...

  8. 为了拿捏 Redis 数据结构,我画了 40 张图

    Redis 为什么那么快? 除了它是内存数据库,使得所有的操作都在内存上进行之外,还有一个重要因素,它实现的数据结构,使得我们对数据进行增删查改操作时,Redis 能高效的处理. 因此,这次我们就来好 ...

  9. 【带你重拾Redis】Redis数据结构及使用场景

    Redis数据结构 Redis有着非常丰富的数据结构,这些数据结构可以满足非常多的应用场景, 如果对这些数据结构有一个比较清晰的认知,使用Redis也会更加得心应手. Redis主要支持以下数据结构: ...

  10. redis 自减命令_Redis 实战 —— 04. Redis 数据结构常用命令简介

    字符串 P39 Redis 的字符串是一个有字节组成的序列,可以存储以下 3 种类型的值:字节串(byte string).整数.浮点数. 在需要的时候, Redis 会将整数转换成浮点数.整数的取值 ...

最新文章

  1. C++ 判断字符串是否为空
  2. tcpdump 抓取icmp数据包
  3. Python基本语法_函数_参数的多类型传值
  4. RPC与其实现方式概念笔记
  5. python排序问题_Python简单处理坐标排序问题示例
  6. 文件创建时间、访问时间、修改时间
  7. Myeclipse修改jdk版本流程
  8. CoreImage的使用及常见滤镜工具(一)
  9. Spring Boot @ServletComponentScan 扫描 @WebServlet、@WebFilter、@WebListener
  10. 对lua 实现面向对象的理解
  11. Strtus2入门简单框架搭建
  12. QT: QTableWidget 表格中按钮槽函数 获取表格该按钮所在的行号信息
  13. kmeans算法及python实现
  14. cpu_scale/max_freq_scale/cpu_capacity/cpu_capacity_orig的含义
  15. SPI Flash是什么?
  16. 中秋之际,我想给月亮做一个智能化改造
  17. Capacitor Plugin 实现
  18. 为什么越来越多的人选择海外服务器?
  19. spring cloud系列一:Cloud Native Applications
  20. 海驾学车过程全揭秘——第三篇:重要的法培

热门文章

  1. PPT:PowerPoint to Flash SDK:SWF
  2. 【物理学术竞赛】——绳上的球(再续)
  3. Foxmail是什么邮箱?
  4. 阿里云服务器apt install 出错怎么办?出现Package gdb is not available, but is referred to by another package怎么办
  5. Unity NavMesh寻路 A*(A star)分析及实例应用(一)
  6. 2019税改有哪些变化?什么是专项扣除?这些改动与你的工资息息相关!
  7. 搭建一个misskey实例
  8. 2G到5G蜂窝网络的定位技术简介
  9. 我在华为工作十年的感悟
  10. 在excel中如何筛选重复数据_高级筛选,让重复的数据记录无处可逃