JSR-107 cache is not being populated from CacheLoader even though the CacheLoader is being called.

180 views
Skip to first unread message

jeff.j...@jeppesen.com

unread,
Feb 3, 2016, 5:15:10 PM2/3/16
to ehcache-users
  1. What version of Ehcache you are currently using;
  2. Paste the configuration for the Cache/CacheManager you have an issue with;
  3. Add any name and version of other library or framework you use Ehcache with (e.g. Hibernate);
  4. Providing JDK and OS versions maybe useful as well.

  1. What version of Ehcache you are currently using;
    1.     <!-- EHCache -->
    2.     <dependency>
    3.       <groupId>org.ehcache</groupId>
    4.       <artifactId>ehcache</artifactId>
    5.       <version>3.0.0.m4</version>
    6.     </dependency>
    7.      <dependency>
    8.       <groupId>javax.cache</groupId>
    9.       <artifactId>cache-api</artifactId>
    10.       <version>1.0.0</version>
    11.     </dependency>
    12.     <dependency>
    13.       <groupId>org.terracotta</groupId>
    14.       <artifactId>management-model</artifactId>
    15.       <version>2.0.0</version>
    16.     </dependency>    
    17.   </dependencies>
  2. Paste the configuration for the Cache/CacheManager you have an issue with;
    1. I am not currently using configuration - just working pragmatically.
  3. Add any name and version of other library or framework you use Ehcache with (e.g. Hibernate);
    1. No framework integration other that the cache loader itself.
  4. Providing JDK and OS versions maybe useful as well.
    1. Java 1.7 OS - various.

I have pasted a snippet below of a cache singleton that has been modified to remove any proprietary info - so any typos are a result of that modification. If I pause execution in the init method right before returning true I can see that my CacheLoader has indeed returned a map with a single key/value. So the configuration is picking up and executing the CacheLoader and the CacheLoader is returning a value. The EHCache appears to be empty however so the data returned by the CacheLoader is not being populated in the cache itself which is returning an empty set. For instance the getAll() method below will return an empty set even though a valid key is passed in and there is data for that key which was returned by the CacheLoader.loadAll() method. So where is the disconnect? Do I need additional configuration or code steps so that the data returned by the cache loader is wired to the cache itself even though the CacheLoader is being called? The SomeKey object is not a simple primitive but has around 8 or 9 fields which are used by the hashCode() method. Below the code are some log snippets from the unit test run.

private static CachingProvider provider = Caching.getCachingProvider(); 
private static CacheManager cacheManager = provider.getCacheManager(); 
private static Cache<SomeKey, SomeObject> cache = null;
private static MutableConfiguration<SomeKey, SomeObject> configuration = null;

private static SomeCache instance = null;
public static SomeCache getInstance()
{
if(instance == null)
synchronized(SomeCache.class)
{
if(instance==null)
{
instance = new SomeCache();
configuration = new MutableConfiguration<SomeKey, SomeObject>();
configuration.setTypes(SomeKey.class, SomeObject.class);
configuration.setCacheLoaderFactory(new CacheLoaderFactory<SomeObject>());
cache = cacheManager.createCache("Cache", configuration);
init();
}
}
return instance;
}
private static boolean init()
{
Set<SomeKey> pwGetAllValidKeys = Dao.pwGetAllValidKeys();
cache.loadAll(pwGetAllValidKeys, true, new CacheLoadCompletionListener());
return true;
}

public Map<SomeKey, SomeObject> getAll()
{
Set<SomeKey> pwGetAllValidKeys = SomeDao.pwGetAllValidKeys();
return cache.getAll(pwGetAllValidKeys);
}


14:17:47,691 INFO  [EhcacheManager] Initialize successful.
14:17:47,774 INFO  [EhcacheManager] Cache 'Cache' is getting created in EhcacheManager.
14:17:47,778 INFO  [DefaultSerializationProvider] Serializer for <com.domain.SomeKey> : org.ehcache.internal.serialization.CompactJavaSerializer@7048f722
14:17:47,782 INFO  [DefaultSerializationProvider] Serializer for <com.domain.SomeObject> : org.ehcache.internal.serialization.CompactJavaSerializer@28c0b664
14:17:47,785 INFO  [DefaultCopyProvider] Copier for <com.domain.SomeKey> : org.ehcache.internal.copy.SerializingCopier@3c8bdd5b
14:17:47,786 INFO  [DefaultCopyProvider] Copier for <com.domain.SomeObject> : org.ehcache.internal.copy.SerializingCopier@40e4ea87
14:17:47,905 INFO  [Ehcache-Cache] Initialize successful.
14:17:47,948 INFO  [EhcacheManager] Cache 'Cache' created in EhcacheManager.


14:17:52,237 INFO [CacheLoader] Returning Map for load all:{SomeKeyKey [blah....about 6 pages of logs truncated]} Definitely returning data here
14:17:52,295 INFO  [CacheLoadCompletionListener] cache successfully loaded!

Thanks in advance.

jeff.j...@jeppesen.com

unread,
Feb 4, 2016, 11:27:45 AM2/4/16
to ehcache-users
FYI, it turned out that no objects were being returned from the cache due to the default equals method for our key objects - so I was able to fix the problem by implementing the equals method. IMHO the JSR-107 spec would benefit from a simple getAll() method that would not require key arguments. Obviously this is not the forum for that discussion.

Louis Jacomet

unread,
Feb 4, 2016, 11:39:35 AM2/4/16
to ehcache-users
Hi,

Glad you found a solution.
Indeed, having proper hash code and equals implementation for cache keys is a must.

I have to admit the getAll method sounds strange, except maybe for testing.

Regards,
Louis

--
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/0c28a188-84a9-472b-9534-115f66da26f5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages