Java Temporary Caching API - Test-driving the Early Draft Review RI

Markus Eisele
2
It was known as "The Neverending Story". The JSR kicked of 11 and a half year ago and passed the JSR Review Ballot on 06 Mar, 2001. If you ever wondered what it takes to get a fancy low JSR number in the hundreds: That is the secret. Unlike in the German fantasy novel by Michael Ende this was not about people's lack of imagination but about resources, political discussions and finally about licensing. But let's forget about the past and move to what is there since yesterday. Note that this material was uploaded to the JCP in February but was delayed while the legal complications of having two companies as shared spec leads got sorted out. That is done and will not be an issue going forward in the process.

What is it all about?
Caching is known for dramatically speeding up applications. Those typically use temporary data which is expensive to create but has a long lifetime during which it can be re-used. This specification standardizes caching of Java objects in a way that allows an efficient implementation, and removes from the programmer the burden of implementing cache expiration, mutual exclusion, spooling, and cache consistency.
It is designed to work with both Java SE and Java EE. For the later it still is not ensured, that it will be included in upcoming EE 7 release but the EG is working hard on it and needs your feedback.

How do I get my hands on it?
That is easy. All the needed artifacts are in maven central already. Let's build a very simple sample for you to get you started. Fire up NetBeans and create a new Maven Java Application. Name it whatever you like (e.g. cachingdemo, open the pom.xml and add the following two dependencies to it:
<dependency>
            <groupId>javax.cache</groupId>
            <artifactId>cache-api</artifactId>
            <version>0.5</version>
        </dependency>

        <dependency>
            <groupId>javax.cache.implementation</groupId>
            <artifactId>cache-ri-impl</artifactId>
            <version>0.5</version>
    </dependency>
And if you are there, change the junit version to 4.8.2.
Refactor the AppTest to utilize the new junit:
package net.eisele.samples.cachingdemo;

import org.junit.Test;

/**
 * Simple Cache Test
 */
public class AppTest {

    @Test
    public void testApp() {
    }
}
All set. To make this simple, I'm going to add some caching features in the test-case.

The Basic Concepts
From a design point of view, the basic concepts are a CacheManager that holds and controls a collection of Caches. Caches have entries. The basic API can be thought of map-­like. Like a map, data is stored as values by key. You can put values, get values and remove values. But it does not have high network cost map-like methods such as keySet() and values(). And generally it prefers zero or low cost return types. So while Map has V put(K key, V value) javax.cache.Cache has void put(K key, V value).
 
 // Name for the cache
        String cacheName = "myfearsCache";
        // Create a cache using a CacheBuilder
        Cache<Integer, String> cache = Caching.getCacheManager().<Integer, String>createCacheBuilder(cacheName).build();
        // define a value
        String value1 = "Markus";
        // define a key
        Integer key = 1;
        //put to the cache
        cache.put(key, value1);
        // get from the cache
        String value2 = cache.get(key);
        //compare values
        assertEquals(value1, value2);
// remove from the cache
        cache.remove(key);
        // ceck if removed
        assertNull(cache.get(key));

Things to come
This basically is all that is possible at the moment. Going down the road with subsequent releases you should be able to:
- Integrate with Spring and CDI via @Annotations
- Use CacheEventListener
- Work with Transactions
The EG is actively searching for feedback on the available stuff. So, if you can get your hands on it, give it a try and let the EG know what you think!

Links and Reading
JCP page: JSR 107: JCACHE - Java Temporary Caching API
Group Mailing List http://groups.google.com/group/jsr107
Log issues in the Issue Tracker https://github.com/jsr107/jsr107spec/issues
A very simple demo https://github.com/jsr107/demo
ehcache-jcache - an implementation of the 0.5 specification https://github.com/jsr107/ehcache-jcache
Tags:

Post a Comment

2Comments

  1. Hello,

    > This basically is all that is possible at the moment.

    JCache annotations (with CDI) worked fine for me with version 0.4 (with JBoss 7.x - there's a bug if you want to use them with WAS 8 though).

    If I remember correctly, you'll have to include cache-annotations-ri-cdi in your pom.xml.

    ReplyDelete
    Replies
    1. Hi,

      thanks for the head-ups. I tried the 0.5 and failed with both the RI and ehcache-jcache ... so ...
      Might need to try a 0.6-snapshot again.

      - M

      Delete
Post a Comment