22
33import java .util .Arrays ;
44
5+ import net .sf .ehcache .Element ;
56import org .slf4j .Logger ;
67import org .slf4j .LoggerFactory ;
78import org .springframework .beans .factory .annotation .Autowired ;
8- import org .springframework .context .ApplicationContext ;
99import org .springframework .stereotype .Controller ;
1010import org .springframework .ui .ModelMap ;
1111import org .springframework .web .bind .annotation .PathVariable ;
1212import org .springframework .web .bind .annotation .RequestMapping ;
1313import net .sf .ehcache .Cache ;
1414import net .sf .ehcache .CacheManager ;
15+ import org .springframework .web .bind .annotation .ResponseBody ;
1516
1617/**
1718 * 浏览ehcache状态
@@ -24,14 +25,15 @@ public class EhcacheController {
2425// private ApplicationContext applicationContext;
2526 @ Autowired
2627 private CacheManager cacheManager ;
28+
2729 @ RequestMapping (value = {"/index/{cache}" })
2830 public String index (@ PathVariable ("cache" ) String cache , ModelMap m ) {
2931 logger .debug ("cache:{}" , cache );
3032 if (cacheManager !=null ){
3133 m .put ("caches" ,Arrays .deepToString (cacheManager .getCacheNames ()));
3234 m .put ("getDiskStorePath" , cacheManager .getDiskStorePath ());
3335 m .put ("getName" , cacheManager .getName ());
34- // m.put("getOriginalConfigurationText", cacheManager.getOriginalConfigurationText());
36+ m .put ("getOriginalConfigurationText" , cacheManager .getOriginalConfigurationText ());
3537 m .put ("getStatus" , cacheManager .getStatus ());
3638 Cache c =cacheManager .getCache (cache );
3739 if (c !=null ){
@@ -42,4 +44,33 @@ public String index(@PathVariable("cache") String cache, ModelMap m) {
4244 logger .debug ("ehcache model:{}" ,m );
4345 return "ehcache" ;
4446 }
47+
48+ /**
49+ * put到指定cache下
50+ * @param cacheName
51+ * @param key
52+ * @param val
53+ * @return
54+ */
55+ @ RequestMapping (value = {"/putcache/{cache}/{key}/{val}" })
56+ @ ResponseBody
57+ public Element put (@ PathVariable ("cache" ) String cacheName ,@ PathVariable ("key" ) String key ,@ PathVariable ("val" ) String val ) {
58+ Cache cache =cacheManager .getCache (cacheName );
59+ Element element =new Element (key ,val );
60+ cache .put (element );
61+ return element ;
62+ }
63+
64+ /**
65+ * 获取指定cache下的指定key内容
66+ * @param cacheName
67+ * @param key
68+ * @return
69+ */
70+ @ RequestMapping (value = {"/getcache/{cache}/{key}" })
71+ @ ResponseBody
72+ public Element get (@ PathVariable ("cache" ) String cacheName ,@ PathVariable ("key" ) String key ) {
73+ Cache cache =cacheManager .getCache (cacheName );
74+ return cache .get (key );
75+ }
4576}
0 commit comments