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:
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 :
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 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)
JSON Request to put content in cache Content-Type: application/json
{"expiresIn":123, "cacheContent":"rO0ABXNydmEvbGFuZy9TdHJpbmc7eHB0AAhCb3JkZWF1eA=="}
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.
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).
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();
With the Java client is something like:
Wine bordeaux = new Wine( "Bordeaux", "very great wine" ); assertTrue( client.put( new DirectMemoryRequest<Wine>( "bordeaux", bordeaux ) ).isStored() );
With the Java api:
DirectMemoryRequest rq = new DirectMemoryRequest( "bordeaux", Wine.class ); DirectMemoryResponse<Wine> response = client.retrieve( rq ); assertTrue( response.isFound() ); Wine wine = response.getResponse();