Authored by hugufei

ehcache限制使用内存大小

... ... @@ -29,13 +29,21 @@ public class EhCache implements CacheInterface {
@PostConstruct
private void init() {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ehcache.xml");
this.manager = CacheManager.create(inputStream);
this.cache = manager.getCache("yohosearch");
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ehcache.xml");
this.manager = CacheManager.create(inputStream);
this.cache = manager.getCache("yohosearch");
} catch (Throwable e) {
e.printStackTrace();
}
}
public String getStatistics(){
return cache.getStatistics().toString();
}
@PreDestroy
private void Destroy() {
private void destroy() {
if (this.manager != null) {
this.manager.shutdown();
}
... ...
package com.yoho.search.restapi.tools;
import java.util.HashMap;
import java.util.Map;
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.common.cache.impls.EhCache;
import com.yoho.search.common.cache.model.CacheObject;
import com.yoho.search.service.scene.common.CommonSceneProductListService;
@Controller
public class TestEhcacheController {
@Autowired
private EhCache ehCache;
@Autowired
private CommonSceneProductListService commonSceneProductListService;
@RequestMapping(value = "/testPut")
@ResponseBody
public Map<String, Object> testPut(int count) {
Map<String,String> paramMap = new HashMap<String, String>();
JSONObject productList = (JSONObject)commonSceneProductListService.productList(paramMap).getData();
for (int i=1;i<=count;i++) {
paramMap.put("viewNum", "300");
ehCache.addOrUpdate("Key"+i, new CacheObject(productList), 1);
}
System.out.println(ehCache.getStatistics());
return new HashMap<String, Object>();
}
@RequestMapping(value = "/testGet")
@ResponseBody
public Map<String, Object> testGet(int count) {
for (int i=0;i<count;i++) {
System.out.println("Key"+i+"_"+ehCache.exist("Key"+i));
}
System.out.println(ehCache.getStatistics());
return new HashMap<String, Object>();
}
}
... ...
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
<ehcache
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false"
>
<diskStore path="java.io.tmpdir" />
<defaultCache
maxElementsInMemory="10000"
maxBytesLocalHeap="50M"
maxElementsOnDisk="0"
eternal="true"
overflowToDisk="true"
... ... @@ -16,7 +20,7 @@
memoryStoreEvictionPolicy="LFU" />
<cache name="yohosearch"
maxElementsInMemory="3000"
maxBytesLocalHeap="300M"
maxElementsOnDisk="0"
eternal="false"
overflowToDisk="false"
... ...