Let the EHCache adapter be a little more configurable.

This commit is contained in:
Par Winzell
2009-01-10 01:46:50 +00:00
parent a8b3e24e87
commit a5de1295f9
@@ -3,7 +3,7 @@
// //
// Depot library - a Java relational persistence library // Depot library - a Java relational persistence library
// Copyright (C) 2006-2008 Michael Bayne and Pär Winzell // Copyright (C) 2006-2008 Michael Bayne and Pär Winzell
// //
// This library is free software; you can redistribute it and/or modify it // This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published // under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or // by the Free Software Foundation; either version 2.1 of the License, or
@@ -42,20 +42,20 @@ public class EHCacheAdapter
* the supplied manager when it is shutdown. The caller is responsible for shutting down the * the supplied manager when it is shutdown. The caller is responsible for shutting down the
* cache manager when it knows that Depot and any other clients no longer need it. * cache manager when it knows that Depot and any other clients no longer need it.
*/ */
public EHCacheAdapter (CacheManager cachemgr) public EHCacheAdapter (EHCacheConfig config, CacheManager cachemgr)
{ {
_config = config;
_cachemgr = cachemgr; _cachemgr = cachemgr;
CacheManagerPeerListener listener = _cachemgr.getCachePeerListener(); if (config.distributed) {
CacheManagerPeerProvider provider = _cachemgr.getCachePeerProvider(); CacheManagerPeerListener listener = _cachemgr.getCachePeerListener();
if ((provider != null) != (listener != null)) { CacheManagerPeerProvider provider = _cachemgr.getCachePeerProvider();
// we want either both listener and provider, or neither if (provider == null || listener == null) {
log.warning("EHCache misconfigured, distributed mode disabled [listener =" + // we want either both listener and provider, or neither
listener + ", provider=" + provider); log.warning("EHCache misconfigured, distributed mode disabled [listener =" +
_distributed = false; listener + ", provider=" + provider);
_config.distributed = false;
} else { }
_distributed = (listener != null);
} }
} }
@@ -117,17 +117,22 @@ public class EHCacheAdapter
// create the cache programatically with reasonable settings // create the cache programatically with reasonable settings
// TODO: we will eventually need this to be configurable in .properties // TODO: we will eventually need this to be configurable in .properties
_cache = new Cache(id, _cache = new Cache(id,
1000, // keep 1000 elements in RAM _config.elementsInMemory,
true, // overflow the rest to disk _config.overflowToDisk,
false, // don't keep records around eternally false,
300, // keep them for 5 minutes after they're created _config.timeToLiveSeconds,
20); // or 20 seconds after last access _config.timeToIdleSeconds);
if (_distributed) { if (_config.distributed) {
// a programatically created cache has to have its replicator event // a programatically created cache has to have its replicator event
// listener programatically added. // listener programatically added.
_cache.getCacheEventNotificationService().registerListener( _cache.getCacheEventNotificationService().registerListener(
new RMIAsynchronousCacheReplicator(true, true, true, true, 1000)); new RMIAsynchronousCacheReplicator(
_config.sendNewRecordsToPeers,
_config.invalidateUpdatedRecordsOnPeers,
_config.sendUpdatedRecordsToPeers,
_config.invalidateRemovedRecordsOnPeers,
_config.replicationInterval));
} }
_cachemgr.addCache(_cache); _cachemgr.addCache(_cache);
} }
@@ -142,7 +147,7 @@ public class EHCacheAdapter
{ {
} }
protected boolean _distributed; protected EHCacheConfig _config;
protected CacheManager _cachemgr; protected CacheManager _cachemgr;
// this is just for convenience and memory use; we don't rely on pointer equality anywhere // this is just for convenience and memory use; we don't rely on pointer equality anywhere