About Markus Eisele

Markus is a principal technology consultant working for msg systems ag in Germany. Markus is a software architect, developer and consultant. He also writes for IT magazines. Markus is speaking at different conferences about his favorite topics. He is also part of the Java EE 7 expert group.

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.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

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


4 + = thirteen



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.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books