redisTemplate批量写入数据

redisTemplate批量写入数据
强烈推介IDEA2021.1.3破解激活,IntelliJ IDEA 注册码,2021.1.3IDEA 激活码  

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说redisTemplate批量写入数据,希望能够帮助大家进步!!!

 /**
     * 批量写入数据
     * @param objectMap
     * @return
     */
    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;
            }
        });
    }

 

本文来源huayang183,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/18015

发表评论