org.apache.jcs.access
Class PartitionedCacheAccess<K extends Serializable,V extends Serializable>

java.lang.Object
  extended by org.apache.jcs.utils.props.AbstractPropertyContainer
      extended by org.apache.jcs.access.PartitionedCacheAccess<K,V>
All Implemented Interfaces:
ICacheAccess<K,V>

public class PartitionedCacheAccess<K extends Serializable,V extends Serializable>
extends AbstractPropertyContainer
implements ICacheAccess<K,V>

TODO: Add new methods that will allow you to provide a partition indicator for all major calls. Add an interface as well.

This handles dividing puts and gets.

There are two required properties.

  1. .numberOfPartitions
  2. .partitionRegionNamePrefix
System properties will override values in the properties file.

We use a JCS region name for each partition that looks like this: partitionRegionNamePrefix + "_" + patitionNuber. The number is ) indexed based.

Author:
Aaron Smuts

Constructor Summary
PartitionedCacheAccess()
          Sets default properties heading and group.
 
Method Summary
protected  void ensureInit()
          Initialize if we haven't already.
 int freeMemoryElements(int numberToFree)
          Calls free on each partition.
 V get(K key)
          Gets the object for the key from the desired partition.
 ICompositeCacheAttributes getCacheAttributes()
          Gets the ICompositeCacheAttributes of the cache region
 ICacheElement<K,V> getCacheElement(K key)
          Gets the ICacheElement (the wrapped object) for the key from the desired partition.
 Map<K,ICacheElement<K,V>> getCacheElements(Set<K> names)
          This is a getMultiple.
 IElementAttributes getElementAttributes()
          GetElementAttributes will return an attribute object describing the current attributes associated with the object name.
 IElementAttributes getElementAttributes(K key)
          This is no more efficient than simply getting the cache element.
 Map<K,V> getMatching(String pattern)
          This is tricky.
 Map<K,ICacheElement<K,V>> getMatchingCacheElements(String pattern)
          This is tricky.
protected  int getNumberOfPartitions()
           
 long getNumericValueForKey(K key)
          This can be overridden for special purposes.
protected  int getPartitionNumberForKey(K key)
          This expects a numeric key.
protected  String getPartitionRegionNamePrefix()
           
protected  ICacheAccess<K,V>[] getPartitions()
           
protected  String getPropertyForName(String propertyName, boolean required)
          Checks the system properties before the properties.
protected  void handleProperties()
          Loads in the needed configuration settings.
protected  void initialize()
          Use the partition prefix and the number of partitions to get JCS regions.
 void put(K key, V object)
          Puts the value into the appropriate cache partition.
 void put(K key, V object, IElementAttributes attr)
          Puts the value into the appropriate cache partition.
 void putSafe(K key, V object)
          Puts in cache if an item does not exist with the name in that region.
 void remove()
          Calls remove on all partitions.
 void remove(K key)
          Removes the item from the appropriate partition.
 void resetElementAttributes(IElementAttributes attributes)
          Resets the default element attributes on all partitions.
 void resetElementAttributes(K key, IElementAttributes attributes)
          Resets the attributes for this item.
 void setCacheAttributes(ICompositeCacheAttributes cattr)
          Sets the attributes on all the partitions.
protected  void setNumberOfPartitions(int numberOfPartitions)
           
protected  void setPartitionRegionNamePrefix(String partitionRegionNamePrefix)
           
protected  void setPartitions(ICacheAccess<K,V>[] partitions)
           
 
Methods inherited from class org.apache.jcs.utils.props.AbstractPropertyContainer
ensureProperties, getProperties, getPropertiesFactory, getPropertiesGroup, getPropertiesHeading, initializeProperties, setProperties, setPropertiesFactory, setPropertiesGroup, setPropertiesHeading
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PartitionedCacheAccess

public PartitionedCacheAccess()
Sets default properties heading and group.

Method Detail

put

public void put(K key,
                V object)
         throws CacheException
Puts the value into the appropriate cache partition.

Specified by:
put in interface ICacheAccess<K extends Serializable,V extends Serializable>
Parameters:
key - key
object - object
Throws:
CacheException - on configuration problem

putSafe

public void putSafe(K key,
                    V object)
             throws CacheException
Puts in cache if an item does not exist with the name in that region.

Specified by:
putSafe in interface ICacheAccess<K extends Serializable,V extends Serializable>
Parameters:
key -
object -
Throws:
CacheException

put

public void put(K key,
                V object,
                IElementAttributes attr)
         throws CacheException
Puts the value into the appropriate cache partition.

Specified by:
put in interface ICacheAccess<K extends Serializable,V extends Serializable>
Parameters:
key - key
object - object
attr -
Throws:
CacheException - on configuration problem

get

public V get(K key)
Gets the object for the key from the desired partition.

Specified by:
get in interface ICacheAccess<K extends Serializable,V extends Serializable>
Parameters:
key - key
Returns:
result, null if not found.

getCacheElement

public ICacheElement<K,V> getCacheElement(K key)
Gets the ICacheElement (the wrapped object) for the key from the desired partition.

Specified by:
getCacheElement in interface ICacheAccess<K extends Serializable,V extends Serializable>
Parameters:
key - key
Returns:
result, null if not found.

getCacheElements

public Map<K,ICacheElement<K,V>> getCacheElements(Set<K> names)
This is a getMultiple. We try to group the keys so that we make as few calls as needed.

Specified by:
getCacheElements in interface ICacheAccess<K extends Serializable,V extends Serializable>
Parameters:
names -
Returns:
Map of keys to ICacheElement

getMatching

public Map<K,V> getMatching(String pattern)
This is tricky. Do we need to get from all the partitions?

If this interface took an object, we could use the hashcode to determine the partition. Then we could use the toString for the pattern.

