Blog

How to place and retrieve objects from the server-side cache

by on March 10, 2015

Backendless provides a very powerful and easy-to-use API to work with your server-side cache. The API is multi-platform, which 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 objects/primitive values. Additionally, the caching system can be strongly typed – a client app can specify the type that 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:

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


    Synchronous sample:

    -(void)addToCacheSync {
        @try {
            Person *p = [Person new];
            p.name = @"James Bond";
            p.age = @42;
            [backendless.cache put:@"myobject" object:p];
            NSLog(@"Person has been placed into cache (SYNC)");
        }
        @catch (Fault *fault) {
            NSLog(@"Server reported an error(SYNC): %@", fault);
        }
    }
    -(void)getFromCacheSync {
        @try {
            Person *person = [backendless.cache get:@"myobject"];
            NSLog(@"Received object from cache (SYNC): name - %@, age - %@", person.name, person.age);
        }
        @catch (Fault *fault) {
            NSLog(@"Server reported an error(SYNC): %@", fault);
        }
    }

    Asynchronous sample:

    -(void)addToCacheAsync {
        Person *p = [Person new];
        p.name = @"James Bond";
        p.age = @42;
        [backendless.cache
         put:@"myobject" object:p
         response:^(id o) {
             NSLog(@"Person has been placed into cache (ASYNC): %@", o);
         }
         error:^(Fault *fault) {
             NSLog(@"Server reported an error (ASYNC): %@", fault);
         }];
    }
    -(void)getFromCacheAsync {
        [backendless.cache
         get:@"myobject"
         response:^(id o) {
             Person *person = (Person *)o;
             NSLog(@"Received object from cache (ASYNC): name - %@, age - %@", person.name, person.age);
         }
         error:^(Fault *fault) {
             NSLog(@"Server reported an error (ASYNC): %@", fault);
         }];
    }


    Synchronous sample:

        func addToCacheSync() {
            Types.try({ () -> Void in
                var p = Person()
                p.name = "James Bond";
                p.age = 42;
                self.backendless.cache.put("myobject", object: p)
                println("Person has been placed into cache (SYNC)")
                },
                catch: { (exception) -> Void in
                    println("Server reported an error (SYNC): \(exception as! Fault)")
                }
            )
        }
        func getFromCacheSync() {
            Types.try({ () -> Void in
                var person = self.backendless.cache.get("myobject") as! Person
                println("Received object from cache (SYNC): name - \(person.name), age - \(person.age)")
                },
                catch: { (exception) -> Void in
                    println("Server reported an error (SYNC): \(exception as! Fault)")
                }
            )
        }

    Asynchronous sample:

    func addToCacheAsync() {
            var p = Person()
            p.name = "James Bond";
            p.age = 42;
            backendless.cache.put(
                "myobject",
                object: p,
                response: { (var o : AnyObject!) -> () in
                    println("Person has been placed into cache (ASYNC)")
                },
                error: { (var fault : Fault!) -> () in
                    println("Server reported an error (ASYNC): \(fault)")
                }
            )
        }
        func getFromCacheAsync() {
            backendless.cache.get(
                "myobject",
                response: { (var o : AnyObject!) -> () in
                    var person = o as! Person
                    println("Received object from cache (ASYNC): name - \(person.name), age - \(person.age)")
                },
                error: { (var fault : Fault!) -> () in
                    println("Server reported an error (ASYNC): \(fault)")
                }
            )
        }
    }

    The Person class used in the example is a basic POJO. Notice it does not implement the Serializable interface (it could if you needed it to do so), nor does it need to implement any other interface:

      package com.mbaas.sample;
      public class Person
      {
          public String name;
          public int age;
      }

      #import "Person.h"
      @implementation Person
      @end

      import Foundation
      class Person : NSObject {
          var name : String?
          var age : Int = 0
      }

      The Caching API supports other useful features, like checking if an object exists in cache, or extending an objects life in cache. These and other features will be reviewed in future posts of this blog series.

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