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 secomds );

// fixed time extension
  public void expireAt( String key, Date date );
  public void expireAt( String key, long timestamp );

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

// fixed time extension
public void expireAt( String key, Date date, AsyncCallback<Object> callback );
public void expireAt( String key, long 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>()
    {
      @Override
          public void handleResponse( Object result )
          {
             Log.i( "MYAPP", "[ASYNC] object life has been extended" );
          }

          @Override
          public void handleFault( BackendlessFault backendlessFault )
          {
            Log.e( "MYAPP", "Error - " + backendlessFault.getMessage() );
          }
    };

    // 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 = System.currentTimeMillis() + 1200000;
    Backendless.Cache.expireAt( "firstorder", timestamp );

Codeless Reference

caching_codeless_extending_object_life_1

where:

Argument                Description
key name Key assigned to the object to identify it in cache. The key is used to extend the life of an object in the cache.
in seconds Number of seconds to extend the life of an object in cache by. Must be a value between 1 and 7200 (2 hours).

This operation does not return a value.

Consider the expiration time(expireAt column) of the following key-value pair stored in cache:

caching_codeless_putting_data_into_cache_3

The example below extends the life of the object associated with the "orderName" key by 7200 seconds(2 hours)

caching_codeless_extending_object_life_2

The operation has successfully extended the life of the object. The value in the expireAt column indicates the corresponding change (before: 04/10/2023 16:57:23) -> (after: 04/10/2023 18:10:35)

caching_codeless_extending_object_life_3