BEA Systems, Inc.

com.beasys.commerce.foundation.cache
Class CacheImpl

java.lang.Object
  |
  +--com.beasys.commerce.foundation.cache.CacheImpl

public class CacheImpl
extends java.lang.Object
implements Cache

Implementation class for a Cache. This is a native Java object that is implemented as a Singleton created by a CacheFactory object. Internally, it is a Hashtable with Objects as keys and CacheEntry objects which are Objects wrapped in doubly linked list functionality as Values. Since the Cache is of fixed size, there is no functionality to actively remove objects from the cache - they are removed passively by expiring.

TODO: This object is heavily synchronized. If performance standards dictate, the synchronization should be made more granular.

CAUTION: This data structure does not itself provide immutability of its values. In other words, it returns references to its actual objects. It is recommended that those values protect themselves from change unless that change is actually desired - remember these objects will eventually expire from the Cache.


Inner classes inherited from class java.util.Map
java.util.Map.Entry
 
Constructor Summary
CacheImpl(int newMaxEntries, long itemTtl, boolean enabled)
          Create a Cache with an initial size, timeout, and enabled value.
 
Method Summary
 void addEntry(java.lang.Object key, java.lang.Object value)
          Deprecated. use put.
 void addEntry(java.lang.Object key, java.lang.Object value, long ttl)
          Deprecated. use put
 void clear()
          Clear.
 boolean containsEntry(java.lang.Object key)
          Deprecated. use containsKey
 boolean containsKey(java.lang.Object aKey)
          Determines if an entry exists in the cache.
 boolean containsValue(java.lang.Object aValue)
          Contains value.
 void disable()
          Disable and flush this Cache.
 void enable()
          Enable this Cache.
 java.util.Set entrySet()
          entrySet defined from Map interface.
 java.lang.Object fetchEntry(java.lang.Object key)
          Deprecated. use get
 java.lang.Object get(java.lang.Object key)
          Lookup an entry in the Cache if it is enabled.
 int getEntryCount()
          Deprecated.  
 long getHitCount()
          Get hit count.
 int getHitRate()
          Returns the integer percentage of requests which found a non-expired value.
 int getMaxEntries()
          Get max entries.
 long getMissCount()
          Get miss count.
 long getTtl()
          Get current Time To Live value assigned to new entries.
 boolean isEmpty()
          Is empty.
 boolean isEnabled()
          Is enabled.
 boolean isFull()
          Is full.
 java.util.Set keySet()
          Implemented from map interface.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Add a new entry to the Cache if it is not already there.
 java.lang.Object put(java.lang.Object key, java.lang.Object value, long ttl)
          Add a new entry to the Cache, replace if already there.
 void putAll(java.util.Map aMap)
          Put all the values of a map.
 java.lang.Object remove(java.lang.Object key)
          Removes a single entry from the Cache if it exists.
 void remove(java.lang.Object[] keys)
          Convenience method to iterate through an array of keys and remove all of them.
 void removeAllEntries()
          Deprecated. use clear
 void removeEntries(java.lang.Object[] keys)
          Deprecated. use remove
 void removeEntry(java.lang.Object key)
          Deprecated. use remove.
 void resetStats()
          Reset stats.
 void setMaxEntries(int newMaxEntries)
          Size or resize this Cache.
 void setTtl(long newTtl)
          Set the Time to Live assigned to all entries in this cache.
 int size()
          Size of the cache.
 java.util.Collection values()
          Values of the map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CacheImpl

public CacheImpl(int newMaxEntries,
                 long itemTtl,
                 boolean enabled)
Create a Cache with an initial size, timeout, and enabled value.
Method Detail

getTtl

public long getTtl()
Get current Time To Live value assigned to new entries.
Specified by:
getTtl in interface Cache

addEntry

public void addEntry(java.lang.Object key,
                     java.lang.Object value)
Deprecated. use put.

Add a new entry to the Cache if it is not already there. Kick out the Least Recently Used entry if it is at capacity. Default TTL.
Specified by:
addEntry in interface Cache
Tags copied from interface: Cache
Parameters:
key -  
value -  

addEntry

public void addEntry(java.lang.Object key,
                     java.lang.Object value,
                     long ttl)
Deprecated. use put

Add a new entry to the Cache if it is not already there. Kick out the Least Recently Used entry if it is at capacity. Specify TTL.
Specified by:
addEntry in interface Cache
Tags copied from interface: Cache
Parameters:
key -  
value -  
ttl -  

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Add a new entry to the Cache if it is not already there. Kick out the Least Recently Used entry if it is at capacity. Default TTL.
Parameters:
key -  
value -  

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value,
                            long ttl)
Add a new entry to the Cache, replace if already there. Kick out the Least Recently Used entry if it is at capacity. Specify TTL.
Specified by:
put in interface Cache
Parameters:
key -  
value -  
ttl -  

enable

public void enable()
Enable this Cache.
Specified by:
enable in interface Cache

