关于项目里server清楚缓存的代码

时间:2022-08-17 04:44:09

Venk proc存在很多问题,不能应对高并发的情况,所以提供了这个 方法来清理cache, 但是前提是需要有prod的权限;

要想验证是否通过URL清楚了缓存,就要 removeCache url执行之后,再去执行 listCache 看看是不是少了一条

removeCache

AdminController.java

package com.citi.recon.controller;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.citi.recon.service.ICommonDataService;

@Controller
@RequestMapping("/admin")
public class AdminController {

@Autowired
private ICommonDataService commonDataService;

@RequestMapping(value = "/refreshCache", method = { RequestMethod.POST,
RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String refreshCache(WebRequest webRequest,
HttpServletRequest request, HttpServletResponse response) {
return "cache refreshed "+commonDataService.refreshcache();
}

@RequestMapping(value = "/removeCache/{id}", method = { RequestMethod.POST,
RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String removeCache(WebRequest webRequest,
HttpServletRequest request, HttpServletResponse response,@PathVariable String id) {
return "cache removed: "+commonDataService.removeCache(id);
}

@RequestMapping(value = "/listCache", method = { RequestMethod.POST,
RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Set<String> listCache(WebRequest webRequest,
HttpServletRequest request, HttpServletResponse response) {
return commonDataService.getCache().keySet();
}

}