这篇文章主要介绍“SpringBoot Redis怎么批量存取数据”,在日常操作中,相信很多人在SpringBoot Redis怎么批量存取数据问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”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; @Componentpublic class RedisService { @Autowired @Qualifier("stringRedisTemplate") private StringRedisTemplate redisTemplate; public void insertKey(List<String> keys, String value){ //批量get数据 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数据 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; } }); }}
如果要设置 key 的过期时间,通过 setEx() 来做就可以了,过期时间单位是秒,相关代码如下:
@Overridepublic void contractBatchSetRedis(String contractBOList, long expire) { List<ContractBO> contracts = JSONObject.parseArray(contractBOList, ContractBO.class); if (contracts == null || contracts.size() == 0) { return; } //批量set数据 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; });} @Overridepublic 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批量存取介绍到此结束。
redisTemplate批量写入数据
public void saveMap(final Map<String,Object> objectMap) { //序列化成字节数组 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批量写入数据异常:"+ex.getMessage(),ex); } return null; } }); }
到此,关于“SpringBoot Redis怎么批量存取数据”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!