O'Reilly logo

Java EE 8 High Performance by Romain Manni-Bucau

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The cache key

Most cache APIs are very close to a map in terms of their behavior—you can put some data with put(key, value) and retrieve it back with the same key through a get(key) invocation.

This means that a poor man's cache can be implemented with ConcurrentMap of the JRE:

private final ConcurrentMap<Long, Optional<Quote>> quoteCache = new ConcurrentHashMap<>();@GET@Path("{id}")public JsonQuote findById(@PathParam("id") final long id) {    return quoteCache.computeIfAbsent(id, identifier ->     quoteService.findById(identifier))            .map(this::convertQuote)            .orElseThrow(() -> new WebApplicationException(Response.Status.NO_CONTENT));}

In this implementation, we wrapped the database access in a concurrent map access, which triggers the database ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required