Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lightweight Java Object cache API [closed]

Tags:

java

caching

Question

I'm looking for a Java in-memory object caching API. Any recommendations? What solutions have you used in the past?

Current

Right now, I'm just using a Map:

Map cache = new HashMap<String, Object>(); cache.put("key", value); 

Requirements

I need to extend the cache to include basic features like:

  • Max size
  • Time to live

However, I don't need more sophisticated features like:

  • Access from multiple processes (caching server)
  • Persistence (to disk)

Suggestions

In-Memory caching:

  • Guava CacheBuilder - active development. See this presentation.
  • LRUMap - Config via API. No TTL. Not purpose built for caching.
  • whirlycache - XML config. Mailing list. Last updated 2006.
  • cache4j - XML config. Documentation in Russian. Last updated 2006.

Enterprise caching:

  • JCS - Properties config. Extensive documentation.
  • Ehcache - XML config. Extensive documentation. By far the most popular according to Google hits.
like image 278
Chase Seibert Avatar asked Oct 23 '08 17:10

Chase Seibert


People also ask

What is ehcache used for?

Ehcache is a standards-based caching API that is used by Integration Server. Caching enables an application to fetch frequently used data from memory (or other nearby resource) rather than having to retrieve it from a database or other back-end system each time the data is needed.

How do you use cache in Java?

By storing frequently accessed or expensive-to-create objects in memory or on disk, the Java Object Cache eliminates the need to repeatedly create and load information within a Java program. The Java Object Cache retrieves content faster and greatly reduces the load on application servers.

What is in memory cache in Java?

But what is “Cache?” A cache is an area of local memory that holds a copy of frequently accessed data that is otherwise expensive to get or compute. Examples of such data include a result of a query to a database, a disk file or a report. Lets look at creating and using a simple thread-safe Java in-memory cache.


2 Answers

EHCache is very nice. You can create an in memory cache. Check out their code samples for an example of creating an in memory cache. You can specify a max size, and a time to live.

EHCache does offer some advanced features, but if your not interested in using them - don't. But it's nice to know they are there if your requirements ever change.

Here is an in memory cache. Created in code, with no configuration files.

CacheManager cacheManager = CacheManager.getInstance(); int oneDay = 24 * 60 * 60; Cache memoryOnlyCache = new Cache("name", 200, false, false, oneDay, oneDay); cacheManager.addCache(memoryOnlyCache); 

Creates a cache that will hold 200 elements, and has a ttl of 24 hours.

like image 117
Steve K Avatar answered Sep 21 '22 07:09

Steve K


I really like the MapMaker that comes with Google Guava (API)

The JavaDoc has a pretty neat example that demonstrates both its ease of use and its power:

ConcurrentMap<Key, Graph> graphs = new MapMaker()    .concurrencyLevel(32)    .softKeys()    .weakValues()    .expiration(30, TimeUnit.MINUTES)    .makeComputingMap(        new Function<Key, Graph>() {          public Graph apply(Key key) {            return createExpensiveGraph(key);          }        }); 

Furthermore, release 10.0 of Guava introduced the much more extensive com.google.common.cache package (there's a nice wiki entry on how to use them).

like image 44
Joachim Sauer Avatar answered Sep 18 '22 07:09

Joachim Sauer