EHCache upgrade.

- some minor API changes
 - Spring and Hibernate share same CacheManagerFactory now.  This allows overridable config
 - Moved EHCache config files down into config/alfresco

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2521 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2006-03-07 15:34:17 +00:00
parent f64119ac90
commit 2cf407613a
7 changed files with 223 additions and 33 deletions

View File

@@ -15,10 +15,16 @@
<bean name="transactionalEHCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" > <bean name="transactionalEHCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >
<property name="configLocation"> <property name="configLocation">
<value>classpath:ehcache-transactional.xml</value> <value>classpath:alfresco/ehcache-transactional.xml</value>
</property> </property>
</bean> </bean>
<!-- ============================================ -->
<!-- EH Cache Manager to produce shared EH Caches -->
<!-- ============================================ -->
<bean name="internalEHCacheManager" class="org.alfresco.repo.cache.InternalEhCacheManagerFactoryBean" />
<!-- ================ --> <!-- ================ -->
<!-- Null permissions --> <!-- Null permissions -->
<!-- ================ --> <!-- ================ -->
@@ -28,8 +34,10 @@
<bean name="nullPermissionSharedCache" class="org.alfresco.repo.cache.EhCacheAdapter"> <bean name="nullPermissionSharedCache" class="org.alfresco.repo.cache.EhCacheAdapter">
<property name="cache"> <property name="cache">
<bean class="org.springframework.cache.ehcache.EhCacheFactoryBean" > <bean class="org.springframework.cache.ehcache.EhCacheFactoryBean" >
<property name="cacheManager">
<ref bean="internalEHCacheManager" />
</property>
<property name="cacheName"> <property name="cacheName">
<!-- The name of the ehCache area -->
<value>nullPermissionCache</value> <value>nullPermissionCache</value>
</property> </property>
<property name="maxElementsInMemory"> <property name="maxElementsInMemory">
@@ -51,7 +59,6 @@
<property name="cacheManager" > <property name="cacheManager" >
<ref bean="transactionalEHCacheManager" /> <ref bean="transactionalEHCacheManager" />
</property> </property>
<!-- Eh cache area -->
<property name="name"> <property name="name">
<value>nullPermissionTransactionalCache</value> <value>nullPermissionTransactionalCache</value>
</property> </property>
@@ -69,8 +76,10 @@
<bean name="userToAuthoritySharedCache" class="org.alfresco.repo.cache.EhCacheAdapter"> <bean name="userToAuthoritySharedCache" class="org.alfresco.repo.cache.EhCacheAdapter">
<property name="cache"> <property name="cache">
<bean class="org.springframework.cache.ehcache.EhCacheFactoryBean" > <bean class="org.springframework.cache.ehcache.EhCacheFactoryBean" >
<property name="cacheManager">
<ref bean="internalEHCacheManager" />
</property>
<property name="cacheName"> <property name="cacheName">
<!-- The name of the ehCache area -->
<value>userToAuthorityCache</value> <value>userToAuthorityCache</value>
</property> </property>
<property name="maxElementsInMemory"> <property name="maxElementsInMemory">
@@ -92,7 +101,6 @@
<property name="cacheManager" > <property name="cacheManager" >
<ref bean="transactionalEHCacheManager" /> <ref bean="transactionalEHCacheManager" />
</property> </property>
<!-- Eh cache area -->
<property name="name"> <property name="name">
<value>userToAuthorityTransactionalCache</value> <value>userToAuthorityTransactionalCache</value>
</property> </property>
@@ -110,8 +118,10 @@
<bean name="permissionsAccessSharedCache" class="org.alfresco.repo.cache.EhCacheAdapter"> <bean name="permissionsAccessSharedCache" class="org.alfresco.repo.cache.EhCacheAdapter">
<property name="cache"> <property name="cache">
<bean class="org.springframework.cache.ehcache.EhCacheFactoryBean" > <bean class="org.springframework.cache.ehcache.EhCacheFactoryBean" >
<property name="cacheManager">
<ref bean="internalEHCacheManager" />
</property>
<property name="cacheName"> <property name="cacheName">
<!-- The name of the ehCache area -->
<value>permissionsAccessCache</value> <value>permissionsAccessCache</value>
</property> </property>
<property name="maxElementsInMemory"> <property name="maxElementsInMemory">
@@ -151,8 +161,10 @@
<bean name="nodeOwnerSharedCache" class="org.alfresco.repo.cache.EhCacheAdapter"> <bean name="nodeOwnerSharedCache" class="org.alfresco.repo.cache.EhCacheAdapter">
<property name="cache"> <property name="cache">
<bean class="org.springframework.cache.ehcache.EhCacheFactoryBean" > <bean class="org.springframework.cache.ehcache.EhCacheFactoryBean" >
<property name="cacheManager">
<ref bean="internalEHCacheManager" />
</property>
<property name="cacheName"> <property name="cacheName">
<!-- The name of the ehCache area -->
<value>nodeOwnerCache</value> <value>nodeOwnerCache</value>
</property> </property>
<property name="maxElementsInMemory"> <property name="maxElementsInMemory">
@@ -174,7 +186,6 @@
<property name="cacheManager" > <property name="cacheManager" >
<ref bean="transactionalEHCacheManager" /> <ref bean="transactionalEHCacheManager" />
</property> </property>
<!-- Eh cache area -->
<property name="name"> <property name="name">
<value>nodeOwnerTransactionalCache</value> <value>nodeOwnerTransactionalCache</value>
</property> </property>

