Fix AR-350: Transactional caches for node ownership and permissions statuses

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2354 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley 2006-02-13 12:28:03 +00:00
parent 12b94f82af
commit ae6bed1eb4
7 changed files with 149 additions and 177 deletions

View File

@ -13,7 +13,7 @@
</bean>
<!-- ================ -->
<!-- Null permissoins -->
<!-- Null permissions -->
<!-- ================ -->
<!-- The cross-transaction shared cache for Null Node Permissions -->
@ -25,6 +25,12 @@
<!-- The name of the ehCache area -->
<value>nullPermissionCache</value>
</property>
<property name="maxElementsInMemory">
<value>10000</value>
</property>
<property name="overflowToDisk">
<value>false</value>
</property>
</bean>
</property>
</bean>
@ -43,7 +49,7 @@
<value>nullPermissionTransactionalCache</value>
</property>
<property name="maxCacheSize">
<value>20000</value>
<value>5000</value>
</property>
</bean>
@ -51,7 +57,7 @@
<!-- Authority container look up for users -->
<!-- ===================================== -->
<!-- The cross-transaction shared cache for Users -->
<!-- The cross-transaction shared cache for User Authorities -->
<bean name="userToAuthoritySharedCache" class="org.alfresco.repo.cache.EhCacheAdapter">
<property name="cache">
@ -60,11 +66,17 @@
<!-- The name of the ehCache area -->
<value>userToAuthorityCache</value>
</property>
<property name="maxElementsInMemory">
<value>10000</value>
</property>
<property name="overflowToDisk">
<value>false</value>
</property>
</bean>
</property>
</bean>
<!-- The transactional cache for Null Node Permissions -->
<!-- The transactional cache for User Authorities -->
<bean name="userToAuthorityCache" class="org.alfresco.repo.cache.TransactionalCache">
<property name="sharedCache">
@ -78,7 +90,89 @@
<value>userToAuthorityTransactionalCache</value>
</property>
<property name="maxCacheSize">
<value>20000</value>
<value>5000</value>
</property>
</bean>
<!-- ===================================== -->
<!-- Permissions access cache -->
<!-- ===================================== -->
<!-- The cross-transaction shared cache for Permissions -->
<bean name="permissionsAccessSharedCache" class="org.alfresco.repo.cache.EhCacheAdapter">
<property name="cache">
<bean class="org.springframework.cache.ehcache.EhCacheFactoryBean" >
<property name="cacheName">
<!-- The name of the ehCache area -->
<value>permissionsAccessCache</value>
</property>
<property name="maxElementsInMemory">
<value>10000</value>
</property>
<property name="overflowToDisk">
<value>false</value>
</property>
</bean>
</property>
</bean>
<!-- The transactional cache for Permissions -->
<bean name="permissionsAccessCache" class="org.alfresco.repo.cache.TransactionalCache">
<property name="sharedCache">
<ref bean="permissionsAccessSharedCache" />
</property>
<property name="cacheManager" >
<ref bean="ehCacheManager" />
</property>
<!-- Eh cache area -->
<property name="name">
<value>permissionsAccessTransactionalCache</value>
</property>
<property name="maxCacheSize">
<value>5000</value>
</property>
</bean>
<!-- ===================================== -->
<!-- Node owner cache -->
<!-- ===================================== -->
<!-- The cross-transaction shared cache for Node Ownership -->
<bean name="nodeOwnerSharedCache" class="org.alfresco.repo.cache.EhCacheAdapter">
<property name="cache">
<bean class="org.springframework.cache.ehcache.EhCacheFactoryBean" >
<property name="cacheName">
<!-- The name of the ehCache area -->
<value>nodeOwnerCache</value>
</property>
<property name="maxElementsInMemory">
<value>10000</value>
</property>
<property name="overflowToDisk">
<value>false</value>
</property>
</bean>
</property>
</bean>
<!-- The transactional cache for Node Ownership -->
<bean name="nodeOwnerCache" class="org.alfresco.repo.cache.TransactionalCache">
<property name="sharedCache">
<ref bean="nodeOwnerSharedCache" />
</property>
<property name="cacheManager" >
<ref bean="ehCacheManager" />
</property>
<!-- Eh cache area -->
<property name="name">
<value>nodeOwnerTransactionalCache</value>
</property>
<property name="maxCacheSize">
<value>5000</value>
</property>
</bean>

View File

@ -9,5 +9,8 @@
<property name="authenticationService">
<ref bean="authenticationServiceImpl"/>
</property>
<property name="nodeOwnerCache">
<ref bean="nodeOwnerCache"/>
</property>
</bean>
</beans>

View File