Specified by:
getMatching in interface ICacheAccess<K extends Serializable,V extends Serializable>
Parameters:
pattern -
Returns:
HashMap key to value

getMatchingCacheElements

public Map<K,ICacheElement<K,V>> getMatchingCacheElements(String pattern)
This is tricky. Do we need to get from all the partitions?

Specified by:
getMatchingCacheElements in interface ICacheAccess<K extends Serializable,V extends Serializable>
Parameters:
pattern -
Returns:
HashMap key to ICacheElement

remove

public void remove()
            throws CacheException
Calls remove on all partitions. This gets translated into a removeAll call.

Specified by:
remove in interface ICacheAccess<K extends Serializable,V extends Serializable>
Throws:
CacheException

remove

public void remove(K key)
            throws CacheException
Removes the item from the appropriate partition.

Specified by:
remove in interface ICacheAccess<K extends Serializable,V extends Serializable>
Parameters:
key -
Throws:
CacheException

freeMemoryElements

public int freeMemoryElements(int numberToFree)
                       throws CacheException
Calls free on each partition.

Specified by:
freeMemoryElements in interface ICacheAccess<K extends Serializable,V extends Serializable>
Parameters:
numberToFree -
Returns:
number removed
Throws:
CacheException

getCacheAttributes

public ICompositeCacheAttributes getCacheAttributes()
Description copied from interface: ICacheAccess
Gets the ICompositeCacheAttributes of the cache region

Specified by:
getCacheAttributes in interface ICacheAccess<K extends Serializable,V extends Serializable>
Returns:
ICompositeCacheAttributes from the first partition.

getElementAttributes

public IElementAttributes getElementAttributes()
                                        throws CacheException
Description copied from interface: ICacheAccess
GetElementAttributes will return an attribute object describing the current attributes associated with the object name. If no name parameter is available, the attributes for the region will be returned. The name object must override the Object.equals and Object.hashCode methods.

Specified by:
getElementAttributes in interface ICacheAccess<K extends Serializable,V extends Serializable>
Returns:
IElementAttributes from the first partition.
Throws:
CacheException

getElementAttributes

public IElementAttributes getElementAttributes(K key)
                                        throws CacheException
This is no more efficient than simply getting the cache element.

Specified by:
getElementAttributes in interface ICacheAccess<K extends Serializable,V extends Serializable>
Parameters:
key -
Returns:
IElementAttributes
Throws:
CacheException

resetElementAttributes

public void resetElementAttributes(IElementAttributes attributes)
                            throws CacheException
Resets the default element attributes on all partitions. This does not change items that are already in the cache.

Specified by:
resetElementAttributes in interface ICacheAccess<K extends Serializable,V extends Serializable>
Parameters:
attributes -
Throws:
CacheException

resetElementAttributes

public void resetElementAttributes(K key,
                                   IElementAttributes attributes)
                            throws CacheException
Resets the attributes for this item. This has the same effect as an update, in most cases. None of the auxiliaries are optimized to do this more efficiently than a simply update.

Specified by:
resetElementAttributes in interface ICacheAccess<K extends Serializable,V extends Serializable>
Parameters:
key -
attributes -
Throws:
CacheException

setCacheAttributes

public void setCacheAttributes(ICompositeCacheAttributes cattr)
Sets the attributes on all the partitions.

Specified by:
setCacheAttributes in interface ICacheAccess<K extends Serializable,V extends Serializable>
Parameters:
cattr -

getPartitionNumberForKey

protected int getPartitionNumberForKey(K key)
This expects a numeric key. If the key cannot be converted into a number, we will return 0. TODO we could md5 it or get the hashcode.

We determine the partition by taking the mod of the number of partitions.

Parameters:
key - key
Returns:
the partition number.

getNumericValueForKey

public long getNumericValueForKey(K key)
This can be overridden for special purposes.

Parameters:
key - key
Returns:
long

ensureInit

protected void ensureInit()
                   throws ConfigurationException
Initialize if we haven't already.

Throws:
ConfigurationException - on configuration problem

initialize

protected void initialize()
                   throws ConfigurationException
Use the partition prefix and the number of partitions to get JCS regions.

Throws:
ConfigurationException - on configuration problem

handleProperties

protected void handleProperties()
                         throws ConfigurationException
Loads in the needed configuration settings. System properties are checked first. A system property will override local property value.

Loads the following JCS Cache specific properties:

Specified by:
handleProperties in class AbstractPropertyContainer
Throws:
ConfigurationException - on configuration problem

getPropertyForName

protected String getPropertyForName(String propertyName,
                                    boolean required)
                             throws ConfigurationException
Checks the system properties before the properties.

Parameters:
propertyName - name
required - is it required?
Returns:
the property value if one is found
Throws:
ConfigurationException - thrown if it is required and not found.

setNumberOfPartitions

protected void setNumberOfPartitions(int numberOfPartitions)
Parameters:
numberOfPartitions - The numberOfPartitions to set.

getNumberOfPartitions

protected int getNumberOfPartitions()
Returns:
Returns the numberOfPartitions.

setPartitionRegionNamePrefix

protected void setPartitionRegionNamePrefix(String partitionRegionNamePrefix)
Parameters:
partitionRegionNamePrefix - The partitionRegionNamePrefix to set.

getPartitionRegionNamePrefix

protected String getPartitionRegionNamePrefix()
Returns:
Returns the partitionRegionNamePrefix.

setPartitions

protected void setPartitions(ICacheAccess<K,V>[] partitions)
Parameters:
partitions - The partitions to set.

getPartitions

protected ICacheAccess<K,V>[] getPartitions()
Returns:
Returns the partitions.


Copyright © 2002-2013 The Apache Software Foundation. All Rights Reserved.