2016-08-18 164 views
1

我有一個Spring Boot應用程序,需要將數百萬個鍵值對插入到Redis中。Redis MSET在Spring中使用TTL RedisTemplate

目前,我一次使用multiSet方法1,000個鍵值對。

@Autowired 
private final StringRedisTemplate template; 

... 

Map<String, String> keyValuePairs = new HashMap<>(); 
template.opsForValue().multiSet(keyValuePairs); 

但是我們還需要爲每一對設置一個TTL。似乎沒有辦法與multiSet做到這一點。有一種方法set,但這將不得不被稱爲數百萬次,因此可能效率不高。

// For each key value pair 
template.opsForValue().set(key, value, timeout, unit); 

有沒有人知道這樣做的方法或使用set高性能的方式?

感謝

回答

1

Pipelining應該能解決你的問題。你創建一個管道,用你想要執行的所有命令填充它,然後將它們批量發送到redis。使用SET與EX,你應該能夠一次發送10,000或更多。

+3

FWIW,這裏是[Spring Data Redis Pipelining]的鏈接(http://docs.spring.io/spring-data/redis/docs/current/reference/html/#pipeline)功能文檔 – mp911de

相關問題