@ -63,6 +63,9 @@
<property name="authorityService">
<ref bean="authorityService" />
</property>
<property name="accessCache">
<ref bean="permissionsAccessCache" />
</property>
<!-- Dynamic authorites are evaluated in the context of a store/node etc -->
<!-- as opposed to being fixed like user name and groups. -->
<!-- There are two dynamic authorities, the ower of a node and the owner -->

View File

@ -3,7 +3,7 @@
path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="5000"
eternal="false"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"

View File

@ -1,151 +0,0 @@
/*
* 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.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* A cache implementation that maintains items up to a threshold size. If the threshold size is reached
* it begins removing old items during get() calls as they time-out after a specified timeout value.
* <p>
* If the threshold value is not reached, the items are not removed unless specifically requested with
* a call to remove() or clear().
* <p>
* If the max size value is reached then no more items are added to the cache until some are removed
* either explicitly or automically via timed-out values.
*
* @author Kevin Roast
*/
public class AutoExpireCache<K extends Serializable, V extends Serializable> implements SimpleCache<K, V>
{
// TODO: configure these values via Spring
private final long TIMEDIFF = 1000000L * 1000L * 60L * 5L; // 5 mins in nano-seconds
private final int MAXSIZE = 4096; // maximum size of the cache
private final float THRESHOLD = 0.75f; // before we start removing items
private int maxsize = MAXSIZE;
private float threshold = THRESHOLD;
private Map<Object, CacheItem<K, V>> cache = new HashMap<Object, CacheItem<K, V>>(maxsize, 1.0f);
/**
* Default constructor
*/
public AutoExpireCache()
{
}
/**
* Constructor
*/
public AutoExpireCache(int maxsize, float threshold)
{
maxsize = maxsize;
threshold = threshold;
}
/**
* @see org.alfresco.repo.cache.SimpleCache#get(K)
*/
public synchronized V get(K key)
{
CacheItem<K, V> wrapper = cache.get(key);
if (wrapper != null)
{
// we cache values till a specific timeout then remove them
// this also gives other values a chance to get added if we are nearing the max size
if (cache.size() > (maxsize * threshold) &&
System.nanoTime() > (wrapper.Timestamp + TIMEDIFF))
{
//if (log.isDebugEnabled())
// log.debug("*****Removing timedout key: " + key);
cache.remove(key);
wrapper = null;
}
}
return wrapper != null ? wrapper.Value : null;
}
/**
* @see org.alfresco.repo.cache.SimpleCache#put(K, V)
*/
public synchronized void put(K key, V value)
{
if (cache.size() < maxsize)
{
//if (log.isDebugEnabled())
// log.debug("***Adding new key: " + key + " size: " + cache.size());
cache.put(key, new CacheItem(key, value));
}
}
/**
* @see org.alfresco.repo.cache.SimpleCache#remove(K)
*/
public synchronized void remove(K key)
{
cache.remove(key);
}
/**
* @see org.alfresco.repo.cache.SimpleCache#clear()
*/
public void clear()
{
// better to let the GC deal with removing the old map in one shot
// rather than try to clear each slot slowly using clear()
cache = new HashMap<Object, CacheItem<K, V>>(maxsize, 1.0f);
}
/**
* @see org.alfresco.repo.cache.SimpleCache#contains(K)
*/
public synchronized boolean contains(K key)
{
return false;
}
/**
* Simple wrapper class for a cache item.
* Stores a timestamp of when the item was added so it can be purged from the cache later.
*/
private static class CacheItem<K, V>
{
CacheItem(K key, V value)
{
this.key = key;
Value = value;
Timestamp = System.nanoTime();
}
public int hashCode()
{
return key.hashCode();
}
public boolean equals(Object o)
{
if (o instanceof CacheItem == false) return false;
return key.equals( ((CacheItem)o).key );
}
private K key;
long Timestamp;
V Value;
}
}

View File

