Backendless Support
 

Placing and retrieving objects from the server-side cache

Backendless provides a very powerful, but extremely easy-to-use API to work with server-side cache. 

The API is a multi-platform - it means clients written in different languages can exchange data using the centralized server-side cache storage. The caching API can accept any arbitrary object, a primitive value or an array of object/primitive values. Additionally, the caching system can be strongly typed - a client app can specify the type which should be used to deserialize the object.

The example below places an arbitrary object into cache. The object is mapped to a name which identifies it in the context of the application. Then the object is retrieved using the same name.

                                       For Objective-C and Swift implementations CLICK HERE

Asynchronous sample (Android and Plain Java):
private static void addToCacheAsync()
{
 Person p = new Person();
 p.name = "James Bond";
 p.age = 42;
 Backendless.Cache.put( "myobject", p, new AsyncCallback<Object>()
 {
 @Override
 public void handleResponse( Object o )
 {
 System.out.println( "Object has been placed into cache" );
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( "Server reported an error " + backendlessFault.getMessage() );
 }
 } );
}
private static void getFromCacheAsync()
{
 Backendless.Cache.get( "myobject", new AsyncCallback<Person>()
 {
 @Override
 public void handleResponse( Person person )
 {
 System.out.println( "Received object from cache. Person's name - " + person.name + ", age - " + person.age );
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( "Server reported an error " + backendlessFault.getMessage() );
 }
 } );
}
Synchronous sample (Plain Java only):
private static void addToCachetSync()
{
 Person p = new Person();
 p.name = "James Bond";
 p.age = 42;
 Backendless.Cache.put( "myobject", p );
}
public static void getFromCacheSync()
{
 Person person = Backendless.Cache.get( "myobject", Person.class );
 System.out.println( "Received object from cache. Person's name - " + person.name + ", age - " + person.age );
}

The Person class used in the example is a basic POJO.

NOTICE: it does not implement the Serializable interface (it could if a developer needed it to do so), nor it needs to implement any other interface:
package com.mbaas.sample;
public class Person
{
 public String name;
 public int age;
}

The Caching API supports other useful features, like checking if an object exists in cache, or extending objects life in cache. 

Review the Backendless documentation to learn more about the Caching API.

Is article helpful?