Authored by hugufei

添加encache的配置文件

1 package com.yoho.search.common.cache.impls; 1 package com.yoho.search.common.cache.impls;
2 2
  3 +import java.io.InputStream;
  4 +
3 import javax.annotation.PostConstruct; 5 import javax.annotation.PostConstruct;
  6 +import javax.annotation.PreDestroy;
  7 +
  8 +import net.sf.ehcache.Cache;
  9 +import net.sf.ehcache.CacheManager;
  10 +import net.sf.ehcache.Element;
4 11
5 import org.slf4j.Logger; 12 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory; 13 import org.slf4j.LoggerFactory;
@@ -8,19 +15,13 @@ import org.springframework.stereotype.Service; @@ -8,19 +15,13 @@ import org.springframework.stereotype.Service;
8 15
9 import com.yoho.search.common.cache.model.CacheObject; 16 import com.yoho.search.common.cache.model.CacheObject;
10 17
11 -import net.sf.ehcache.Cache;  
12 -import net.sf.ehcache.CacheManager;  
13 -import net.sf.ehcache.Element;  
14 -  
15 @Service("ehCache") 18 @Service("ehCache")
16 public class EhCache implements CacheInterface { 19 public class EhCache implements CacheInterface {
17 20
18 private static final Logger logger = LoggerFactory.getLogger(EhCache.class); 21 private static final Logger logger = LoggerFactory.getLogger(EhCache.class);
19 22
20 - private static final CacheManager cacheManager = CacheManager.create(); 23 + private CacheManager manager;
21 24
22 - private static final String cacheName = "talent";  
23 -  
24 private Cache cache; 25 private Cache cache;
25 26
26 private EhCache() { 27 private EhCache() {
@@ -28,10 +29,22 @@ public class EhCache implements CacheInterface { @@ -28,10 +29,22 @@ public class EhCache implements CacheInterface {
28 29
29 @PostConstruct 30 @PostConstruct
30 private void init() { 31 private void init() {
31 - cacheManager.addCache(cacheName);  
32 - this.cache = cacheManager.getCache(cacheName); 32 + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ehcache.xml");
  33 + if (inputStream == null) {
  34 + logger.error("url is null");
  35 + return;
  36 + }
  37 + this.manager = CacheManager.create(inputStream);
  38 + this.cache = manager.getCache("yohosearch");
33 } 39 }
34 - 40 +
  41 + @PreDestroy
  42 + private void Destroy() {
  43 + if (this.manager != null) {
  44 + this.manager.shutdown();
  45 + }
  46 + }
  47 +
35 @Override 48 @Override
36 public void addOrUpdate(String key, CacheObject value, int expiredTimeInMinute) { 49 public void addOrUpdate(String key, CacheObject value, int expiredTimeInMinute) {
37 try { 50 try {
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
  3 +
  4 + <diskStore path="java.io.tmpdir" />
  5 +
  6 + <defaultCache
  7 + maxElementsInMemory="10000"
  8 + maxElementsOnDisk="0"
  9 + eternal="true"
  10 + overflowToDisk="true"
  11 + diskPersistent="false"
  12 + timeToIdleSeconds="0"
  13 + timeToLiveSeconds="0"
  14 + diskSpoolBufferSizeMB="50"
  15 + diskExpiryThreadIntervalSeconds="120"
  16 + memoryStoreEvictionPolicy="LFU" />
  17 +
  18 + <cache name="yohosearch"
  19 + maxElementsInMemory="3000"
  20 + maxElementsOnDisk="0"
  21 + eternal="false"
  22 + overflowToDisk="false"
  23 + diskPersistent="false"
  24 + timeToIdleSeconds="120"
  25 + timeToLiveSeconds="300"
  26 + diskSpoolBufferSizeMB="50"
  27 + diskExpiryThreadIntervalSeconds="120"
  28 + memoryStoreEvictionPolicy="LFU" />
  29 +
  30 +</ehcache>