Difficult migration from 2.10 to 3.5.2

2,080 views
Skip to first unread message

samir.ha...@gmail.com

unread,
Apr 3, 2018, 11:18:46 AM4/3/18
to ehcache-users
Hi guys,

I want to migrate my application using Ehcache from 2.10 to latest version, so 3.5.2. I've looked over the migration guide here : http://www.ehcache.org/documentation/3.5/migration-guide.html but it has not been helpful at all.

I've looked around the forum and I did not find any similar topics so I'm going to ask all my questions here. Hopefully, I'll be able to gather some answers and improve the migration guide so that it will be easier for future users.

Basically, I had an ehcache.xml describing several cache associated to several names, for example :
<cache name="template_message"
           
maxEntriesLocalHeap="100"
           
statistics="false"
           
eternal="false"
           
maxEntriesLocalDisk="10000000"
           
diskExpiryThreadIntervalSeconds="120"
           
memoryStoreEvictionPolicy="LFU">
       
<persistence strategy="localTempSwap"/>
   
</cache>
   
<cache name="template_thread"
           
maxEntriesLocalHeap="1000"
           
statistics="false"
           
eternal="false"
           
maxEntriesLocalDisk="10000000"
           
diskExpiryThreadIntervalSeconds="120"
           
memoryStoreEvictionPolicy="LFU">
       
<persistence strategy="localTempSwap"/>
   
</cache>

