com.j256.ormlite.dao
Class ReferenceObjectCache

java.lang.Object
  extended by com.j256.ormlite.dao.ReferenceObjectCache
All Implemented Interfaces:
ObjectCache

public class ReferenceObjectCache
extends Object
implements ObjectCache

Cache for ORMLite which stores objects with a WeakReference or SoftReference to them. Java Garbage Collection can then free these objects if no one has a "strong" reference to the object (weak) or if it runs out of memory (soft).

Author:
graywatson

Constructor Summary
ReferenceObjectCache(boolean useWeak)
           
 
Method Summary
<T> void
cleanNullReferences(Class<T> clazz)
          Run through the map and remove any references that have been null'd out by the GC.
<T> void
cleanNullReferencesAll()
          Run through all maps and remove any references that have been null'd out by the GC.
<T> void
clear(Class<T> clazz)
          Remove all entries from the cache of a certain class.
 void clearAll()
          Remove all entries from the cache of all classes.
<T,ID> T
get(Class<T> clazz, ID id)
          Lookup in the cache for an object of a certain class that has a certain id.
static ReferenceObjectCache makeSoftCache()
          Create and return an object cache using SoftReference.
static ReferenceObjectCache makeWeakCache()
          Create and return an object cache using WeakReference.
<T,ID> void
put(Class<T> clazz, ID id, T data)
          Put an object in the cache that has a certain class and id.
<T,ID> void
remove(Class<T> clazz, ID id)
          Delete from the cache an object of a certain class that has a certain id.
<T> int
size(Class<T> clazz)
          Return the number of elements in the cache.
 int sizeAll()
          Return the number of elements in all of the caches.
<T,ID> T
updateId(Class<T> clazz, ID oldId, ID newId)
          Change the id in the cache for an object of a certain class from an old-id to a new-id.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReferenceObjectCache

public ReferenceObjectCache(boolean useWeak)
Parameters:
useWeak - Set to true if you want the cache to use WeakReference. If false then the cache will use SoftReference.
Method Detail

makeWeakCache

public static ReferenceObjectCache makeWeakCache()
Create and return an object cache using WeakReference.


makeSoftCache

public static ReferenceObjectCache makeSoftCache()
Create and return an object cache using SoftReference.


get

public <T,ID> T get(Class<T> clazz,
                    ID id)
Description copied from interface: ObjectCache
Lookup in the cache for an object of a certain class that has a certain id.

Specified by:
get in interface ObjectCache
Returns:
The found object or null if none.

put

public <T,ID> void put(Class<T> clazz,
                       ID id,
                       T data)
Description copied from interface: ObjectCache
Put an object in the cache that has a certain class and id.

Specified by:
put in interface ObjectCache

clear

public <T> void clear(Class<T> clazz)
Description copied from interface: ObjectCache
Remove all entries from the cache of a certain class.

Specified by:
clear in interface ObjectCache

clearAll

public void clearAll()
Description copied from interface: ObjectCache
Remove all entries from the cache of all classes.

Specified by:
clearAll in interface ObjectCache

remove

public <T,ID> void remove(Class<T> clazz,
                          ID id)
Description copied from interface: ObjectCache
Delete from the cache an object of a certain class that has a certain id.

Specified by:
remove in interface ObjectCache

updateId

public <T,ID> T updateId(Class<T> clazz,
                         ID oldId,
                         ID newId)
Description copied from interface: ObjectCache
Change the id in the cache for an object of a certain class from an old-id to a new-id.

Specified by:
updateId in interface ObjectCache

size

public <T> int size(Class<T> clazz)
Description copied from interface: ObjectCache
Return the number of elements in the cache.

Specified by:
size in interface ObjectCache

sizeAll

public int sizeAll()
Description copied from interface: ObjectCache
Return the number of elements in all of the caches.

Specified by:
sizeAll in interface ObjectCache

cleanNullReferences

public <T> void cleanNullReferences(Class<T> clazz)
Run through the map and remove any references that have been null'd out by the GC.


cleanNullReferencesAll

public <T> void cleanNullReferencesAll()
Run through all maps and remove any references that have been null'd out by the GC.



This content is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.