Search code examples
ignitejcache

Access underlying type definition in Ignite cache


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?


Solution

  • 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.