2015/07/26 - Apache DirectMemory has been retired.

For more information, please explore the Attic.

DirectMemory Server

Apache DirectMemory has a server implementation (a servlet) to provide you a way to store your project remotely and to share those cached objects with various applications.

Server side and client side (Java only) are provided.

The exchange are based on http(s) exchange with the following implementations/format:

  • JSON format
  • "binary" format: parameters are send via http headers
  • text/plain format: you can send a text value(json object, xml etc..), the server will serialize the content and store it for you.

Exchange formats

We simply use HTTP method names to resolve the action to proceed and depends on the Accept or Content-Type headers the format will be different :

  • GET to retrieve content ${webPath}/${key}
  • DELETE to delete content ${webPath}/${key}
  • PUT/POST to add some content in cache ${webPath}/${key}

HTTP Methods

PUT/POST

PUT/POST are used for adding/updating content in cache.
Note: if the content was not put in cache, you will receive a 204 (Not Content)

GET

GET is used to retrieve content from the cache.
If not content is found for the key, you will receive the http code 204 (No content)

DELETE

DELETE is used to removed content from cache
Note: if the entry was not available in the server you will receive a 204 (Not Content)

Http Headers

see various exchange type for dedicated http headers

Common http headers:

  • X-DirectMemory-SerializeSize: returns bytes number stored on server side.

JSON Exchange

JSON PUT Content

JSON Request to put content in cache Content-Type: application/json

  {"expiresIn":123,
    "cacheContent":"rO0ABXNydmEvbGFuZy9TdHJpbmc7eHB0AAhCb3JkZWF1eA=="}
          
To put this content in DirectMemory Cache Server, just use a PUT request on path ${webPath}/DirectMemoryServlet/${key}.

JSON GET Content

Use a GET request on ${webPath}/DirectMemoryServlet/${key} and you will receive the JSON response:

            {"key":"foo","cacheContent":"Zm9vIGJhcg=="}
          

JSON DELETE Content

Use a DELETE request on ${webPath}/DirectMemoryServlet/${key}.

Binary Exchange

Binary PUT Content

PUT Request to put content in cache Content-Type: application/x-java-serialized-object.
To put this content in DirectMemory Cache Server, just use a PUT request on path ${webPath}/DirectMemoryServlet/${key}.
ExpiresIn value can be set with http header: X-DirectMemory-ExpiresIn
The http request body will contains the serialized object value.

Binary GET Content

Use a GET request on ${webPath}/DirectMemoryServlet/${key} and you will receive the binary form of the object.

Binary DELETE Content

Use a DELETE request on ${webPath}/DirectMemoryServlet/${key}.

text/plain Exchange

text/plain PUT Content

PUT Request to put content in cache Content-Type: text/plain.
To put this content in DirectMemory Cache Server, just use a PUT request on path ${webPath}/DirectMemoryServlet/${key}.
ExpiresIn value can be set with http header: X-DirectMemory-ExpiresIn
The http request body will contains a String value which will be serialized on server side and stored in directMemory.
You can specify the Serializer to use with the http header: "X-DirectMemory-Serializer" (must contains the full class name).

text/plain GET Content

Use a GET request on ${webPath}/DirectMemoryServlet/${key} and you will receive the stored String.

text/plain DELETE Content

Use a DELETE request on ${webPath}/DirectMemoryServlet/${key}.

Java Client API

Client Configuration

Before using the client api, you must configure it using the following pattern:

        DirectMemoryClientConfiguration configuration =
            new DirectMemoryClientConfiguration()
                .setHost( "localhost" )
                .setPort( port )
                .setHttpPath( "/direct-memory/DirectMemoryServlet" )
                .setSerializer( SerializerFactory.createNewSerializer() )
                .setHttpClientClassName( httpClientClassName )
                .setExchangeType( getExchangeType() );

        client = DirectMemoryClientBuilder.newBuilder( configuration ).buildClient();

        // or

        client = DirectMemoryClientBuilder
            .newBuilder()
            .toHost( "localhost" )
            .onPort( port )
            .toHttpPath( "/direct-memory/DirectMemoryServlet" )
            .withSerializer( SerializerFactory.createNewSerializer() )
            .forExchangeType( getExchangeType() )
            .withHttpClientClassName( httpClientClassName )
            .buildClient();

Here you have a configured client. Have a look at DirectMemoryServerClientConfiguration javadoc for advanced options.

PUT Content

With the Java client is something like:

        Wine bordeaux = new Wine( "Bordeaux", "very great wine" );
        assertTrue( client.put( new DirectMemoryRequest<Wine>( "bordeaux", bordeaux ) ).isStored() );

GET Content

With the Java api:

        DirectMemoryRequest rq = new DirectMemoryRequest( "bordeaux", Wine.class );

        DirectMemoryResponse<Wine> response = client.retrieve( rq );

        assertTrue( response.isFound() );
        Wine wine = response.getResponse();
You must check the method (response.isFound()) if true retrieve the object with: Wine wine = response.getResponse();

DELETE Content

With the Java api:

        DirectMemoryResponse deleteResponse = client.delete( new DirectMemoryRequest<Wine>( "bordeaux" ) );
        assertTrue( deleteResponse.isDeleted() );