View File

@@ -823,11 +823,4 @@
</property> </property>
</bean> </bean>
<!-- caching -->
<bean name="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>
</beans> </beans>

View File

@@ -7,7 +7,7 @@ hibernate.show_sql=false
hibernate.hbm2ddl.auto=update hibernate.hbm2ddl.auto=update
hibernate.cache.use_query_cache=true hibernate.cache.use_query_cache=true
hibernate.max_fetch_depth=10 hibernate.max_fetch_depth=10
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider hibernate.cache.provider_class=org.alfresco.repo.cache.InternalEhCacheManagerFactoryBean
hibernate.cache.use_second_level_cache=true hibernate.cache.use_second_level_cache=true
hibernate.default_batch_fetch_size=1 hibernate.default_batch_fetch_size=1
hibernate.jdbc.batch_size=32 hibernate.jdbc.batch_size=32

View File

@@ -26,8 +26,8 @@ import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException; import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager; import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element; import net.sf.ehcache.Element;
import net.sf.ehcache.Status;
import org.alfresco.error.AlfrescoRuntimeException;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.quartz.Job; import org.quartz.Job;
@@ -115,7 +115,7 @@ public class EhCacheTracerJob implements Job
public CacheAnalysis(Cache cache) throws CacheException public CacheAnalysis(Cache cache) throws CacheException
{ {
this.cache = cache; this.cache = cache;
if (this.cache.getStatus() == Cache.STATUS_ALIVE) if (this.cache.getStatus().equals(Status.STATUS_ALIVE))
{ {
try try
{ {
@@ -166,21 +166,6 @@ public class EhCacheTracerJob implements Job
} }
} }
public String getStatusStr()
{
switch (cache.getStatus())
{
case Cache.STATUS_ALIVE:
return "ALIVE";
case Cache.STATUS_DISPOSED:
return "DISPOSED";
case Cache.STATUS_UNINITIALISED:
return "UNINITIALIZED";
default:
throw new AlfrescoRuntimeException("Unknown cache status: " + cache.getStatus());
}
}
public String toString() public String toString()
{ {
double sizeMB = (double)getSize()/1024.0/1024.0; double sizeMB = (double)getSize()/1024.0/1024.0;

View File

@@ -0,0 +1,201 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.cache;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Properties;
import net.sf.ehcache.CacheManager;
import org.alfresco.error.AlfrescoRuntimeException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.cache.Cache;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.CacheProvider;
import org.hibernate.cache.EhCacheProvider;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.util.ResourceUtils;
/**
* Alfresco-specific cache manager factory.
* <p>
* The purpose of this bean is to provide a common point from which the system-wide
* EHCache <code>CacheManager</code> singleton is created. Hibernate and Spring
* will all pick up the same <code>CacheManager</code> instance. It then becomes
* possible to initialise this instance in whichever way we require, provided it
* is done in a well-known (non-configurable) way.
* <p>
* For Alfresco purposes, there are two files that are looked for:
* <ul>
* <li><b>classpath:alfresco/extension/ehcache-custom.xml</b>, which will take precedence</li>
* <li><b>classpath:alfresco/ehcache.xml</b>, which is the default shipped with Alfresco</li>
* </ul>
* <p>
* The EHCache static singleton instance is used but ensuring that all access to the
* instance goes through the required initialization code first.
* <p>
* TODO: Provide mixing of config so that cache definitions in the custom file override
* those in the default file
*
* @see #getInstance()
*
* @author Derek Hulley
*/
public class InternalEhCacheManagerFactoryBean implements FactoryBean, CacheProvider
{
public static final String CUSTOM_CONFIGURATION_FILE = "classpath:alfresco/extension/ehcache-custom.xml";
public static final String DEFAULT_CONFIGURATION_FILE = "classpath:alfresco/ehcache-default.xml";
private static Log logger = LogFactory.getLog(InternalEhCacheManagerFactoryBean.class);
/** keep track of the singleton status to avoid work */
private static boolean initialized;
/** used to ensure that the existing Hibernate logic is maintained */
private static EhCacheProvider hibernateEhCacheProvider = new EhCacheProvider();
/**
* Default constructor required by Hibernate. In fact, we anticipate several
* instances of this class to be created.
*/
public InternalEhCacheManagerFactoryBean()
{
}
/**
* News up the singleton cache manager according to the rules set out
* in the class comments.
*/
private static synchronized void initCacheManager()
{
if (initialized)
{
return;
}
try
{
boolean defaultLocation = false;
try
{
URL configUrl = ResourceUtils.getURL(CUSTOM_CONFIGURATION_FILE);
CacheManager.create(configUrl);
}
catch (FileNotFoundException e)
{
// try the alfresco default
URL configUrl = ResourceUtils.getURL(DEFAULT_CONFIGURATION_FILE);
CacheManager.create(configUrl); // this file MUST be present
defaultLocation = true;
}
// done
if (logger.isDebugEnabled())
{
logger.debug("Created EHCache CacheManager instance: \n" +
" configuration: " + (defaultLocation ? DEFAULT_CONFIGURATION_FILE : CUSTOM_CONFIGURATION_FILE));
}
initialized = true;
}
catch (Throwable e)
{
throw new AlfrescoRuntimeException("EHCache configuration failed", e);
}
}
/**
* @return Returns the properly initialized instance for Alfresco internal use
*
* @see #initCacheManager()
*/
public static CacheManager getInstance()
{
initCacheManager();
return CacheManager.getInstance();
}
/**
* @see #hibernateEhCacheProvider
*/
public Cache buildCache(String regionName, Properties properties) throws CacheException
{
initCacheManager();
return hibernateEhCacheProvider.buildCache(regionName, properties);
}
/**
* @see #hibernateEhCacheProvider
*/
public boolean isMinimalPutsEnabledByDefault()
{
return hibernateEhCacheProvider.isMinimalPutsEnabledByDefault();
}
/**
* @see #hibernateEhCacheProvider
*/
public long nextTimestamp()
{
return hibernateEhCacheProvider.nextTimestamp();
}
/**
* @see #initCacheManager()
* @see #hibernateEhCacheProvider
*/
public void start(Properties properties) throws CacheException
{
initCacheManager();
hibernateEhCacheProvider.start(properties);
}
/**
* @see #initCacheManager()
* @see #hibernateEhCacheProvider
*/
public void stop()
{
initCacheManager();
hibernateEhCacheProvider.stop();
}
/**
* @return Returns the singleton cache manager
*
* @see #initCacheManager()
*/
public Object getObject() throws Exception
{
initCacheManager();
return CacheManager.getInstance();
}
/**
* @return Returns the singleton cache manager type
*/
public Class getObjectType()
{
return CacheManager.class;
}
/**
* @return Returns true always
*/
public boolean isSingleton()
{
return true;
}
}