TestEhcacheController.java 2.31 KB
package com.yoho.search.restapi.tools;

import java.util.HashMap;
import java.util.Map;

import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import net.sf.ehcache.Statistics;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yoho.search.cache.impls.EhCache;
import com.yoho.search.cache.model.CacheObject;
import com.yoho.search.service.list.ProductListSwitchService;

@Controller
public class TestEhcacheController {
	
	private static final Logger Logger = LoggerFactory.getLogger(TestEhcacheController.class);
	
	@Autowired
	private EhCache ehCache;
	@Autowired
	private ProductListSwitchService productListSwitchService;
	
	
	@RequestMapping(value = "/testEhcache/put")
	@ResponseBody
	public Map<String, Object> testPut(int count,int viewNum) {
		Map<String,String> paramMap = new HashMap<String, String>();
		paramMap.put("viewNum", ""+viewNum);
		JSONObject productList = (JSONObject) productListSwitchService.productList(paramMap).getData();
		for (int i=1;i<=count;i++) {
			ehCache.addOrUpdate(new RedisKeyBuilder().appendFixed("Key").appendVar(i), new CacheObject(productList), 1);
		}
		Map<String, Object> result =  new HashMap<String, Object>();
		result.put("statistics", ehCache.getStatistics().toString());
		return result;
	}
	
	@RequestMapping(value = "/testEhcache/get")
	@ResponseBody
	public Map<String, Object> testGet(int count) {
		for (int i=1;i<=count;i++) {
			Logger.info("key is [{}], exist is [{}]","Key"+i, ehCache.exist(new RedisKeyBuilder().appendFixed("Key").appendVar(i)));
		}
		Map<String, Object> result =  new HashMap<String, Object>();
		result.put("statistics", ehCache.getStatistics().toString());
		return result;
	}
	
	@RequestMapping(value = "/testEhcache/statistics")
	@ResponseBody
	public Map<String, Object> getStatistics() {
		Statistics statistics = ehCache.getStatistics();
		Map<String, Object> result =  new HashMap<String, Object>();
		result.put("objectCount", statistics.getObjectCount());
		result.put("memoryStoreObjectCount", statistics.getMemoryStoreObjectCount());
		return result;
	}

}