redis的map和zset写入50万个数据的测试结果

需求描述

某个项目中需要使用缓存工具缓存55万个分类信息,然后对每个分类进行计数统计,调研了redis的hashmap和zset的性能,最终决定使用zset。原因如下:

  1. hashmap和zset都写入55万个key,zset约要34秒,hashmap约要39秒
  2. hashmap读取55万个key的耗时3.3秒,zset读取55万个key约耗时1.4秒
  3. hashmap存储的key是无序的,zset存储的value是有序的

代码如下

public void TestReadRedis() throws Exception {
        String sRedisHost = "127.0.0.1";
        int nRedisPort = 6379;
        int REDIS_DBIDX = 0;
        int REDIS_POOLSIZE = 20;
        RedisApi redisApi = new RedisApi(sRedisHost, nRedisPort, REDIS_DBIDX, REDIS_POOLSIZE);

        String sRedisKey = "my_map_key";
        HashMap<String, String> mapKeyValue = new HashMap<>();
        long lTime98 = System.currentTimeMillis();
        for (int i = 0; i <= 550000; i++) {
            String sMapKey = String.format("key_%s", i);
            mapKeyValue.put(sMapKey, String.valueOf(i));
            if (i % 10000 == 0) {
                //System.out.println("map i=" + i);
                redisApi.hmset(sRedisKey, mapKeyValue, -1);
                mapKeyValue.clear();
            }
        }
        if (mapKeyValue.size() > 0) {
            System.out.println(mapKeyValue.size());
            redisApi.hmset(sRedisKey, mapKeyValue, -1);
        }
        long lTime99 = System.currentTimeMillis();
        System.out.println("写入map耗时:" + (lTime99 - lTime98));

        long lTime1 = System.currentTimeMillis();
        Map<String, String> hMapGetValue = redisApi.hgetAll(sRedisKey);
        long lTime2 = System.currentTimeMillis();

        System.out.println("读取map用时:" + (lTime2 - lTime1) + ",size=" + hMapGetValue.size());

        //测试zset的效果
        String sRedisSetKEey = "my_set_key";
        long lTime3 = System.currentTimeMillis();
        Map<String, Double> hScoreMembers = new HashMap<>();
        Double dScore = Double.valueOf(1);
        for (int i = 0; i <= 550000; i++) {
            String sMapKey = String.format("key_%s", i);
            hScoreMembers.put(sMapKey, dScore);
            if (i % 10000 == 0) {
                //System.out.println("zset i=" + i);
                redisApi.zadd(sRedisSetKEey, hScoreMembers, -1);
                hScoreMembers.clear();
            }
        }
        if (hScoreMembers.size() > 0) {
            System.out.println("hScoreMembers.size=" + hScoreMembers.size());
            redisApi.zadd(sRedisSetKEey, hScoreMembers, -1);
        }
        long lTime4 = System.currentTimeMillis();
        System.out.println("写入zset耗时: " + (lTime4 - lTime3));


        long lTime5 = System.currentTimeMillis();
        Set<String> setGetValue = redisApi.zrange(sRedisSetKEey, 0, -1);
        long lTime6 = System.currentTimeMillis();
        System.out.println("读取zset用时:" + (lTime6 - lTime5) + ",size=" + setGetValue.size());
    }

结果如下

写入map耗时:39497 
读取map用时:3343,size=550001
写入zset耗时: 34992
读取zset用时:1429,size=550001

你可能感兴趣的:(java,redis)