RESTful, resource-oriented caching now available in ehcache-server

I have just released ehcache-server-0.3, which includes a fully functional RESTful, resource-oriented implementation. The standalone-server has also been updated to 0.3.

Cache API

CacheManager Resource Operations
OPTIONS /
Lists the methods supported by the CacheManager resource
GET /
Lists the Caches in the CacheManager.
Cache Resource Operations
OPTIONS /{cache}/
Lists the methods supported by the Cache resource
GET /{cache}
Lists the elements in the cache.
PUT /{cache}
Creates a Cache using the defaultCache configuration.
DELETE / {cache}
Deletes the Cache.
Element Resource Operations
OPTIONS /{cache}/{element}
Lists the methods supported by the Element resource
HEAD /{cache}/{element}
Retrieves the same metadata a GET would receive returned as HTTP headers. There is no body returned.
GET /{cache}/{element}
Gets the element.
HEAD /{cache}/{element}
Gets the element’s metadata.
PUT /{cache}/{element}
Puts and element into the Cache.
DELETE / {cache}/{element}
Deletes the element from the cache.

Simple Client Example

There is no client for the RESTful cache server. You can use any HTTP client library in any language.
Put XML into cache
String xmlDocument = “\n” +
\n” +
Say goodnight,\n” +
“Gracie.
\n” +
Goodnight, \n” +
“Gracie.
\n” +
\n” +
“;
HttpUtil.put(“http://localhost:8080/ehcache/rest/sampleCache2/2”, “application/xml”,
new ByteArrayInputStream(xmlDocument.getBytes()));
The server will respond with 201 for success.
Get XML example
HttpURLConnection urlConnection = HttpUtil.get(“http://localhost:8080/ehcache/rest/sampleCache2/2”);
assertEquals(200, urlConnection.getResponseCode());
assertTrue(urlConnection.getContentType().matches(“application/xml”));
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = documentBuilder.parse(urlConnection.getInputStream());
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = “/oldjoke/burns”;
Node node = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
assertEquals(“burns”, node.getNodeName());
The server will respond with 200 for success and provide the data along with Content Type of “application/xml”
Get with wget example
This one shows how to do a get with the Unix utiiity wget:
wget -S http://localhost:8080/ehcache/rest/sampleCache2/1
–15:11:54– http://localhost:8080/ehcache/rest/sampleCache2/1
=> `1′
Resolving localhost… 127.0.0.1, ::1
Connecting to localhost|127.0.0.1|:8080… connected.
HTTP request sent, awaiting response…
HTTP/1.1 200 OK
X-Powered-By: Servlet/2.5
Server: GlassFish/v3
Expires: Sun Aug 17 17:12:55 EST 292278994
Last-Modified: Sun, 03 Aug 2008 05:07:50 GMT
ETag: “1217740070067”
Content-Type: application/octet-stream
Content-Length: 512
Date: Sun, 03 Aug 2008 05:11:54 GMT
Connection: Keep-Alive
Length: 512 [*/*]
100%[=============================================================================================================>] 512 –.–K/s
15:11:54 (8.28 MB/s) – `1′ saved [512/512]
Ehcache server fully supports wget, including the site mirroring capabilities.

Getting Started

Download the server here: http://sourceforge.net/project/showfiles.php?group_id=93232
Read the manual here: http://ehcache.sourceforge.net/documentation/cache_server.html
Check out the project and work your way through the tests. These show how to put and get different content with different mime types and how to use HEAD, PUT, DELETE, OPTIONS and GET. Ehcache server supports conditional GET and follows normal HTTP caching conventions.
I hope to follow up with some articles on how to use ehcache-server with different languages.

Published
Categorized as Java

By Greg Luck

As Terracotta’s CTO, Greg (@gregrluck) is entrusted with understanding market and technology forces and the business drivers that impact Terracotta’s product innovation and customer success. He helps shape company and technology strategy and designs many of the features in Terracotta’s products. Greg came to Terracotta on the acquisition of the popular caching project Ehcache which he founded in 2003. Prior to joining Terracotta, Greg served as Chief Architect at Australian online travel giant Wotif.com. He also served as a lead consultant for ThoughtWorks on accounts in the United States and Australia, was CIO at Virgin Blue, Tempo Services, Stamford Hotels and Resorts and Australian Resorts and spent seven years as a Chartered Accountant in KPMG’s small business and insolvency divisions. He is a regular speaker at conferences and contributor of articles to the technical press.