Redis项目应用场景与实例(三):队列(List)_我的博客-CSDN博客_redis运用实例

文章目录

一、背景

在前两篇文章

strings

hashes

lists

sets

sorted sets

封锁一个IP地址

存储用户信息

模拟队列

自动排重

以某一个条件为权重,进行排序

二、项目需求

  • 创建SpringBoot上传文件WebApi服务接口;
  • 通过Redis缓存队列记录最新10笔用户上传文件的信息。
    缓存队列时序图

三、环境配置

  • 开发环境:
  • JDK 1.8
  • SpringBoot 2.2.5
  • JPA
  • Spring Security
  • Mysql 8.0
  • Redis Server 3.2.1
  • Redis Desktop Manager
  • 微信开发者工具

– java后端项目结构

在这里插入图片描述

四、项目代码

4.1 Redis工具类增加队列操作方法
/**
 * Redis工具类
 *
 * @author zhuhuix
 * @date 2020-06-15
 * @date 2020-06-17 增加队列操作方法
 */
@Component
@AllArgsConstructor
public class RedisUtils {
    private RedisTemplate<Object, Object> redisTemplate;
    
    ...
    
    /**
     * 入队
     *
     * @param key   队列键值
     * @param value 元素
     * @return 添加数量
     */
    public long leftPush(String key, Object value) {
        return redisTemplate.opsForList().leftPush(key, value);
    }

    /**
     * 向队列头部添加全部集合元素
     *
     * @param key  队列键值
     * @param list 集合
     * @return 返回添加的数量
     */
    public long leftPushAll(String key, List<Object> list) {
        return redisTemplate.opsForList().leftPushAll(key, list);
    }

    /**
     * 统计队列中所有元素数量
     *
     * @param key 队列键值
     * @return 队列中元素数量
     */
    public long size(String key){
        return redisTemplate.opsForList().size(key);
    }

    /**
     * 返回队列中从起始位置到结束位置的集合元素
     *
     * @param key   队列键值
     * @param start 起始位置
     * @param end   结束位置
     * @return 返回集合
     */
    public List<Object> range(String key, long start, long end) {
        return redisTemplate.opsForList().range(key, start, end);
    }

    /**
     * 出队
     *
     * @param key 队列键值
     * @return 元素
     */
    public Object rightPop(String key){
        return redisTemplate.opsForList().rightPop(key);
    }

    /**
     * 弹出队列最新元素
     *
     * @param key 队列键值
     * @return 元素
     */
    public Object leftPop(String key) {
        return redisTemplate.opsForList().leftPop(key);
    }

    /**
     * 删除队列所有元素
     *
     * @param key 队列键值
     */
    public void deleteAll(String key){
        redisTemplate.opsForList().trim(key,0,0);
        redisTemplate.opsForList().leftPop(key);
    }
   ...
}

4.2 图片上传服务增加Redis队列
/**
 * 微信小程序CRM实现类
 *
 * @author zhuhuix
 * @date 2020-04-20
 * @date 2020-06-17 将最新的上传信息推入Redis队列
 */
@Slf4j
@AllArgsConstructor
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class WxMiniCrmImpl implements WxMiniCrm {

    ...

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<CrmIndex> uploadCrmIndex(String json, String openId, String realName, MultipartFile multipartFile) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(json);

            String createTime = jsonObject.getString("create");
            String employeeCode = jsonObject.getString("employeeCode");
            String customerCode = jsonObject.getString("customerCode");
            String customerName = jsonObject.getString("customerName");
            String type = jsonObject.getString("type");

            if (StringUtils.isEmpty(createTime) || StringUtils.isEmpty(employeeCode) || StringUtils.isEmpty(customerCode)
                    || StringUtils.isEmpty(customerName) || StringUtils.isEmpty(type)) {
                throw new RuntimeException("上传信息中缺少关键资料");
            }

            UploadFile uploadFile = uploadFileTool.upload(openId, realName, multipartFile);
            if (uploadFile == null) {
                throw new RuntimeException("上传文件失败!");
            }
            CrmIndex crmIndex = new CrmIndex();
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
            crmIndex.setCreateTime(Timestamp.valueOf(LocalDateTime.parse(createTime, dateTimeFormatter)));
            crmIndex.setEmployeeCode(employeeCode);
            crmIndex.setCustomerCode(customerCode);
            crmIndex.setCustomerName(customerName);
            crmIndex.setType(type);
            crmIndex.setJson(json);
            crmIndex.setOpenId(openId);
            crmIndex.setPath(uploadFile.getPath());

            // 将最新10条上传的信息放入redis缓存
            if (redisUtils.size(Constant.REDIS_UPLOAD_QUEUE_NAME) >= Constant.REDIS_UPLOAD_QUEUE_COUNT) {
                log.warn(Constant.REDIS_UPLOAD_QUEUE_NAME.concat("队列已满,移除最旧上传信息:") + redisUtils.rightPop(Constant.REDIS_UPLOAD_QUEUE_NAME));
            }
            log.info(Constant.REDIS_UPLOAD_QUEUE_NAME.concat("队列增加上传信息:").concat(crmIndex.toString()));
            redisUtils.leftPush(Constant.REDIS_UPLOAD_QUEUE_NAME, crmIndex);

            return new Result<CrmIndex>().ok(crmIndexRepository.save(crmIndex));

        } catch (JSONException ex) {
            throw new RuntimeException("json转换失败:" + ex.getMessage());
        }

    }
    ...
}

文件上传的原理与实现可参考该文章《SpringBoot实现微信小程序文件上传的完整案例》

五、测试与验证

  • 微信小程序端
    – 前端将识别信息与图片上传至服务器
    在这里插入图片描述
    在这里插入图片描述
  • Redis缓存队列
    – 队列中只保存最新10条(数量可自行调整)信息.:

在这里插入图片描述

六、源码

Redis项目应用场景与实例(三)后端源码
Redis项目应用场景与实例(三)前端源码


原网址: 访问
创建于: 2021-02-04 14:25:23
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论