JAVA中redis增删查以及清理缓存

Controller

@ApiOperation(value = "获取信息放入Redis中")
    @RequestMapping(value = "/getPropertyTakeLookShoppingDetailRedis",method = RequestMethod.POST)
    public JsonResult getPropertyTakeLookShoppingDetailRedis(@RequestBody PropertyTakeLookRedisParam param) throws Exception {
        log.info("PropertyTakeLookController.getPropertyTakeLookShoppingDetailRedis:" + param);
        JSONObject jsonObject = propertyTakeLookWriteService.getPropertyTakeLookShoppingDetailRedis(param);
        log.info("PropertyTakeLookController.getPropertyTakeLookSysInfo:" + jsonObject);
        return JsonResult.getResult(jsonObject);
    }

Service

增删查区分 读缓存 = 0; 添加 =1;删除 = 2 ; 清除相应经纪人清单缓存 = 3 ")

 /**
     * 获取信息放入Redis中 
     * @return
     * @throws Exception
     */
    public JSONObject getPropertyTakeLookShoppingDetailRedis(PropertyTakeLookRedisParam param) throws Exception {
        String takeLookStr = "DKQD";
        checkArgument(StringUtils.isNotBlank(param.getTakeLookUserEmpNo()), "工号不能为空!");
        checkArgument(param.getSelOrAddOrRemove() != null, "增删查区分不能为空!");
        String takeLookUserEmpNo = param.getTakeLookUserEmpNo();
        JSONObject jsonObject;
        if(redisHandle.exists(takeLookUserEmpNo+takeLookStr) && param.getSelOrAddOrRemove() == 0){
            jsonObject = JSONObject.parseObject(redisHandle.get(takeLookUserEmpNo+takeLookStr).toString());
        } else if(param.getSelOrAddOrRemove() == 1 || param.getSelOrAddOrRemove() == 2){
            Long[] effectiveIds = param.getPropertyEffectiveId();
            PropertyTakeLookShoppingCartParam propertyTakeLookShoppingCartParam = new PropertyTakeLookShoppingCartParam();
            BeanUtils.copyProperties(param,propertyTakeLookShoppingCartParam);
            propertyTakeLookShoppingCartParam.setPropertyEffectiveId(effectiveIds);
            //获取信息
            PageInfo takeLookDetail = propertytakelookService.getPropertyTakeLookShoppingCartDetail(propertyTakeLookShoppingCartParam);
            System.out.print("参数查看" + JsonResult.getResult(takeLookDetail).toJson());
            jsonObject = JSONObject.parseObject(JsonResult.getResult(takeLookDetail).toJson());
            Object jsonArray = jsonObject.get("data");
            redisHandle.set(takeLookUserEmpNo+takeLookStr,jsonArray);
        }else if (redisHandle.exists(takeLookUserEmpNo+takeLookStr) && param.getSelOrAddOrRemove() == 3){
            //物理清除缓存
            redisHandle.remove(takeLookUserEmpNo+takeLookStr);
            jsonObject = new JSONObject();
            jsonObject.put("code",ErrorCode.SUCCESS.getCode());
            jsonObject.put("msg",ErrorCode.SUCCESS.getMsg());
        }else {
            jsonObject = new JSONObject();
            jsonObject.put("code",ErrorCode.SUCCESS.getCode());
            jsonObject.put("msg",ErrorCode.SUCCESS.getMsg());
        }
        return jsonObject;
    }

你可能感兴趣的:(redis)