redis存Object和String的性能比較

測試代碼

...
    @Test
    public void redisTest() {
        int size = 100;
        List<String> hobby = new ArrayList<>();
        hobby.add("swimming");
        hobby.add("sing");
        hobby.add("draw");
        hobby.add("football");
        TestClass t = new TestClass("Bob","Rocket",20,hobby);
        redisTemplate.setDefaultSerializer(new StringRedisSerializer());
        System.out.println("redis test(data size = " + size +"):");
        long startTime = 0l;
        startTime = System.currentTimeMillis();
        for(int i=0;i<size;i++){
            redisTemplate.opsForValue().set("Test:TestClass", t,3, TimeUnit.DAYS);
        }
        System.out.println("Object way to save: " + (double) (System.currentTimeMillis() - startTime) / 1000);
        startTime = System.currentTimeMillis();
        for(int i=0;i<size;i++){
            stringRedisTemplate.opsForValue().set("Test:TestClass", JSON.toJSONString(t),3, TimeUnit.DAYS);
        }
        System.out.println("String way to save: " + (double) (System.currentTimeMillis() - startTime) / 1000);
    }
...

測試數據

  1. 數據量爲100時
    數據量爲100時

  2. 數據量爲1000時
    數據量爲1000時

  3. 數據量爲10000時
    數據量爲10000時

  4. 數據量爲100000時
    數據量爲100000時

分析

Object存取時需要將對象進行序列化處理,而通過阿里的JSON工具將Object轉String這一過程要比Object序列化這一過程要快。所以整體的存取效率stringRedisTemplate高於redisTemplate。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章