java 文件服务器 清缓存_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;

}

补充:java按照关键字指定的key删除redis(支持模糊删除)

pom依赖:

redis.clients

jedis

2.5.0

代码直接可用:

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");

}

/**

* 删除数据

*

*

* @param key:要删除数据的key

* @return:返回boolean值,表示是否删除成功

*/

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");

}

}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值