package cn.com.qmth.stmms.monitor;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller("cacheMonitor")
@RequestMapping("/monitor/cache")
public class CacheMonitorController {

    @Autowired
    private CacheManager cacheManager;

    @RequestMapping
    public ModelAndView list(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("modules/monitor/cache/list");
        List<String> list = new LinkedList<String>();
        list.addAll(cacheManager.getCacheNames());
        view.addObject("caches", list);
        return view;
    }

    @RequestMapping("/clear")
    @ResponseBody
    public boolean clear(HttpServletRequest request, @RequestParam String name) {
        Cache cache = cacheManager.getCache(name);
        if (cache != null) {
            cache.clear();
            return true;
        } else {
            return false;
        }
    }
}