disable

public void disable()
Disable and flush this Cache. Will cause fetch, add, and remove to do nothing.
Specified by:
disable in interface Cache

containsEntry

public boolean containsEntry(java.lang.Object key)
Deprecated. use containsKey

Determines if an entry exists in the cache. This is useful when nulls might be cached, as fetchEntry will return null if the entry's value is null, or if it does not exist.
Specified by:
containsEntry in interface Cache
Tags copied from interface: Cache
Parameters:
key -  
Returns:
boolean

fetchEntry

public java.lang.Object fetchEntry(java.lang.Object key)
Deprecated. use get

Lookup an entry in the Cache if it is enabled. Returns null if the entry does not exist or has expired. Otherwise, it returns the Value associated with the key and moves the entry to the top of the LRU list (making it the most recently used object.
Specified by:
fetchEntry in interface Cache
Tags copied from interface: Cache
Parameters:
key -  
Returns:
Object

get

public java.lang.Object get(java.lang.Object key)
Lookup an entry in the Cache if it is enabled. Returns null if the entry does not exist or has expired. Otherwise, it returns the Value associated with the key and moves the entry to the top of the LRU list (making it the most recently used object.
Parameters:
aKey -  
Returns:
Object

removeEntries

public void removeEntries(java.lang.Object[] keys)
Deprecated. use remove

Convenience method to iterate through an array of keys and remove all of them.

remove

public void remove(java.lang.Object[] keys)
Convenience method to iterate through an array of keys and remove all of them.
Specified by:
remove in interface Cache
Parameters:
keys[] -  

removeEntry

public void removeEntry(java.lang.Object key)
Deprecated. use remove.

Removes a single entry from the Cache if it exists.
Specified by:
removeEntry in interface Cache
Tags copied from interface: Cache
Parameters:
key -  

remove

public java.lang.Object remove(java.lang.Object key)
Removes a single entry from the Cache if it exists.
Parameters:
key -  

isFull

public boolean isFull()
Is full.
Specified by:
isFull in interface Cache
Returns:
boolean

isEmpty

public boolean isEmpty()
Is empty.
Returns:
boolean

resetStats

public void resetStats()
Reset stats.
Specified by:
resetStats in interface Cache

getHitRate

public int getHitRate()
Returns the integer percentage of requests which found a non-expired value.
Specified by:
getHitRate in interface Cache

getEntryCount

public int getEntryCount()
Deprecated.  

Current size of the Cache.
Specified by:
getEntryCount in interface Cache

removeAllEntries

public void removeAllEntries()
Deprecated. use clear

Flush this Cache.
Specified by:
removeAllEntries in interface Cache

setMaxEntries

public void setMaxEntries(int newMaxEntries)
Size or resize this Cache.
Specified by:
setMaxEntries in interface Cache

getMaxEntries

public int getMaxEntries()
Get max entries.
Specified by:
getMaxEntries in interface Cache
Returns:
int

setTtl

public void setTtl(long newTtl)
Set the Time to Live assigned to all entries in this cache.
Specified by:
setTtl in interface Cache

getHitCount

public long getHitCount()
Get hit count.
Specified by:
getHitCount in interface Cache
Returns:
long

getMissCount

public long getMissCount()
Get miss count.
Specified by:
getMissCount in interface Cache
Returns:
long

isEnabled

public boolean isEnabled()
Description copied from interface: Cache
Is enabled.
Specified by:
isEnabled in interface Cache
Tags copied from interface: Cache
Returns:
boolean

size

public int size()
Size of the cache. This returns the size of the internal map. This may or may not match the size of the collection returned from the values method. The values method will strip out all expired values.
Returns:
int

containsKey

public boolean containsKey(java.lang.Object aKey)
Determines if an entry exists in the cache. This is usefule when nulls might be cached, as fetchEntry will return null if the entry's value is null, or if it does not exist.
Parameters:
aKey -  
Returns:
boolean

containsValue

public boolean containsValue(java.lang.Object aValue)
Contains value.
Parameters:
aValue -  
Returns:
boolean

clear

public void clear()
Clear.

values

public java.util.Collection values()
Values of the map. Map interface. 1. Check to see if the key is valid (hasn't expired) 2. If it hasn't expired add it to the linked list, even if it is null.
Returns:
Collection

keySet

public java.util.Set keySet()
Implemented from map interface. 1. Check to see if the key is valid (hasn't expired) 2. If it hasn't expired add the key to the set, even if it is null.
Returns:
Set

entrySet

public java.util.Set entrySet()
entrySet defined from Map interface. 1. Check to see if the key is valid (hasn't expired) 2. If it hasn't expired add the value to the set, even if it is null.
Returns:
Set

putAll

public void putAll(java.util.Map aMap)
Put all the values of a map. Make sure and use the put api so that we get the ttls.
Parameters:
aMap -  

BEA Systems, Inc.

Copyright © 2000 BEA Systems, Inc. All Rights Reserved