TestEhcacheController.java
2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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.scene.pages.entrance.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;
}
}