Search code examples
cache2k

Cache2k: java.lang.UnsupportedOperationException: loader not set


Cache2k looks like a very promising caching implementation. Unfortunately the documentation is very limited, which is why I need some help with the following issue. I am using the latest version 0.26-BETA.

According to the documentation the cache is supposed to be created like this:

Cache<String,String> c =
CacheBuilder.newCache(String.class, String.class).build();
String val = c.peek("something");
assertNull(val);
c.put("something", "hello");
val = c.get("something");
assertNotNull(val);
c.destroy();

Unfortunately at least 2 of these methods are deprecated, including the CacheBuilder class itself. I therefore tried creating the cache like this:

        org.cache2k.Cache<String, Object> newCache = Cache2kBuilder.of(String.class, Object.class)
                .name(cacheKey.toString())
                .entryCapacity(100000)
                .eternal(true)
                .build();

This however throws the "java.lang.UnsupportedOperationException: loader not set" exception.

The question therefore is: how am I supposed to build the cache so that I do not get this exception?

EDIT:

This gives me the same exception:

            org.cache2k.Cache<Object, Object> newCache =
                CacheBuilder.newCache(Object.class, Object.class)
                        .eternal(true)
                        .build();

EDIT #2:

Just one more note: When I copy&paste the code from the wiki page I get an error - as can be seen in the image below.

This does not seem to work with jdk 1.8.0_91

With what jdk version are you testing? I'll try just removing the <> that are causing the problem for now.

Thanks very much in advance!

Michael


Solution

  • Cache2k looks like a very promising caching implementation.

    Thanks :)

    According to the documentation the cache is supposed to be created like this

    There are new interfaces in place. The deprecated one is still there to support users of old cache2k versions. That will get cleared up in the next weeks. Sorry for the confusion.

    Please take a look here for the latest getting started information: https://github.com/cache2k/cache2k/blob/master/doc/src/docs/asciidoc/user-guide/sections/_start.adoc

    This however throws the "java.lang.UnsupportedOperationException: loader not set" exception. The question therefore is: how am I supposed to build the cache so that I do not get this exception?

    Short answer: Either use cache.peek() or wait for 0.27, since then it is working with cache.get() transparently.

    Longer answer: In our own applications I use cache.get() only when a loader is defined and cache.peek() when no loader is defined or when I want to inspect the cache only. Reserving cache.get() only for the read through usage, seemed like a good idea. However, I reasoned that it might be a caveat for new users, so I change that behavior and align it to other cache solutions.

    Answer to Edit 2: For an untyped cache use the factory method Cache2kBuilder.forUnkownTypes(). Constructing the anonymous class is only needed for specific types.