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依賴:

 <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");
 }
 /**
  * 刪除數據
  * 
  *
  * @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");
 }
}

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀: