今天就跟大家聊聊有关使用java怎么对redis进行增删查并清理缓存,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。Controller@apiOperation(value 
今天就跟大家聊聊有关使用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); }
增删查区分 读缓存 = 0; 添加 =1;删除 = 2 ; 清除相应经纪人清单缓存 = 3 ")
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; }
补充:java按照关键字指定的key删除redis(支持模糊删除)
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.5.0</version> </dependency>
import org.testng.annotations.BeforeClass;import org.testng.annotations.Test;import redis.clients.jedis.Jedis;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;public class TestRedis { private Jedis jedis; @BeforeClass public void setup() { //连接redis服务器,ip jedis = new Jedis("ip", 6379); //权限认证 jedis.auth("123456"); } public boolean delete(String key) { if (jedis.exists(key)) { if (jedis.del(key) == 1) { System.out.println("删除数据成功"); return true; } else { System.out.println("删除数据失败"); return false; } } else { System.out.println(key + "不存在"); return false; } } @Test public void test() { delete("age"); }}
看完上述内容,你们对使用java怎么对redis进行增删查并清理缓存有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网精选频道,感谢大家的支持。
--结束END--
本文标题: 使用java怎么对redis进行增删查并清理缓存
本文链接: https://lsjlt.com/news/247444.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0