小言_互联网的博客

SpringBoot实战(十七):Redis Pipeline 轻松实现百倍性能提升(续)

350人阅读  评论(0)

前言

最近在做业务的时候,需要批量操作Redis,虽然Redis的速度非常快,但是for循环操作Redis还是会有问题,在之前的基础上又对批量操作Redis进行了汇总;

批量操作Redis:

  • 批量Set
  • 批量Get
  • 批量Set且设置过期时间
  • 批量Delete

批量操作效果&技术实现方案

代码实现

  • 完整代码(GitHub,欢迎大家Star,Fork,Watch)

    https://github.com/dangnianchuntian/springboot

  • 主要代码展示

    • Controller
    /*
     * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved.
     * 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升
     * 类名称:BatchRedisController.java
     * 创建人:张晗
     * 联系方式:zhanghan_java@163.com
     * 开源地址: https://github.com/dangnianchuntian/springboot
     * 博客地址: https://zhanghan.blog.csdn.net
     */
    
    package com.zhanghan.zhredisbatch.controller;
    
    import com.zhanghan.zhredisbatch.controller.request.ListMultiGetRequest;
    import com.zhanghan.zhredisbatch.controller.request.PostMultiDeleteRequest;
    import com.zhanghan.zhredisbatch.controller.request.PostMultiSetRequest;
    import com.zhanghan.zhredisbatch.service.BatchRedisService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.validation.annotation.Validated;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class BatchRedisController {
    
        @Autowired
        private BatchRedisService batchRedisService;
    
        /**
         * Redis批量Set
         */
        @RequestMapping(value = "/post/multi/set", method = RequestMethod.POST)
        public Object postMultiSet(@RequestBody @Validated PostMultiSetRequest postMultiSetRequest) {
            return batchRedisService.postMultiSet(postMultiSetRequest);
        }
    
        /**
         * Redis批量Get
         */
        @RequestMapping(value = "/list/multi/get", method = RequestMethod.POST)
        public Object listMultiGet(@RequestBody @Validated ListMultiGetRequest listMultiGetRequest) {
            return batchRedisService.listMultiGet(listMultiGetRequest);
        }
    
        /**
         * Redis批量Set且设置失效时间
         */
        @RequestMapping(value = "/post/multi/set/expire", method = RequestMethod.POST)
        public Object postMultiSetAndExpire(@RequestBody @Validated PostMultiSetRequest postMultiSetRequest) {
            return batchRedisService.postMultiSetAndExpire(postMultiSetRequest);
        }
    
        /**
         * Redis批量Delete
         */
        @RequestMapping(value = "/post/multi/delete", method = RequestMethod.POST)
        public Object postMultiDelete(@RequestBody @Validated PostMultiDeleteRequest postMultiDeleteRequest) {
            return batchRedisService.postMultiDelete(postMultiDeleteRequest);
        }
    
    }
    
  • service

    /*
     * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved.
     * 项目名称:Spring Boot实战:Redis批量操作轻松实现百倍性能提升
     * 类名称:BatchRedisServiceImpl.java
     * 创建人:张晗
     * 联系方式:zhanghan_java@163.com
     * 开源地址: https://github.com/dangnianchuntian/springboot
     * 博客地址: https://zhanghan.blog.csdn.net
     */
    
    package com.zhanghan.zhredisbatch.service.impl;
    
    import com.zhanghan.zhredisbatch.controller.request.ListMultiGetRequest;
    import com.zhanghan.zhredisbatch.controller.request.PostMultiDeleteRequest;
    import com.zhanghan.zhredisbatch.controller.request.PostMultiSetRequest;
    import com.zhanghan.zhredisbatch.controller.response.ListMultiGetResponse;
    import com.zhanghan.zhredisbatch.dto.BatchRedisDto;
    import com.zhanghan.zhredisbatch.service.BatchRedisService;
    import com.zhanghan.zhredisbatch.util.wrapper.WrapMapper;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.dao.DataAccessException;
    import org.springframework.data.redis.connection.RedisConnection;
    import org.springframework.data.redis.connection.RedisStringCommands;
    import org.springframework.data.redis.core.RedisCallback;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.types.Expiration;
    import org.springframework.stereotype.Service;
    import org.springframework.util.StringUtils;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    
    @Service
    public class BatchRedisServiceImpl implements BatchRedisService {
    
        private static Logger logger = LoggerFactory.getLogger(BatchRedisServiceImpl.class);
    
        @Autowired
        private RedisTemplate<String, String> strRedisTemplate;
    
        /**
         * Redis批量Set
         */
        @Override
        public Object postMultiSet(PostMultiSetRequest postMultiSetRequest) {
            Map<String, String> batchSetMap = new HashMap<>();
            List<BatchRedisDto> batchRedisDtoList = postMultiSetRequest.getBatchRedisDtoList();
            for (BatchRedisDto batchRedisDto : batchRedisDtoList) {
                batchSetMap.put(batchRedisDto.getRedisKey(), batchRedisDto.getRedisValue());
            }
    
            strRedisTemplate.opsForValue().multiSet(batchSetMap);
            return WrapMapper.ok();
        }
    
        /**
         * Redis批量Get
         */
        @Override
        public Object listMultiGet(ListMultiGetRequest listMultiGetRequest) {
            List<String> keyList = listMultiGetRequest.getKeyList();
            List<String> valueList = strRedisTemplate.opsForValue().multiGet(keyList);
            List<BatchRedisDto> batchRedisDtoList = new ArrayList<>();
            ListMultiGetResponse listMultiGetResponse = new ListMultiGetResponse();
    
            for (int i = 0; i < valueList.size(); i++) {
                String value = valueList.get(i);
                if (StringUtils.isEmpty(value)) {
                    continue;
                }
                BatchRedisDto batchRedisDto = new BatchRedisDto();
                batchRedisDto.setRedisKey(keyList.get(i));
                batchRedisDto.setRedisValue(value);
                batchRedisDtoList.add(batchRedisDto);
            }
    
            listMultiGetResponse.setBatchRedisDtoList(batchRedisDtoList);
    
            return WrapMapper.ok(listMultiGetResponse);
        }
    
        /**
         * Redis批量Set且设置失效时间
         */
        @Override
        public Object postMultiSetAndExpire(PostMultiSetRequest postMultiSetRequest) {
            List<BatchRedisDto> batchRedisDtoList = postMultiSetRequest.getBatchRedisDtoList();
            strRedisTemplate.executePipelined(new RedisCallback<String>() {
                @Override
                public String doInRedis(RedisConnection connection) throws DataAccessException {
                    for (BatchRedisDto batchRedisDto : batchRedisDtoList) {
                        String key = batchRedisDto.getRedisKey();
                        String value = batchRedisDto.getRedisValue();
                        connection.set(key.getBytes(), value.getBytes(), Expiration.from(1, TimeUnit.DAYS), RedisStringCommands.SetOption.UPSERT);
                    }
                    return null;
                }
            });
    
            return WrapMapper.ok();
        }
    
        /**
         * Redis批量Delete
         */
        @Override
        public Object postMultiDelete(PostMultiDeleteRequest postMultiDeleteRequest) {
            strRedisTemplate.delete(postMultiDeleteRequest.getKeyList());
            return WrapMapper.ok();
        }
    
    }
    

测试

  • 批量Set

    • 进行请求

    • 查看Redis结果

  • 批量Get

    • 进行请求
  • 批量Set且设置失效时间

    • 进行请求

    • 查看结果

  • 批量Delete

    • 进行请求

    • 查看结果

总结

  • 亮点:批量操作Redis,轻松实现百倍性能提升
  • 注意点:批量Set ,redisTemplate的multiSet不支持设置超时时间,需要用executePipelined去实现

转载:https://blog.csdn.net/u012829124/article/details/108305904
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场