SpringBoot Redis批量存取數(shù)據(jù)的操作
SpringBoot Redis批量存取數(shù)據(jù)
springboot中的redisTemplate封裝了redis批處理數(shù)據(jù)的接口,我們使用redisTemplate可以直接進行批量數(shù)據(jù)的get和set。
package com.huateng.applacation.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @program: applacation
* @description:
* @author: daiwenlong
* @create: 2019-01-24 13:26
**/
@Component
public class RedisService {
@Autowired
@Qualifier("stringRedisTemplate")
private StringRedisTemplate redisTemplate;
public void insertKey(List<String> keys, String value){
//批量get數(shù)據(jù)
List<Object> list = redisTemplate.executePipelined(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
for (String key : keys) {
connection.get(key.getBytes());
}
return null;
}
});
//批量set數(shù)據(jù)
redisTemplate.executePipelined(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
for (int i=0;i<keys.size();i++) {
connection.set(keys.get(i).getBytes(),value.getBytes());
}
return null;
}
});
}
}
如果要設(shè)置 key 的過期時間,通過 setEx() 來做就可以了,過期時間單位是秒,相關(guān)代碼如下:
/**
* 合同批量導(dǎo)入redis
*
* @param contractBOList
* @param expire
* @return com.openailab.oascloud.common.model.ResponseResult
* @author zxzhang
* @date 2019/10/14
*/
@Override
public void contractBatchSetRedis(String contractBOList, long expire) {
List<ContractBO> contracts = JSONObject.parseArray(contractBOList, ContractBO.class);
if (contracts == null || contracts.size() == 0) {
return;
}
//批量set數(shù)據(jù)
redisUtil.getRedisTemplate().executePipelined((RedisCallback<String>) connection -> {
for (ContractBO contract : contracts) {
connection.setEx((RedisPrefixConst.CONTRACT_PREFIX + contract.getBusinessCode() + RedisPrefixConst.UNDERLINE_SEPARATOR + contract.getContractNo()).getBytes(), expire, JSONObject.toJSONString(contract).getBytes());
}
return null;
});
}
/**
* 合同批量獲取redis
*
* @param contractBOList
* @return java.lang.String
* @author zxzhang
* @date 2019/10/14
*/
@Override
public List<Object> contractBatchGetRedis(String contractBOList) {
List<ContractBO> contracts = JSONObject.parseArray(contractBOList, ContractBO.class);
if (contracts == null || contracts.size() == 0) {
return null;
}
List<Object> list = redisUtil.getRedisTemplate().executePipelined((RedisCallback<String>) connection -> {
for (ContractBO contract : contracts) {
connection.get((RedisPrefixConst.CONTRACT_PREFIX + contract.getBusinessCode() + RedisPrefixConst.UNDERLINE_SEPARATOR + contract.getContractNo()).getBytes());
}
return null;
});
return list;
}
SpringBoot對redis批量存取介紹到此結(jié)束。
redisTemplate批量寫入數(shù)據(jù)
/**
* 批量寫入數(shù)據(jù)
* @param objectMap
* @return
*/
public void saveMap(final Map<String,Object> objectMap) {
//序列化成字節(jié)數(shù)組
final Map<byte[],byte[]> byteMap=new HashMap<>();
for(Map.Entry<String,Object> objectEntry:objectMap.entrySet()){
String key=objectEntry.getKey();
final byte[] keyBytes = redisTemplate.getStringSerializer().serialize(key);
Object value=objectEntry.getValue();
final byte[] valueBytes =SerializeUtil.serialize(value);
byteMap.put(keyBytes,valueBytes);
}
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) {
try{
connection.mSet(byteMap);
}catch (Exception ex){
log.error("redis批量寫入數(shù)據(jù)異常:"+ex.getMessage(),ex);
}
return null;
}
});
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何在 Java 中實現(xiàn)一個 redis 緩存服務(wù)
為什么要使用緩存?說到底是為了提高系統(tǒng)的運行速度。將用戶頻繁訪問的內(nèi)容存放在離用戶最近,訪問速度最快的地方,提高用戶的響應(yīng)速度。下面我們來一起深入學(xué)習(xí)一下吧2019-06-06
Springboot集成JSR303參數(shù)校驗的方法實現(xiàn)
這篇文章主要介紹了Springboot集成JSR303參數(shù)校驗的方法實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
詳解Servlet入門級設(shè)置(超詳細(xì) IDEA2020版)
這篇文章主要介紹了詳解Servlet入門級設(shè)置(超詳細(xì) IDEA2020版),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Java 實戰(zhàn)項目之精美物流管理系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+Vue+maven+Mysql實現(xiàn)一個精美的物流管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11

