java中redis增删查以及清理缓存的案例
我就废话不多说了,大家还是直接看代码吧~
Controller
@ApiOperation(value="获取信息放入Redis中") @RequestMapping(value="/getPropertyTakeLookShoppingDetailRedis",method=RequestMethod.POST) publicJsonResultgetPropertyTakeLookShoppingDetailRedis(@RequestBodyPropertyTakeLookRedisParamparam)throwsException{ log.info("PropertyTakeLookController.getPropertyTakeLookShoppingDetailRedis:"+param); JSONObjectjsonObject=propertyTakeLookWriteService.getPropertyTakeLookShoppingDetailRedis(param); log.info("PropertyTakeLookController.getPropertyTakeLookSysInfo:"+jsonObject); returnJsonResult.getResult(jsonObject); }
Service
增删查区分读缓存=0;添加=1;删除=2;清除相应经纪人清单缓存=3")
/** *获取信息放入Redis中 *@return *@throwsException */ publicJSONObjectgetPropertyTakeLookShoppingDetailRedis(PropertyTakeLookRedisParamparam)throwsException{ StringtakeLookStr="DKQD"; checkArgument(StringUtils.isNotBlank(param.getTakeLookUserEmpNo()),"工号不能为空!"); checkArgument(param.getSelOrAddOrRemove()!=null,"增删查区分不能为空!"); StringtakeLookUserEmpNo=param.getTakeLookUserEmpNo(); JSONObjectjsonObject; if(redisHandle.exists(takeLookUserEmpNo+takeLookStr)&¶m.getSelOrAddOrRemove()==0){ jsonObject=JSONObject.parseObject(redisHandle.get(takeLookUserEmpNo+takeLookStr).toString()); }elseif(param.getSelOrAddOrRemove()==1||param.getSelOrAddOrRemove()==2){ Long[]effectiveIds=param.getPropertyEffectiveId(); PropertyTakeLookShoppingCartParampropertyTakeLookShoppingCartParam=newPropertyTakeLookShoppingCartParam(); 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()); ObjectjsonArray=jsonObject.get("data"); redisHandle.set(takeLookUserEmpNo+takeLookStr,jsonArray); }elseif(redisHandle.exists(takeLookUserEmpNo+takeLookStr)&¶m.getSelOrAddOrRemove()==3){ //物理清除缓存 redisHandle.remove(takeLookUserEmpNo+takeLookStr); jsonObject=newJSONObject(); jsonObject.put("code",ErrorCode.SUCCESS.getCode()); jsonObject.put("msg",ErrorCode.SUCCESS.getMsg()); }else{ jsonObject=newJSONObject(); jsonObject.put("code",ErrorCode.SUCCESS.getCode()); jsonObject.put("msg",ErrorCode.SUCCESS.getMsg()); } returnjsonObject; }
补充:java按照关键字指定的key删除redis(支持模糊删除)
pom依赖:
redis.clients jedis 2.5.0
代码直接可用:
importorg.testng.annotations.BeforeClass; importorg.testng.annotations.Test; importredis.clients.jedis.Jedis; importjava.util.HashMap; importjava.util.Iterator; importjava.util.List; importjava.util.Map; publicclassTestRedis{ privateJedisjedis; @BeforeClass publicvoidsetup(){ //连接redis服务器,ip jedis=newJedis("ip",6379); //权限认证 jedis.auth("123456"); } /** *删除数据 * * *@paramkey:要删除数据的key *@return:返回boolean值,表示是否删除成功 */ publicbooleandelete(Stringkey){ if(jedis.exists(key)){ if(jedis.del(key)==1){ System.out.println("删除数据成功"); returntrue; }else{ System.out.println("删除数据失败"); returnfalse; } }else{ System.out.println(key+"不存在"); returnfalse; } } @Test publicvoidtest(){ delete("age"); } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持毛票票。如有错误或未考虑完全的地方,望不吝赐教。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。