Redis数据结构--集合Set

  Redis 的集合不是一个线性结构,而是一个哈希表结构,内部会根据 hash 分子来存储和查找数据,理论上一个集合可以存储2的32次方减一(约42亿)个元素。

  因为采用哈希表结构,所以对于 Redis 集合的插入、删除和查找的复杂度都是 0(1),需要注意 3 点

  对于集合而言,每一个元素都是不能重复的,当插入相同记录的时候都会失败
  集合是无序的
  集合的每一个元素都是 String 数据结构类型

       

        Redis 的集合可以对于不同的集合进行操作,比如求出两个或者以上集合的交集、 差集和并集等

交集、并集和差集保存命令的用法

package com.smart;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisListCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class RedisStringDemo {

    public static void main(String[] args) throws UnsupportedEncodingException {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:redis/spring-redis.xml");

        // redisTemplate.opsForValue()所返回的对象可以操作简单的键值对,可以是字符串,也可以是对象,具体依据所配置的序列化方案
        // 在spring-redis-string.xml中key和value是指定的 stringRedisSerializer
        RedisTemplate redisTemplate= (RedisTemplate<String, String>) context.getBean("redisTemplate");

        flushDb(redisTemplate);

        String SET1="set1";
        String SET2="set2";

        redisTemplate.boundSetOps(SET1).add("v1","v2","v3","v4","v5");
        redisTemplate.boundSetOps(SET2).add("v2","v4","v6","v8");

        System.out.println(SET1+"的长度是"+redisTemplate.opsForSet().size(SET1));
        System.out.println(SET1+"的长度是"+redisTemplate.opsForSet().size(SET2));

        Set<String> set = redisTemplate.opsForSet().difference(SET1, SET2);
        scanSet(set);

        set = redisTemplate.opsForSet().intersect(SET1, SET2);
        scanSet(set);

        Boolean exist = redisTemplate.opsForSet().isMember(SET1, "v1");
        System.out.println(SET1+"中存在v1:"+exist);

        set=redisTemplate.opsForSet().members(SET1);
        scanSet(set);

        // 从集合中随机弹出一个元素,集合中会删除该元素
        String randomValue = (String) redisTemplate.opsForSet().pop(SET2);
        System.out.println(SET2 + "中弹出的随机元素为" + randomValue);
        System.out.println(SET2 + "的长度为:" + redisTemplate.opsForSet().size(SET2));

        // 随机获取一个集合的元素 ,该元素仍然在集合中
        randomValue = (String) redisTemplate.opsForSet().randomMember(SET1);
        System.out.println(randomValue);
        System.out.println(SET1 + "的长度为:" + redisTemplate.opsForSet().size(SET1));

        // 随机获取集合中 的4 个元素
        List<String> list = redisTemplate.opsForSet().randomMembers(SET1, 4);
        for (String string : list) {
            System.out.println(string);
        }

        redisTemplate.opsForSet().remove(SET1, "v1", "v2");
        System.out.println(SET1+"的长度"+redisTemplate.opsForSet().size(SET1));

        set = redisTemplate.opsForSet().union(SET1, SET2);
        scanSet(set);

        redisTemplate.opsForSet().differenceAndStore(SET1,SET2,"diff_set");
        scanSet(redisTemplate.opsForSet().members("diff_set"));

        redisTemplate.opsForSet().intersectAndStore(SET1, SET2, "inter_set");
        scanSet(redisTemplate.opsForSet().members("inter_set"));

        redisTemplate.opsForSet().unionAndStore(SET1,SET2,"union_set");
        scanSet(redisTemplate.opsForSet().members("union_set"));
    }

    private static void scanSet(Set<String> set) {
        for (String value : set) {
            System.out.println(value);
        }
        System.out.println("----------------");
    }

    public static String flushDb(RedisTemplate<String,String> redisTemplate){
      return redisTemplate.execute(new RedisCallback<String>() {
          @Override
          public String doInRedis(RedisConnection connection) throws DataAccessException {
              connection.flushDb();
              return "ok";
          }
      });
    }

}

 

  

posted on 2019-02-25 21:35  溪水静幽  阅读(224)  评论(0)    收藏  举报