Then I was able to retrieve that file into a Configuration object, and to pass it to a CacheManager. It was also possible to configure a DiskStoreConfiguration along. It looked like that :
try {
           
Configuration cfg = new Configuration();
            URL urlConfig
= Loader.getDefault().getResource(DEFAULT_CONFIG);
           
                    cfg
= ConfigurationFactory.parseConfiguration(stream);
               
           
DiskStoreConfiguration dsc = cfg.getDiskStoreConfiguration();
           
if (dsc == null) {
                dsc
= new DiskStoreConfiguration();
                cfg
.addDiskStore(dsc);
           
}
           
String path = configureDiskPath(dsc.getPath());
           
            dsc
.setPath(path);
            manager
= CacheManager.create(cfg);

Now, I am trying to do the same but with Ehcache 3. I'm not getting into the xml because I think it would be quite easy. I'm currently struggling with the Configuration. I was able to specify an XML configuration and to configure the diskStore. Is it no possible anymore? Or is everything configured in the XML?

What I do now is that :
CacheManagerBuilder<CacheManager> builder = CacheManagerBuilder.newCacheManagerBuilder();
           
CacheManagerConfiguration<PersistentCacheManager> conf = CacheManagerBuilder.persistence(path);
           
CacheManagerBuilder<PersistentCacheManager> newBuilder = builder.with(conf);
 manager
= newBuilder.build();
manager
.init();
So I'm simply building a persistent CacheManager. And when I create the Cache, I retrieve the XMLConfiguration and give it to that manager :
CacheConfiguration cConf = CacheWrapperManager.getInstance().getXmlConfiguration().getCacheConfigurations().get(cacheTemplateName);
 cache
= manager.createCache(cacheName, cConf);

In theory, I should have a Cache that follow the configuration given in the XML, and is persistent on disk right?

But really I'm diving into darkness here and I'm not sure at all about what I'm doing. For example, when I create the Cache, I give a cacheName.
Now I want to remove that Cache, so I want to call the remove method of the CacheManager, but it needs the alias. This alias is nowhere to be seen in the Cache interface?! And the CacheManager has no remove methods taking a Cache object directly.

Last but not least, I used to check the Cache Status before. This Status is nowhere to be seen now, and the only Status I see is on the CacheManager itself. Is there a way to check the status of a specific cache?

Any help, links, answers are really appreciated,
Regards,
Hadzic Samir

Alex Snaps

unread,
Apr 3, 2018, 1:46:42 PM4/3/18
to ehcach...@googlegroups.com
Hey,
I'm confused about a couple of things in the example you gave. What's `CacheWrapperManager`? 
If you want to configure things from XML, you'll have to create a new XML configuration file, according to this Schema: http://www.ehcache.org/documentation/3.5/xsds.html#core 
You can programmatically parse that XML to get a `XmlConfiguration` instance as so: http://www.ehcache.org/documentation/3.5/xml.html#xml-programmatic-parsing
That will then let you instantiate a new `CacheManager` with all services that you want (including a disk tier, if you configure that: http://www.ehcache.org/documentation/3.5/tiering.html#disk-tier )

wrt to name, cache aren't ever named… When they are managed (i.e. belong to a CacheManager), then they are registered under an alias. You can retrieve the aliases for all these from the CacheManager.getRuntimeConfiguration().getCacheConfigurations().keySet(). But I'm unsure why you would want to remove a Cache from a CacheManager, even more so if it all comes from a static XML file somewhere… what am I missing? Maybe `UserManagedCache` is what you need instead?

Finally, the Cache status is of no use really… even more so being able to check for it, other than possibly closing it, which is something you'd only do to a `UserManagedCache` again. I mean `if(cache.isNotClosed()) cache.get()` is just racy anyways… doesn't provide any guarantees. So, again, not sure what you are trying to achieve with these API calls.

I hope this sheds some light…
Alex

--
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/be3bd80f-945a-4205-92b0-19c9b20157e2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Alex Snaps
Twitter: @alexsnaps

samir.ha...@gmail.com

unread,
Apr 4, 2018, 10:55:47 AM4/4/18
to ehcache-users
Hi Alex,

First of all, thank you for the time you took to answer me.

I'll be honest with you, I'm a bit confused about everything. So I suggest we go baby-step by baby-step ;)

I want to configure several Cache that will use a defined template in XML. For example the template_message I mentioned earlier. In 2.10, I had a single CacheManager, which would create several caches based on template_message. Some may live the entire application time, some don't.

Now I want this cache template to have a max number of entries on the heap, and if it's too much, the rest must go on the disk. In 2.10 this was possible by specifying maxEntriesLocalHeap and maxEntriesLocalDisk. Allright, so I managed to write this in 3.5 which should be good :
<?xml version="1.0" encoding="UTF-8"?>
<config
   
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
 
xmlns='http://www.ehcache.org/v3'
 
xsi:schemaLocation="
        http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.5.xsd"
>
   
<cache alias="template_message">
       
<resources>
           
<heap unit="entries">100</heap>
           
<disk unit="MB"> 100</disk>
       
</resources>
   
</cache>
</config>

Apparently I cannot specify a max entries on disk, only a maximum space. Don't act.

Now, where I'm confused as you have seen, is in the mixing of all that.
What I want to do is to have a CacheManager where I say "Create a cache based on template_message where the disk location will be C:/Temp".

Based on your links, I should be able to do that :
XmlConfiguration cfg = new XmlConfiguration(urlConfig);//My XML file
           
CacheConfigurationBuilder cacheConfBuilder = cfg.newCacheConfigurationBuilderFromTemplate("template_cache", String.class, String.class);
           
           
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                   
.with(CacheManagerBuilder.persistence(new File(getStoragePath(), "myData")))
                   
.withCache("template_cache", cacheConfBuilder).build();

Cache cache1 = cacheManager.createCache("CACHE1", cfg.getCacheConfigurations().get("template_cache"));

But this seems very weird because I need to give a CacheConfiguration when I'm creating a Cache when I already gave it when I built the CacheManager! Moreover, if I have 10 different templates, I would have to write 10 times the withCache methods?


wrt to name, cache aren't ever named… When they are managed (i.e. belong to a CacheManager), then they are registered under an alias. You can retrieve the aliases for all these from the CacheManager.
getRuntimeConfiguration().getCacheConfigurations().keySet(). But I'm unsure why you would want to remove a Cache from a CacheManager, even more so if it all comes from a static XML file somewhere… what am I missing? Maybe `UserManagedCache` is what you need instead?

In my application, I will maybe use three different message provider. One class will use one provider, thus creating a Cache using template_message. It will be used for 1 hour for example. Now another class want to use a different message provider and I don't need to use the first one anymore. So basically, I want to close my first Cache, remove it from the CacheManager, and create a new Cache from my new class and begin to use the other message provider. That's why I'm quite puzzled by the fact that we are able to create some Cache in the manager specifying an alias, and not able to remove them easily. But that's not really important because I can keep the alias somewhere.

Best regards,
Sam'
Reply all
Reply to author
Forward
0 new messages