1

Is there any way I can get the underlying key and value type definition of the cache that is created by some other code? At this point of time I only know the cache name. I don't have any other information about how the cache was created.

IgniteCache<K, V> dataCache = ignite.cache("dataCache");

I need to get the type of K and V in the line above.

I will be attaching a CacheEntryListener to the cache, and I need to access the fields in the value part of the cache. How do I do this? Is there any way I can access the type information from the CacheEntryEvent events that I get in the listener methods?

James Isaac
  • 2,587
  • 6
  • 20
  • 30

1 Answers1

2

Ignite cache does not have any underlying type definition, it can hold data of any type. Generics are only for your convenience.

For example, you can do this:

IgniteCache<String, String> sCache = ignite.createCache("foo");
sCache.put("1", "2");

IgniteCache<Integer, Integer> iCache = ignite.cache("foo"); // same cache
iCache.put(1, 2);

IgniteCache<Object, Object> oCache = ignite.cache("foo"); // same cache
oCache.get(1);  // 2
oCache.get("1");  // "2"

However, this is not recommended.

One type per cache is the recommended approach: your application logic should make sure that each named cache works with particular data types only.

Pavel Tupitsyn
  • 8,393
  • 3
  • 22
  • 44
  • I agree. However, assume that my ignite code does not know what is the type of the value in the cache. The cache could have been created by some other component in my application pipeline. It could be of Employee type, or Student type. Can I get the type definition (could be Employee.class or Student.class) ? – James Isaac Sep 25 '17 at 10:49
  • @JamesIsaac general answer is no: Ignite does not store that. createCache("c") is the same as createCache("c"). – Pavel Tupitsyn Sep 25 '17 at 11:37