Skip to content

Extending the life of object in cache

There are two way to extend object's life in cache - relative timeframe and fixed timeframe. With the relative timeframe a period of time is added to the timestamp of the call to determine the new expiration time. The fixed timestamp approach sets the timestamp when the object must expire from cache. All methods are available via Backendless.Cache.[methodname] accessor:

// **************************************************************
// synchronous methods
// **************************************************************
// relative time extension
public void ExpireIn( String key, int seconds );

// fixed time extension
public void ExpireAt( String key, int timestamp )

// **************************************************************
// asynchronous methods
// **************************************************************
// relative time extension
public void ExpireIn( String key, int seconds, AsyncCallback<Object> callback )

// fixed time extension
public void ExpireAt( String key, int timestamp, AsyncCallback<Object> callback )

where:

Argument                Description
key identifies the object to extend the life of in cache.
seconds number of seconds to extend the life of object in cache by. Must be a value between 1 and 7200 (2 hours).
timestamp a timestamp in milliseconds when the object should expire and removed from cache. The difference between timestamp and the current time must be equal or less than 7200000 milliseconds (2 hours).
callback the callback used for asynchronous calls to deliver result or fault to the calling program.

Example

AsyncCallback<Object> callback = new AsyncCallback<Object>(
result =>
{
  System.Console.WriteLine( "[ASYNC] object life has been extended" );
},
fault =>
{
  System.Console.WriteLine( "Error - " + fault );
} );

// extend object's life in cache asynchronously by 1200 seconds - 20 minutes
Backendless.Cache.ExpireIn( "firstorder", 1200, callback );

// extend object's life in cache synchronously by 1200 seconds - 20 minutes

long timestamp = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond + 72000000;
Backendless.Cache.ExpireAt( "firstorder", timestamp );