org.zkoss.util
Class ThreadLocalCache

java.lang.Object
  extended by org.zkoss.util.ThreadLocalCache
All Implemented Interfaces:
Cache

public class ThreadLocalCache
extends java.lang.Object
implements Cache

A cache that resides on the thread local memory. The performance is excellent since no need to synchronize the access. However, it takes more memory since each thread has its own map. In addition, it might cause hot re-deployment failed to free old classes if the map stores the references of the classes loaded by the Web Application class loader.

Since:
3.0.0
Author:
tomyeh

Field Summary
 
Fields inherited from interface org.zkoss.util.Cache
DEFAULT_LIFETIME, DEFAULT_MAX_SIZE
 
Constructor Summary
ThreadLocalCache()
          Constructs a thread-local cache with the default setting: max size=128 and lifetime=30minutes.
ThreadLocalCache(int maxSize, int lifetime)
          Constucts a thread-local cache with the specified max size and the lifetime.
 
Method Summary
 void clear()
          Clears all objects being cached.
 boolean containsKey(java.lang.Object key)
          Returns whether the specified key is stored.
 void copyTo(java.util.Map map)
          Puts all object cached in the current thread to the specifed map.
 java.lang.Object get(java.lang.Object key)
          Returns the object of the specified key, or null if not found.
 int getLifetime()
          Returns the minimal lifetime, unit=milliseconds.
 int getMaxSize()
          Returns the maximal allowed size.
 boolean isEmpty()
          Returns whether the cache for the current thread is empty.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Stores an object to the cache.
 java.lang.Object remove(java.lang.Object key)
          Removes an object from the cache.
 void setLifetime(int lifetime)
          Sets the minimal lifetime.
 void setMaxSize(int maxsize)
          Sets the maximal allowed size.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadLocalCache

public ThreadLocalCache(int maxSize,
                        int lifetime)
Constucts a thread-local cache with the specified max size and the lifetime.


ThreadLocalCache

public ThreadLocalCache()
Constructs a thread-local cache with the default setting: max size=128 and lifetime=30minutes.

Method Detail

isEmpty

public boolean isEmpty()
Returns whether the cache for the current thread is empty.


copyTo

public void copyTo(java.util.Map map)
Puts all object cached in the current thread to the specifed map.


containsKey

public boolean containsKey(java.lang.Object key)
Description copied from interface: Cache
Returns whether the specified key is stored.

Specified by:
containsKey in interface Cache

get

public java.lang.Object get(java.lang.Object key)
Description copied from interface: Cache
Returns the object of the specified key, or null if not found.

Specified by:
get in interface Cache

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Description copied from interface: Cache
Stores an object to the cache.

Specified by:
put in interface Cache
Returns:
the previous value of the same, or null if no such value

remove

public java.lang.Object remove(java.lang.Object key)
Description copied from interface: Cache
Removes an object from the cache.

Specified by:
remove in interface Cache
Returns:
the object if found.

clear

public void clear()
Description copied from interface: Cache
Clears all objects being cached.

Specified by:
clear in interface Cache

getLifetime

public int getLifetime()
Description copied from interface: Cache
Returns the minimal lifetime, unit=milliseconds. An mapping won't be removed by GC unless the minimal lifetime or the maximal allowed size exceeds.

Specified by:
getLifetime in interface Cache
See Also:
Cache.getMaxSize()

setLifetime

public void setLifetime(int lifetime)
Description copied from interface: Cache
Sets the minimal lifetime. Default: Cache.DEFAULT_LIFETIME.

Specified by:
setLifetime in interface Cache
Parameters:
lifetime - the lifetime, unit=milliseconds; if non-posive, they will be removed immediately.
See Also:
Cache.getLifetime()

getMaxSize

public int getMaxSize()
Returns the maximal allowed size.

Defalut: 128 (it is smaller than most cache since this cache one per thread). An mapping won't be removed by GC unless the minimal lifetime or the maximal allowed size exceeds.

Specified by:
getMaxSize in interface Cache
See Also:
getLifetime()

setMaxSize

public void setMaxSize(int maxsize)
Description copied from interface: Cache
Sets the maximal allowed size.

Specified by:
setMaxSize in interface Cache
See Also:
Cache.getMaxSize()


Copyright © 2005-2009 Potix Corporation. All Rights Reserved. SourceForge.net Logo