app开发者平台在数字化时代的重要性与发展趋势解析
927
2022-12-21
SpringBoot Redis批量存取数据的操作
SpringBoot Redis批量存取数据
springboot中的redisTemplate封装了redis批处理数据的接口,我们使用redisTemplate可以直接进行批量数据的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
//批量get数据
List
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
for (String key : keys) {
connection.get(key.getBytes());
}
return null;
}
});
//批量set数据
redisTemplate.executePipelined(new RedisCallback
gZwltNW @Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
for (int i=0;i connection.set(keys.get(i).getBytes(),value.getBytes()); } return null; } }); } } 如果要设置 key 的过期时间,通过 setEx() 来做就可以了,过期时间单位是秒,相关代码如下: /** * 合同批量导入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 if (contracts == null || contracts.size() == 0) { return; } //批量set数据 redisUtil.getRedisTemplate().executePipelined((RedisCallback 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 List if (contracts == null || contracts.size() == 0) { return null; } List for (ContractBO contract : contracts) { connection.get((RedisPrefixConst.CONTRACT_PREFIX + contract.getBusinessCode() + RedisPrefixConst.UNDERLINE_SEPARATOR + contract.getContractNo()).getBytes()); } return null; }); return list; } SpringBoot对redis批量存取介绍到此结束。 redisTemplate批量写入数据 /** * 批量写入数据 * @param objectMap * @return */ public void saveMap(final Map final Map for(Map.Entry 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 @Override public Object doInRedis(RedisConnection connection) { try{ connection.mSet(byteMap); }catch (Exception ex){ log.error("redis批量写入数据异常:"+ex.getMessage(),ex); } return null; } }); }
connection.set(keys.get(i).getBytes(),value.getBytes());
}
return null;
}
});
}
}
如果要设置 key 的过期时间,通过 setEx() 来做就可以了,过期时间单位是秒,相关代码如下:
/**
* 合同批量导入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
if (contracts == null || contracts.size() == 0) {
return;
}
//批量set数据
redisUtil.getRedisTemplate().executePipelined((RedisCallback
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
List
if (contracts == null || contracts.size() == 0) {
return null;
}
List
for (ContractBO contract : contracts) {
connection.get((RedisPrefixConst.CONTRACT_PREFIX + contract.getBusinessCode() + RedisPrefixConst.UNDERLINE_SEPARATOR + contract.getContractNo()).getBytes());
}
return null;
});
return list;
}
SpringBoot对redis批量存取介绍到此结束。
redisTemplate批量写入数据
/**
* 批量写入数据
* @param objectMap
* @return
*/
public void saveMap(final Map
//序列化成字节数组
final Map
for(Map.Entry
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
@Override
public Object doInRedis(RedisConnection connection) {
try{
connection.mSet(byteMap);
}catch (Exception ex){
log.error("redis批量写入数据异常:"+ex.getMessage(),ex);
}
return null;
}
});
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~