RedisTemplate批量操作工具類性能測(cè)試
RedisTemplate批量添加操作教程
RedisTemplate批量添加操作教程,利用pipeline批量操作;multiSet()批量操作;for循環(huán)批量操作
一、使用pipeline的好處
了解redis的小伙伴都知道,redis是一個(gè)高性能的單線程的key-value數(shù)據(jù)庫(kù)。它的執(zhí)行過(guò)程為:
(1)發(fā)送命令-〉(2)命令排隊(duì)-〉(3)命令執(zhí)行-〉(4)返回結(jié)果
如果我們使用redis進(jìn)行批量插入數(shù)據(jù),正常情況下相當(dāng)于將以上四個(gè)步驟批量執(zhí)行N次。(1)和(4)稱為Round Trip Time(RTT,往返時(shí)間)。在一條簡(jiǎn)單指令中,往往(1)(4)步驟之和大過(guò)于(2)(3)步驟之和,如何進(jìn)行優(yōu)化?Redis提供了pipeline管道機(jī)制,它能將一組Redis命令進(jìn)行組裝,通過(guò)一次RTT傳輸給Redis,并將這組Redis命令的執(zhí)行結(jié)果按順序返回給客戶端。
優(yōu)缺點(diǎn)總結(jié):
- 1、性能對(duì)比:multiSet()>pipeline管道>普通for循環(huán)set
 - 2、擴(kuò)展性強(qiáng),可以支持設(shè)置失效時(shí)間。multiSet()不支持失效時(shí)間的設(shè)置
 
二、批量操作的工具類
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
/**
 * @author: huangyibo
 * @Date: 2022/6/23 16:15
 * @Description:
 */
@Component
public class BatchRunRedisUtil {
    @Autowired
    private RedisTemplate<String, Object> stringRedisTemplate;
    /**
     * 批量添加
     * @param map
     */
    public void batchSet(Map<String, String> map) {
        stringRedisTemplate.opsForValue().multiSet(map);
    }
    /**
     * 批量添加 并且設(shè)置失效時(shí)間
     * @param map
     * @param seconds
     */
    public void batchSetOrExpire(Map<String, String> map, Long seconds) {
        RedisSerializer<String> serializer = stringRedisTemplate.getStringSerializer();
        stringRedisTemplate.executePipelined(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                map.forEach((key, value) -> {
                    connection.set(serializer.serialize(key), serializer.serialize(value), Expiration.seconds(seconds), RedisStringCommands.SetOption.UPSERT);
                });
                return null;
            }
        }, serializer);
    }
    /**
     * 批量獲取
     * @param list
     * @return
     */
    public List<Object> batchGet(List<String> list) {
        List<Object> objectList = stringRedisTemplate.opsForValue().multiGet(list);
        return objectList;
    }
    /**
     * Redis批量Delete
     * @param list
     */
    public void batchDelete(List<String> list) {
        stringRedisTemplate.delete(list);
    }
}三、性能測(cè)試
通過(guò)for循環(huán)來(lái)向redis插入數(shù)據(jù),通過(guò)pipeline插入數(shù)據(jù),通過(guò)使用redisTemplate.opsForValue().multiSet(map)插入數(shù)據(jù)查看執(zhí)行時(shí)間。
import com.demo.util.BatchRunRedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.HashMap;
import java.util.Map;
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    @Autowired
    private RedisTemplate<String, Object> stringRedisTemplate;
    @Autowired
    private BatchRunRedisUtil batchRunRedisUtil;
    @Override
    public void run(String... args) throws Exception {
        //for循環(huán)批量添加
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            stringRedisTemplate.opsForValue().set("aaa" + i, "a", 60);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("普通set消耗" + (endTime - startTime) + "毫秒");
        //利用pipeline批量操作
        long startTime2 = System.currentTimeMillis();
        Map map = new HashMap(100000);
        for (int i = 0; i < 100000; i++) {
            map.put("bbb" + i, "b");
        }
        batchRunRedisUtil.batchSetOrExpire(map, 60l);
        long endTime2 = System.currentTimeMillis();
        System.out.println("管道set消耗" + (endTime2 - startTime2) + "毫秒");
        //multiSet()批量操作
        long startTime3 = System.currentTimeMillis();
        Map map2 = new HashMap(100000);
        for (int i = 0; i < 100000; i++) {
            map2.put("ccc" + i, "b");
        }
        batchRunRedisUtil.batchSet(map2);
        long endTime3 = System.currentTimeMillis();
        System.out.println("批量set消耗" + (endTime3 - startTime3) + "毫秒");
    }
}在本機(jī)分別執(zhí)行了三次結(jié)果:
普通set消耗9010毫秒
管道set消耗1606毫秒
批量set消耗18毫秒
普通set消耗8228毫秒
管道set消耗1059毫秒
批量set消耗14毫秒
普通set消耗8365毫秒
管道set消耗1092毫秒
批量set消耗13毫秒
通過(guò)比較發(fā)現(xiàn),逐條執(zhí)行時(shí)間是pipeline執(zhí)行平均時(shí)間的8倍!這是在本機(jī)測(cè)試的結(jié)果,理論上,客戶端與服務(wù)端的網(wǎng)絡(luò)延遲越大,性能體能越明顯。
當(dāng)然,pipeline性能提升雖然明顯,但是每次管道里命令個(gè)數(shù)太多的話,也會(huì)造成客戶端響應(yīng)時(shí)間變久,網(wǎng)絡(luò)傳輸阻塞。最好還是根據(jù)業(yè)務(wù)情況,將大的pipeline拆分成多個(gè)小的pipeline來(lái)執(zhí)行。
如果不用設(shè)置失效時(shí)間的話最好使用redisTemplate.opsForValue().multiSet(map)方法來(lái)添加
以上就是RedisTemplate批量操作工具類性能測(cè)試的詳細(xì)內(nèi)容,更多關(guān)于RedisTemplate批量操作的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- redis redistemplate序列化對(duì)象配置方式
 - 配置redis的序列化,注入RedisTemplate方式
 - 解讀RedisTemplate的各種操作(set、hash、list、string)
 - 使用redisTemplate的scan方式刪除批量key問(wèn)題
 - Redis Template使用詳解示例教程
 - redistemplate下opsForHash操作示例
 - 解決redisTemplate向redis中插入String類型數(shù)據(jù)時(shí)出現(xiàn)亂碼問(wèn)題
 - 解決RedisTemplate存儲(chǔ)至緩存數(shù)據(jù)出現(xiàn)亂碼的情況
 - reids自定義RedisTemplate以及亂碼問(wèn)題解決
 
相關(guān)文章
 redis哈希和集合_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了redis哈希和集合的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
 redis中opsForList().range()的使用方法詳解
這篇文章主要給大家介紹了關(guān)于redis中opsForList().range()的使用方法,文中通過(guò)實(shí)例代碼以及圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用redis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-03-03
 reids自定義RedisTemplate以及亂碼問(wèn)題解決
本文主要介紹了reids自定義RedisTemplate以及亂碼問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04

