• java-redis集合数据操作示例(三)


     redis系列博文,redis连接管理类的代码请跳转查看《java-redis字符类数据操作示例(一)》。

    一、集合类型缓存测试类

    public class SetTest {
        /**
         * 主测试方案
         */
        @Test
        public void test() {
            RedisUtil.instance.run(conn -> oper(conn));
            Assert.assertTrue(true);
        }
        /**
         * 测试用的key
         */
        private final String _key = "simm-set";
        /**
         * 字符串操作
         * 
         * @param conn
         */
        private void oper(ShardedJedis conn) {
            System.out.println(MessageFormat.format("key[{0}]存在:{1} ", _key,conn.exists(_key)));
            // 集合数据初始化
            String[] arr= "AA,AB,AC,AD,AE,AF,AG,BA,BB,BC,BD,BE,BF,BG".split(",");
            conn.sadd(_key, arr);
            print(conn);
            //1.查询元素
            //指定个数 count 在redis服务中默认值为10 
            ScanResult<String> result = conn.sscan(_key, "0",new ScanParams().match("*").count(2));
            String cursor = result.getStringCursor();
            System.out.println(MessageFormat.format("游标位置:{0}", cursor));
            print(result.getResult());
            //匹配内容
            result = conn.sscan(_key, "0",new ScanParams().match("A*"));
            print(result.getResult());
            result = conn.sscan(_key, "0",new ScanParams().match("A*").count(100));
            print(result.getResult());
            //2.集合元素删除方法 
            //2.1.移除
            Long effected = conn.srem(_key, "AE","BG","HI");//移除CGH。
            System.out.println(MessageFormat.format("成功移除{0}个元素", effected));
            print(conn);
            //2.2.从集合中弹出一个元素
            String pop = conn.spop(_key);
            System.out.println(MessageFormat.format("POP: {0}", pop));
            print(conn);
            //3.判断元素是否存在
            System.out.println(MessageFormat.format("A是集合中的元素? {0}", conn.sismember(_key, "AA")));
            System.out.println(MessageFormat.format("J是集合中的元素? {0}", conn.sismember(_key, "JQ")));
            
            conn.expire(_key, 1); //设置改key值1s后过期,过期后redis自动清理该缓存
            System.out.println(MessageFormat.format("key[{0}]存在:{1} ", _key,conn.exists(_key)));
        }
        
        private void print(List<String> list){
            System.out.print(MessageFormat.format("scan输出,长度[{1}]:", _key,list.size()));
            for (String str : list) {
                System.out.print(MessageFormat.format("{0} ", str));
            }
            System.out.println();
        }
        
        private void print(ShardedJedis conn){
            System.out.print(MessageFormat.format("{0}元素输出,长度[{1}]:", _key,conn.scard(_key)));
            Set<String> list = conn.smembers(_key);
            for (String str : list) {
                System.out.print(MessageFormat.format("{0} ", str));
            }
            System.out.println();
        }
    }

    二、结果输出

      

    三、关于sscan命令

      

      match参数用于过滤,count参数用于限制一次迭代返回的集合数目。这个值默认是10。我测试了两种情况,集合元素个数少于10以及大于10。后面给出测试的结果。测试过这个sscan方法后,有点懵逼,网上查找别人的分享,好像也是懵逼的多。显示设置一次迭代的个数,是否能够生效,还受到set元素总个数是否超过10的影响。这块的实现让人费解。就测试后来看,当数据量不是很大,想正确的一次查询所有匹配项的话,请设置一个较大的count参数,这样能保证数据量从少到多的过程中都不会出问题。

      1、初始化集合元素为7个

      // 集合数据初始化
            String[] arr= "AA,AB,AC,AD,AE,AF,AG".split(",");
            conn.sadd(_key, arr);
            print(conn);
            //1.查询元素
            //指定个数 count 在redis服务中默认值为10 
            ScanResult<String> result = conn.sscan(_key, "0",new ScanParams().match("A*").count(2));
            String cursor = result.getStringCursor();
            System.out.println(MessageFormat.format("游标位置:{0}", cursor));
            print(result.getResult());
            //匹配内容
            result = conn.sscan(_key, "0",new ScanParams().match("A*"));
            print(result.getResult());
            result = conn.sscan(_key, "0",new ScanParams().match("A*").count(100));
            print(result.getResult());

        测试3种场景,情况如下

      •  match("A*").count(2):查询以A开头的元素,限定一次迭代2个元素。结果返回了3个数据。设置一次迭代2个元素,结果返回元素还大于设置的迭代量,卧槽,蛋疼的结果;
      •  match("A*"):不显示指定迭代个数,结果返回了所有的匹配项;
      •  match("A*").count(100):指定一次迭代上限为100,结果也返回了所有的匹配项。

      2、初始化集合元素为14个

            // 集合数据初始化
            String[] arr= "AA,AB,AC,AD,AE,AF,AG,BA,BB,BC,BD,BE,BF,BG".split(",");
            conn.sadd(_key, arr);
            print(conn);
            //1.查询元素
            //指定个数 count 在redis服务中默认值为10 
            ScanResult<String> result = conn.sscan(_key, "0",new ScanParams().match("A*").count(2));
            String cursor = result.getStringCursor();
            System.out.println(MessageFormat.format("游标位置:{0}", cursor));
            print(result.getResult());
            //匹配内容
            result = conn.sscan(_key, "0",new ScanParams().match("A*"));
            print(result.getResult());
            result = conn.sscan(_key, "0",new ScanParams().match("A*").count(100));
            print(result.getResult());

        测试3种场景,情况如下

      •  match("A*").count(2):查询以A开头的元素,限定一次迭代2个元素。结果返回为空。可以理解为这一次的2个迭代元素均不符合过滤条件;
      •  match("A*"):不显示指定迭代个数,结果返回了4个匹配项。这样看默认的10个一批迭代的设置起作用了;
      •  match("A*").count(100):指定一次迭代上限为100,结果返回了所有的匹配项。看来想一次正确返回所有匹配项,只能直接设置一个较大的迭代值了。
  • 相关阅读:
    [Angular 9] Built-in template syntax $any
    [Angular 9] Improved Dependency Injection with the new providedIn scopes 'any' and 'platform'
    [Angular 9 Unit testing] Stronger typing for dependency injection in tests
    [Angular] Preserve the current route’s query parameters when navigating with the Angular Router
    [Angular] Do relative routing inside component
    [Typescript] Make your optional fields required in TypeScript
    [Typescript] Exclude Properties from a Type in TypeScript
    [Javascript] Hide Properties from Showing Up in "for ... in" Loops in JavaScript
    [Debug] Set and remove DOM breakpoints
    【职业素养】4种让你显得没教养的做法
  • 原文地址:https://www.cnblogs.com/MrSi/p/8479632.html
Copyright © 2020-2023  润新知