@ -20,7 +20,7 @@ import java.io.Serializable;
import java.util.HashMap;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.cache.AutoExpireCache;
import org.alfresco.repo.cache.SimpleCache;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
@ -40,7 +40,7 @@ public class OwnableServiceImpl implements OwnableService, InitializingBean
private AuthenticationService authenticationService;
private static AutoExpireCache<NodeRef, String> ownerCache = new AutoExpireCache<NodeRef, String>(1024, 0.75f);
private SimpleCache<NodeRef, String> nodeOwnerCache;
public OwnableServiceImpl()
{
@ -59,16 +59,27 @@ public class OwnableServiceImpl implements OwnableService, InitializingBean
this.authenticationService = authenticationService;
}
/**
* @param ownerCache a transactionally-safe cache of node owners
*/
public void setNodeOwnerCache(SimpleCache<NodeRef, String> ownerCache)
{
this.nodeOwnerCache = ownerCache;
}
public void afterPropertiesSet() throws Exception
{
if (nodeService == null)
{
throw new IllegalArgumentException("A node service must be set");
throw new IllegalArgumentException("Property 'nodeService' has not been set");
}
if (authenticationService == null)
{
throw new IllegalArgumentException("An authentication service must be set");
throw new IllegalArgumentException("Property 'authenticationService' has not been set");
}
if (nodeOwnerCache == null)
{
throw new IllegalArgumentException("Property 'nodeOwnerCache' has not been set");
}
}
@ -76,7 +87,7 @@ public class OwnableServiceImpl implements OwnableService, InitializingBean
public String getOwner(NodeRef nodeRef)
{
String userName = ownerCache.get(nodeRef);
String userName = nodeOwnerCache.get(nodeRef);
if (userName == null)
{
@ -89,7 +100,7 @@ public class OwnableServiceImpl implements OwnableService, InitializingBean
{
userName = DefaultTypeConverter.INSTANCE.convert(String.class, nodeService.getProperty(nodeRef, ContentModel.PROP_CREATOR));
}
ownerCache.put(nodeRef, userName);
nodeOwnerCache.put(nodeRef, userName);
}
return userName;
@ -97,8 +108,6 @@ public class OwnableServiceImpl implements OwnableService, InitializingBean
public void setOwner(NodeRef nodeRef, String userName)
{
ownerCache.remove(nodeRef);
if (!nodeService.hasAspect(nodeRef, ContentModel.ASPECT_OWNABLE))
{
HashMap<QName, Serializable> properties = new HashMap<QName, Serializable>(1, 1.0f);
@ -109,6 +118,7 @@ public class OwnableServiceImpl implements OwnableService, InitializingBean
{
nodeService.setProperty(nodeRef, ContentModel.PROP_OWNER, userName);
}
nodeOwnerCache.put(nodeRef, userName);
}
public void takeOwnership(NodeRef nodeRef)

View File

@ -25,7 +25,7 @@ import net.sf.acegisecurity.Authentication;
import net.sf.acegisecurity.GrantedAuthority;
import net.sf.acegisecurity.providers.dao.User;
import org.alfresco.repo.cache.AutoExpireCache;
import org.alfresco.repo.cache.SimpleCache;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.security.permissions.DynamicAuthority;
import org.alfresco.repo.security.permissions.NodePermissionEntry;
@ -62,8 +62,8 @@ public class PermissionServiceImpl implements PermissionServiceSPI, Initializing
private static Log log = LogFactory.getLog(PermissionServiceImpl.class);
private static AutoExpireCache<Serializable, AccessStatus> accessCache = new AutoExpireCache<Serializable, AccessStatus>(4096, 0.75f);
/** a transactionally-safe cache to be injected */
private SimpleCache<Serializable, AccessStatus> accessCache;
/*
* Access to the model
@ -147,33 +147,46 @@ public class PermissionServiceImpl implements PermissionServiceSPI, Initializing
this.dynamicAuthorities = dynamicAuthorities;
}
/**
* Set the permissions access cache.
*
* @param accessCache a transactionally safe cache
*/
public void setAccessCache(SimpleCache<Serializable, AccessStatus> accessCache)
{
this.accessCache = accessCache;
}
public void afterPropertiesSet() throws Exception
{
if (dictionaryService == null)
{
throw new IllegalArgumentException("There must be a dictionary service");
throw new IllegalArgumentException("Property 'dictionaryService' has not been set");
}
if (modelDAO == null)
{
throw new IllegalArgumentException("There must be a permission model service");
throw new IllegalArgumentException("Property 'modelDAO' has not been set");
}
if (nodeService == null)
{
throw new IllegalArgumentException("There must be a node service");
throw new IllegalArgumentException("Property 'nodeService' has not been set");
}
if (permissionsDAO == null)
{
throw new IllegalArgumentException("There must be a permission dao");
throw new IllegalArgumentException("Property 'permissionsDAO' has not been set");
}
if (authenticationComponent == null)
{
throw new IllegalArgumentException("There must be an authentication component");
throw new IllegalArgumentException("Property 'authenticationComponent' has not been set");
}
if(authorityService == null)
{
throw new IllegalArgumentException("There must be an authority service");
throw new IllegalArgumentException("Property 'authorityService' has not been set");
}
if (accessCache == null)
{
throw new IllegalArgumentException("Property 'accessCache' has not been set");
}
}
//
@ -405,9 +418,9 @@ public class PermissionServiceImpl implements PermissionServiceSPI, Initializing
* dynamically so they must all be used) the NodeRef ID and the permission reference itself.
* This gives a unique key for each permission test.
*/
static Serializable generateKey(Set auths, NodeRef ref, PermissionReference perm)
static Serializable generateKey(Set<String> auths, NodeRef ref, PermissionReference perm)
{
HashSet key = new HashSet(auths);
HashSet<Serializable> key = new HashSet<Serializable>(auths);
key.add(ref.getId());
key.add(perm.toString());
return key;