Skip to content

Checking if key exists in cache

This API request checks if an object exists in cache. If object is present in cache, the method returns true, otherwise - false. All methods are available via Backendless.Cache.[methodname] accessor:

// synchronous method
public Boolean contains( String key )

// asynchronous method
public void contains( String key, AsyncCallback<Boolean> callback )

where:

Argument                Description
key identifies the object to check in cache.
callback the callback used for asynchronous calls to deliver result or fault to the calling program

Example

AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>()
    {
      @Override
          public void handleResponse( Boolean result )
          {
             Log.i( "MYAPP", "[ASYNC] object exists in cache - " + result );
          }

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

    // get object from cache asynchronously
    Backendless.Cache.contains( "firstorder", callback );

    // get object from cache synchronously
    boolean objectExists = Backendless.Cache.contains( "firstorder" );
    Log.i( "MYAPP", "[SYNC] object exists in cache - "  + objectExists );