com.opensymphony.oscache.base
Class CacheEntry

java.lang.Object
  extended by com.opensymphony.oscache.base.CacheEntry
All Implemented Interfaces:
Serializable

public class CacheEntry
extends Object
implements Serializable

A CacheEntry instance represents one entry in the cache. It holds the object that is being cached, along with a host of information about that entry such as the cache key, the time it was cached, whether the entry has been flushed or not and the groups it belongs to.

Version:
$Revision: 388 $
Author:
Mike Cannon-Brookes, Todd Gochenour, Francois Beauregard
See Also:
Serialized Form

Field Summary
private  Object content
          The actual content that is being cached.
private  long created
          The time this entry was created.
private  Set groups
          The set of cache groups that this cache entry belongs to, if any.
static int INDEFINITE_EXPIRY
          Specifying this as the refresh period for the needsRefresh(int) method will ensure an entry does not become stale until it is either explicitly flushed or a custom refresh policy causes the entry to expire.
private  String key
          The unique cache key for this entry
private  long lastUpdate
          The time this emtry was last updated.
private static byte NOT_YET
          Default initialization value for the creation time and the last update time.
private  EntryRefreshPolicy policy
          The entry refresh policy object to use for this cache entry.
private  boolean wasFlushed
          true if this entry was flushed
 
Constructor Summary
CacheEntry(String key)
          Construct a new CacheEntry using the key provided.
CacheEntry(String key, EntryRefreshPolicy policy)
          Construct a CacheEntry.
CacheEntry(String key, EntryRefreshPolicy policy, String[] groups)
          Construct a CacheEntry.
 
Method Summary
 void flush()
          Flush the entry from cache.
 Object getContent()
          Get the cached content from this CacheEntry.
 long getCreated()
          Get the date this CacheEntry was created.
 Set getGroups()
          Gets the cache groups that this cache entry belongs to.
 String getKey()
          Get the key of this CacheEntry
 long getLastUpdate()
          Get the date this CacheEntry was last updated.
 int getSize()
          Get the size of the cache entry in bytes (roughly).
 boolean isNew()
          Indicates whether this CacheEntry is a freshly created one and has not yet been assigned content or placed in a cache.
 boolean needsRefresh(int refreshPeriod)
          Check if this CacheEntry needs to be refreshed.
 void setContent(Object value)
          Sets the actual content that is being cached.
 void setGroups(Collection groups)
          Sets the cache groups for this entry
 void setGroups(String[] groups)
          Sets the cache groups for this entry.
 void setLastUpdate(long update)
          Set the date this CacheEntry was last updated.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NOT_YET

private static final byte NOT_YET
Default initialization value for the creation time and the last update time. This is a placeholder that indicates the value has not been set yet.

See Also:
Constant Field Values

INDEFINITE_EXPIRY

public static final int INDEFINITE_EXPIRY
Specifying this as the refresh period for the needsRefresh(int) method will ensure an entry does not become stale until it is either explicitly flushed or a custom refresh policy causes the entry to expire.

See Also:
Constant Field Values

policy

private EntryRefreshPolicy policy
The entry refresh policy object to use for this cache entry. This is optional.


content

private Object content
The actual content that is being cached. Wherever possible this object should be serializable. This allows PersistenceListeners to serialize the cache entries to disk or database.


groups

private Set groups
The set of cache groups that this cache entry belongs to, if any.


key

private String key
The unique cache key for this entry


wasFlushed

private boolean wasFlushed
true if this entry was flushed


created

private long created
The time this entry was created.


lastUpdate

private long lastUpdate
The time this emtry was last updated.

Constructor Detail

CacheEntry

public CacheEntry(String key)
Construct a new CacheEntry using the key provided.

Parameters:
key - The key of this CacheEntry

CacheEntry

public CacheEntry(String key,
                  EntryRefreshPolicy policy)
Construct a CacheEntry.

Parameters:
key - The unique key for this CacheEntry.
policy - Object that implements refresh policy logic. This parameter is optional.

CacheEntry

public CacheEntry(String key,
                  EntryRefreshPolicy policy,
                  String[] groups)
Construct a CacheEntry.

Parameters:
key - The unique key for this CacheEntry.
policy - The object that implements the refresh policy logic. This parameter is optional.
groups - The groups that this CacheEntry belongs to. This parameter is optional.
Method Detail

setContent

public void setContent(Object value)
Sets the actual content that is being cached. Wherever possible this object should be Serializable, however it is not an absolute requirement when using a memory-only cache. Being Serializable allows PersistenceListeners to serialize the cache entries to disk or database.

Parameters:
value - The content to store in this CacheEntry.

getContent

public Object getContent()
Get the cached content from this CacheEntry.

Returns:
The content of this CacheEntry.

getCreated

public long getCreated()
Get the date this CacheEntry was created.

Returns:
The date this CacheEntry was created.

setGroups

public void setGroups(String[] groups)
Sets the cache groups for this entry.

Parameters:
groups - A string array containing all the group names

setGroups

public void setGroups(Collection groups)
Sets the cache groups for this entry

Parameters:
groups - A collection containing all the group names

getGroups

public Set getGroups()
Gets the cache groups that this cache entry belongs to. These returned groups should be treated as immuatable.

Returns:
A set containing the names of all the groups that this cache entry belongs to.

getKey

public String getKey()
Get the key of this CacheEntry

Returns:
The key of this CacheEntry

setLastUpdate

public void setLastUpdate(long update)
Set the date this CacheEntry was last updated.

Parameters:
update - The time (in milliseconds) this CacheEntry was last updated.

getLastUpdate

public long getLastUpdate()
Get the date this CacheEntry was last updated.

Returns:
The date this CacheEntry was last updated.

isNew

public boolean isNew()
Indicates whether this CacheEntry is a freshly created one and has not yet been assigned content or placed in a cache.

Returns:
true if this entry is newly created

getSize

public int getSize()
Get the size of the cache entry in bytes (roughly).

Currently this method only handles Strings and ResponseContent objects.

Returns:
The approximate size of the entry in bytes, or -1 if the size could not be estimated.

flush

public void flush()
Flush the entry from cache. note that flushing the cache doesn't actually remove the cache contents it just tells the CacheEntry that it needs a refresh next time it is asked this is so that the content is still there for a .


needsRefresh

public boolean needsRefresh(int refreshPeriod)
Check if this CacheEntry needs to be refreshed.

Parameters:
refreshPeriod - The period of refresh (in seconds). Passing in INDEFINITE_EXPIRY will result in the content never becoming stale unless it is explicitly flushed, or expired by a custom EntryRefreshPolicy. Passing in 0 will always result in a refresh being required.
Returns:
Whether or not this CacheEntry needs refreshing.


Copyright © 2011 OpenSymphony. All Rights Reserved.