Ehcache/JCache and generic types

532 views
Skip to first unread message

calvin ang

unread,
Oct 31, 2016, 7:09:33 PM10/31/16
to ehcache-users
Hi there,

Is there any way to configure a cache whose value type is a generic using either the Ehcache or JCache APIs? In other words, I'd like to do something like the following:

public class BasicCache<T>
{
private void foo()
{
CachingProvider provider = Caching.getCachingProvider(fullyQualifiedProviderName);
     CacheManager cacheManager = provider.getCacheManager();
     configuration = new MutableConfiguration<String, T>()
     .setTypes(String.class, T.class)....
} 
 }

T.class is not permissible and from what I understand, there is no "good" workaround for it. (I'm a C# guy so ordinarily something like the above would be permissible.)

Is there any way possible to get around this limitation? Maybe it _is_ indeed possible and I am missing something?

As you might have guessed, I'm trying to write a wrapper around the API. 

Ehcache 3.1; JDK 1.7

Thanks!
-calvin


Alex Snaps

unread,
Oct 31, 2016, 7:53:23 PM10/31/16
to ehcache-users
Right, in Java, the generic type gets erased... 
But you could pass in the T's class instance, say to the constructor as in:

BasicCache<Foo> basicCache = new BasicCache(Foo.class);

Where the constructor would that a Class<T>:  BasicCache(Class<T> valueType);
You could then use the Class<T> field to create the actual cache instance in foo();


--
You received this message because you are subscribed to the Google Groups "ehcache-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ehcache-user...@googlegroups.com.
To post to this group, send email to ehcach...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ehcache-users/bc284613-1a39-4280-825c-83e187f2b55b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Alex Snaps
Twitter: @alexsnaps

calvin ang

unread,
Oct 31, 2016, 9:22:36 PM10/31/16
to ehcache-users
Thanks, that would work!
Reply all
Reply to author
Forward
0 new messages