public class

CacheEntry

extends Object
java.lang.Object
   ↳ com.neomades.content.cache.CacheEntry
Known Direct Subclasses

Class Overview

Data and metadata for an entry returned by the cache.
CacheEntry will describe the cache policy thanks to the methods isExpired() and refreshNeeded().

In this implementation, isExpired() and refreshNeeded() are both based on the time to live of the data in the server found in the response header. This enables the cache to be updated automatically only when the data are declared as invalid by the server. This implies some configuration of the server to send the good header with the request response. If not, the data will be considered as invalid each time a request is sent and they will be downloaded again.
To fully understand this behavior, read the javadoc of setTtl(long), setSoftTtl(long), isExpired() and refreshNeeded().

Summary

Public Constructors
CacheEntry()
Creates a CacheEntry.
Public Methods
byte[] getData()
Returns the raw data in the cache.
Object getDeserializedData()
Returns the deserialized data in the cache.
String getEtag()
Gets the value associated with the ETag header found when the data was saved in the cache.
Map<StringString> getResponseHeaders()
Returns all the headers returned by the response that were used to download the data.
long getServerDate()
Returns the date returned by the server in the header Date when the response with the data was returned (in ms).
long getSoftTTL()
Returns the time when the data will require an update.
long getTTL()
Returns the validity time in ms of the cache data.
boolean isExpired()
Returns if the entry is expired.
boolean refreshNeeded()
Returns true if a refresh is needed from the original data source.
void setData(byte[] data)
Inserts a byte array of data inside the cache entry in order to get it.
void setDeserializedData(Object data)
Inserts a deserialized object inside the cache entry in order to get it.
void setEtag(String etag)
Saves the value associated with the ETag header found in the response that get the data.
void setResponseHeaders(Map<StringString> responseHeaders)
Saves all the headers returned by the response that were used to download the data.
void setServerDate(long serverDate)
Saves the date returned by the server in the header Date when the response with the data is returned (in ms).
void setSoftTtl(long softTtl)
Sets the time when the data will require an update.
void setTtl(long ttl)
Saves the validity time in ms of the cache data.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public CacheEntry ()

Creates a CacheEntry.

Public Methods

public byte[] getData ()

Returns the raw data in the cache.

Returns
  • the raw data in the cache

public Object getDeserializedData ()

Returns the deserialized data in the cache.

Returns
  • a deserialized object

public String getEtag ()

Gets the value associated with the ETag header found when the data was saved in the cache. If the header was not sent by the server, the value is null.

Returns
  • the value associated with the ETag header found when the data was saved in the cache

public Map<StringString> getResponseHeaders ()

Returns all the headers returned by the response that were used to download the data.

Returns
  • the headers returned by the response that were used to download the data

public long getServerDate ()

Returns the date returned by the server in the header Date when the response with the data was returned (in ms). If the header was not sent by the server, the value is 0.

Returns
  • the date returned by the server in the header Date when the response with the data was returned (in ms)

public long getSoftTTL ()

Returns the time when the data will require an update. Contrary to getTTL(), when expired, the next request will trigger an update of the data but while it is not done, the previous data will be used. The data are invalid but we can still use them while they are not updated. This value is used by refreshNeeded() to determine if a "soft" update is required.

Returns
  • the time when the data will require an update

public long getTTL ()

Returns the validity time in ms of the cache data. When expired, the data will be considered as invalid and the next request will trigger a download and the cache will be updated with new data. This value is used by isExpired() to determine if the cache is invalid.

Returns
  • the validity time in ms of the cache data

public boolean isExpired ()

Returns if the entry is expired. An entry becomes expired when its time to live is outdated (it is older than the current time). To understand how the time to live is calculated, refer to setTtl(long). When a ContentQuery is sent, if the entry is expired, the cache is ignored, and the resource will be requested again through the network.

Returns
  • true if the entry is expired.

public boolean refreshNeeded ()

Returns true if a refresh is needed from the original data source. A refresh is needed when the "soft" time to live is outdated (it is older than the current time). To understand how the soft time to live is calculated, refer to setSoftTtl(long). When a ContentQuery is posted, if a refresh is needed, the entry is sent as an intermediate response, but the cached resource is requested again through the network to refresh it. If the refresh is not needed, the entry is sent as a final response.

In this implementation of CacheEntry there is no difference between setTtl(long) and setSoftTtl(long). Consequently, isExpired() and refreshNeeded() will be true or false at the same time. Since isExpired() is called before refreshNeeded() in the internal implementation, it is always a "strong" refresh that will be done.
To adapt this behavior, isExpired() and refreshNeeded() must be overriden in a specific CacheEntry()

Returns
  • true if a refresh is needed from the original data source

public void setData (byte[] data)

Inserts a byte array of data inside the cache entry in order to get it. This method can be used in get(String) to save data in the CacheEntry.

Parameters
data byte array of data to set in the CacheEntry

public void setDeserializedData (Object data)

Inserts a deserialized object inside the cache entry in order to get it. This method can be used in get(String) to save data in the CacheEntry.

Parameters
data data to set in the CacheEntry

public void setEtag (String etag)

Saves the value associated with the ETag header found in the response that get the data.

The ETag value will be added to the headers of the request that will be used to update the cache as the value of a If-None-Match header. Doing this, the server will send the data only if the ETag has changed for the requested resource.

Parameters
etag value associated with the ETag header

public void setResponseHeaders (Map<StringString> responseHeaders)

Saves all the headers returned by the response that were used to download the data.

Parameters
responseHeaders the headers returned by the response that were used to download the data

public void setServerDate (long serverDate)

Saves the date returned by the server in the header Date when the response with the data is returned (in ms).

The server date will be added to the headers of the request that will be used to update the cache as the value of a If-Modified-Since header (only if the value is not 0). Doing this, if the server understands the header, it will send a response only if the data were updated.

Parameters
serverDate date of the server when the data were sent (in ms)

public void setSoftTtl (long softTtl)

Sets the time when the data will require an update. Contrary to setTtl(long), when expired, the next request will trigger an update of the data but while it is not done, the previous data will be used. The data are invalid but we can still use them while they are not updated. This value is used by refreshNeeded() to determine if a "soft" update is required.

In this implementation of CacheEntry there is no difference between setTtl(long) and setSoftTtl(long). Consequently, isExpired() and refreshNeeded() will be true or false at the same time. Since isExpired() is called before refreshNeeded() in the internal implementation, it is always a "strong" refresh that will be done.
To adapt this behavior, isExpired() and refreshNeeded() must be overriden in a specific CacheEntry()

Parameters
softTtl the time when the data will require an update
See Also

public void setTtl (long ttl)

Saves the validity time in ms of the cache data. When expired, the data will be considered as invalid and the next request will trigger a download and the cache will be updated with new data. This value is used by isExpired() to determine if the cache is invalid.

By default, this value is set when parsing the response header. If a Cache-Control, max-age=xxx is found in the header, the ttl value will be: System.currentTimeMillis() + maxAge * 1000;.
If there is no Cache-Control, max-age=xxx header, an Expires header will be searched in the response headers. If found the time to live will be equal to System.currentTimeMillis() + (serverExpires - serverDate); where serverExpires is the value of the Expires header and serverDate is the value of the Date header.
If there is no header, its value will be 0 and the cache will be updated each time a request is triggered.

Parameters
ttl the validity time in ms of the cache data
See Also