Markus Eisele

About Markus Eisele

Markus is a Developer Advocate at Red Hat and focuses on JBoss Middleware. He is working with Java EE servers from different vendors since more than 14 years and talks about his favorite topics around Java EE on conferences all over the world. Follow him on Twitter @myfear.

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

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
 

Reference: Java Temporary Caching API – Test-driving the Early Draft Review RI from our JCG partner Markus Eisele at the Enterprise Software Development with Java blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "Java Temporary Caching API – Test-driving the Early Draft Review RI"

  1. I have to say I haven’t yet looked at the spec yet. Will there be support for the time an object will be kept inside the cache or a maximum size of the cache?

Leave a Reply


six − 4 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close