Project

General

Profile

« Previous | Next » 

Revision f4142aa7

Added by Cherian Mathew almost 9 years ago

cdmlib-ehcache : removed disk based cache manager since the hb config is now cached in memory
CdmApplicationRemoteController, CdmModelCacher, CdmRemoteCacheManager, CdmModelGetMethodCacherTest : running the hibernate config load in separate thread
CdmTransientEntityCacher, EntityCacherDebugResult, CdmStore, SessionsViewPart: refactoring
httpInvokerServiceClients.xml : removed lazy loading of beans

View differences:

eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/cache/CdmRemoteCacheManager.java
1 1
package eu.etaxonomy.taxeditor.remoting.cache;
2 2

  
3
import java.io.InputStream;
4
import java.util.HashSet;
5
import java.util.Set;
6

  
7 3
import net.sf.ehcache.Cache;
8 4
import net.sf.ehcache.CacheException;
9 5
import net.sf.ehcache.CacheManager;
10 6
import net.sf.ehcache.config.CacheConfiguration;
11 7
import net.sf.ehcache.config.SizeOfPolicyConfiguration;
12 8

  
13
import org.springframework.core.io.ClassPathResource;
14
import org.springframework.core.io.Resource;
9
import org.apache.log4j.Logger;
15 10

  
16 11

  
17 12
public class CdmRemoteCacheManager {
18 13

  
19
	private CacheManager cdmlibModelCacheManager;
14
    private static final Logger logger = Logger.getLogger(CdmRemoteCacheManager.class);
20 15

  
21
	private static CdmRemoteCacheManager cdmRemoteCacheManager = null;
22 16

  
23
	private final Set<CdmTransientEntityCacher> transientEntityCachers = new HashSet<CdmTransientEntityCacher>();
17
    private Cache cdmlibModelCache;
24 18

  
25
    public static final Resource CDMLIB_CACHE_MANAGER_CONFIG_RESOURCE =
26
            new ClassPathResource("cdmlib-ehcache.xml");
19
    private static CdmRemoteCacheManager cdmRemoteCacheManager = null;
27 20

  
28
    public static final String CDM_MODEL_CACHE_MGR_NAME = "cdmlibModelCacheManager";
29 21
    public static final String CDM_MODEL_CACHE_NAME = "cdmModelGetMethodsCache";
30 22

  
23
    private static Thread initThread;
24

  
25
    private static boolean cacheInitialised = false;
31 26

  
32 27
    public enum CdmCacheManagerType {
33
    	CDMLIB_MODEL,
34
    	DEFAULT
28
        CDMLIB_MODEL,
29
        DEFAULT
35 30
    }
36 31

  
37 32
    public static CdmRemoteCacheManager getInstance(){
38
    	if(cdmRemoteCacheManager == null) {
39
    		cdmRemoteCacheManager = new CdmRemoteCacheManager();
40
    	}
41
    	return cdmRemoteCacheManager;
33

  
34
        if(cdmRemoteCacheManager == null) {
35
            cdmRemoteCacheManager = new CdmRemoteCacheManager();
36
        }
37
        return cdmRemoteCacheManager;
42 38
    }
43 39
    private CdmRemoteCacheManager() {
44 40

  
45 41

  
46
    	try {
47
    		// NOTE:Programmatically creating the cache manager may solve the problem of
48
    		//      recreating data written to disk on startup
49
    		//      see https://stackoverflow.com/questions/1729605/ehcache-persist-to-disk-issues
50
    		//String cacheFilePath = CDMLIB_CACHE_MANAGER_CONFIG_RESOURCE.getFile().getAbsolutePath();
51
    		InputStream in = this.getClass().getClassLoader().getResourceAsStream("cdmlib-ehcache.xml");
42
        try {
43
            // NOTE:Programmatically creating the cache manager may solve the problem of
44
            //      recreating data written to disk on startup
45
            //      see https://stackoverflow.com/questions/1729605/ehcache-persist-to-disk-issues
46
            //String cacheFilePath = CDMLIB_CACHE_MANAGER_CONFIG_RESOURCE.getFile().getAbsolutePath();
47
            //InputStream in = this.getClass().getClassLoader().getResourceAsStream("cdmlib-ehcache.xml");
52 48

  
53 49
            SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
54 50
            sizeOfConfig.setMaxDepth(1000);
55 51
            sizeOfConfig.setMaxDepthExceededBehavior("abort");
56 52

  
57
    		CacheConfiguration modelcc = new CacheConfiguration(CDM_MODEL_CACHE_NAME, 0)
53
            CacheConfiguration modelcc = new CacheConfiguration(CDM_MODEL_CACHE_NAME, 0)
58 54
            .eternal(true)
59 55
            .statistics(true)
60 56
            .sizeOfPolicy(sizeOfConfig)
61 57
            .overflowToOffHeap(false);
62 58

  
63
    		Cache modelCache = new Cache(modelcc);
59
            cdmlibModelCache = new Cache(modelcc);
64 60

  
65
			cdmlibModelCacheManager = CacheManager.create(CDM_MODEL_CACHE_MGR_NAME);
66
			cdmlibModelCacheManager.addCache(modelCache);
61
            CacheManager.create().addCache(cdmlibModelCache);
67 62

  
68
		} catch (CacheException e) {
69
			throw new CdmClientCacheException(e);
70
		}
63
            initCache(cdmlibModelCache);
64

  
65
        } catch (CacheException e) {
66
            throw new CdmClientCacheException(e);
67
        }
71 68

  
72 69
    }
73 70

  
74
	public Cache getCdmModelGetMethodsCache(){
75
		return cdmlibModelCacheManager.getCache(CDM_MODEL_CACHE_NAME);
76
	}
77

  
78
	public CacheManager getCdmModelGetMethodsCacheManager() {
79
	    return cdmlibModelCacheManager;
80
	}
81

  
82
	public CacheManager getDefaultCacheManager() {
83
	    return CacheManager.create();
84
	}
85

  
86
	public void shutdown(CdmCacheManagerType ccmt) {
87
		CacheManager cm;
88
		switch(ccmt) {
89
		case CDMLIB_MODEL:
90
			cdmlibModelCacheManager.shutdown();
91
			break;
92
		case DEFAULT:
93
			cm = CacheManager.create();
94
			cm.shutdown();
95
			break;
96
		default:
97
			//do nothing
98
		}
99
	}
71
    private void initCache(final Cache cache) {
72

  
73
        initThread = new Thread() {
74
            @Override
75
            public void run(){
76
                synchronized (cdmlibModelCache) {
77
                    CdmModelCacher cmdmc = new CdmModelCacher();
78
                    cmdmc.cacheGetterFields(cache);
79
                    cacheInitialised = true;
80
                    logger.info("Initialisation of CDM getter fields complete");
81
                    cdmlibModelCache.notify();
82
                }
83
            }
84

  
85
        };
86
        initThread.start();
87
    }
88

  
89
    public Cache getCdmModelGetMethodsCache(){
90
        synchronized (cdmlibModelCache) {
91
            while(!cacheInitialised) {
92
                try {
93
                    logger.info("Waiting for initialisation of CDM getter fields to complete ...");
94
                    cdmlibModelCache.wait();
95
                } catch (InterruptedException e) {}
96
            }
97
        }
98
        logger.info("CDM getter fields cache initialised");
99
        return cdmlibModelCache;
100
    }
101

  
102
    public static void removeEntityCaches() {
103
        CacheManager cm = CacheManager.create();
104
        String[] cacheNames = CacheManager.create().getCacheNames();
105
        for(String cacheName : cacheNames) {
106
            if(!cacheName.equals(CDM_MODEL_CACHE_NAME)) {
107
                cm.removeCache(cacheName);
108
            }
109
        }
110
    }
100 111

  
101 112

  
102 113

  

Also available in: Unified diff