I have a problem when using cache across different modules. At first, I was using Ehcache 2.10.6 so in my configuration ehcache.xml file I had something like this:
<bootstrapCacheLoaderFactory
class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>
and I was using it as cache.bootstrap();
After upgrading to 3.x version (3.8.1) , this is not available nor I can see any replacements. Is there any replacement to do or something else to replicate the configuration above?
Thanks.
RMI based replication has been discontinued in Ehcache 3.x. Its implementation in Ehcache 2.x was lacking in semantics: writes could be lost, reads were not guaranteed to be consistent, etc ...
Instead you should look into cache clustering with Terracotta.
Related
I'm using JCache annotations with Spring and EhCache. If I use #CacheResult on a method without specifying the cache name (or a #CacheDefaults on the class), then the default cache name resolves to the fully qualified method name. However such a cache is not found, unless explicitly created using the CacheManager. This may be manageable for a few such cache-enabled methods, but not if I have to create 50 different caches manually.
Is there a way to tell Spring (or any JCache implementer) to automatically create caches with the default name, if not found? This would allow me to use #CacheResult on any method without having to go update the cache configuration every time.
Is there a way to tell Spring (or any JCache implementer) to automatically create caches with the default name, if not found?
This problem is covered in cache2k. You can configure cache2k to use a default configuration, if the requested cache name is not known.
Here is an example XML configuration for this scenario, which is placed at /cache2k.xml in the class path:
<cache2k xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='https://cache2k.org/schema/v1.x'
xsi:schemaLocation="https://cache2k.org/schema/v1.x https://cache2k.org/schema/cache2k-core-v1.x.xsd">
<version>1.0</version>
<ignoreMissingCacheConfiguration>true</ignoreMissingCacheConfiguration>
<defaults>
<!-- default settings for every cache -->
<cache>
<entryCapacity>100_000</entryCapacity>
<expireAfterWrite>5m</expireAfterWrite>
</cache>
</defaults>
<templates>
<cache>
<name>shortExpiry</name>
<expireAfterWrite>5m</expireAfterWrite>
</cache>
<cache>
<name>lessResilient</name>
<resilienceDuration>1m</resilienceDuration>
</cache>
</templates>
<caches>
<cache>
<name>products</name>
<entryCapacity>10_000</entryCapacity>
<include>shortExpiry,lessResilient</include>
</cache>
<cache>
<name>users</name>
<entryCapacity>1000</entryCapacity>
<include>shortExpiry</include>
</cache>
</caches>
</cache2k>
By default, according to this configuration, a cache without a specific configuration entry will get 100K entry limit and 5 minutes expiry. The configuration may be suitable for the trails in production. Later on, you, or a system operator, can check what caches are existing and their performance via JMX and then add a more specific cache configuration as soon as there is need. If you have many caches, the configuration has a template mechanism to avoid repetition.
It may happen that caches need a specific configuration upfront and some don't. I suggest separating them into different cache managers.
If you need an in process cache, cache2k is a a good alternative to EHCache. It is way faster and more memory efficient.
You can use cache2k via JCache, however there is also direct Spring framework support. See the User Guide Spring Framework Section for details.
I am the author of cache2k and not a heavy Spring user at the moment, however, I am happy to help if there are any issues.
How can I configure EhCache 3.5 (xml-config, ehcache.xml) to allow only 100 cached instances for:
org.company.Foo? Basically, I have a dozen of entities with separate caching limits wanted.
I've read some sources where they state this is possible, like this one source
This syntax doesn't work for versions 3.x (and I doubt it is the correct config for 2.x too)
<ehcache>
<cache name="org.company.Foo" maxElementsInMemory="100" />
</ehcache>
The official documentation is silent (or I missed it somehow, but scrutinized pretty thoroughly) on this topic for versions 2.x as well as for versions 3.x
If not possible, what is the preferred idiom for configuring EhCache?
Thank you.
P.S. I am using EhCache 3.5 with Hibernate 5.3 and able to monitor the cache state via VisualVM console through MBeans plugin.
The correct config would be like this:
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
xmlns='http://www.ehcache.org/v3'
xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd">
<service>
<jsr107:defaults enable-management="true" enable-statistics="true"/>
</service>
<cache alias="org.company.Foo">
<heap>1000</heap>
</cache>
I want to configure Hibernate's second level cache to use AWS Elasticache (currently trying to use memcached). I've looked at things like this, but can't seem to get it to work.
In the Hibernate 5.0 docs, it says that Wildfly automatically configures Hibernate's second level cache to use Inifinispan. Is there an easy way to disable this? Does configuring the cache to use elasticache (correctly) override this?
How can I configure the cache to use memcached in Hibernate 5? Several places say to set the property "hibernate.cache.provider_class" but the Hibernate 5.0 docs say that the property "hibernate.cache.region.factory_class" needs to be set to determine which provider to use. Also are old versions of hibernate-memcached (like this) still usable?
I need to load some data from DB to Cache on server start up.And once a request came, need to take the data from this cache.Also need to refresh cache in frequent intervals.It would be help if somebody provide way for achieving this.I am using Spring 3.1.
Thanks
There are lots of cache implementations available:
The Spring Cache abstraction
Ehcache (which is one possible provider for Spring Cache)
Guava's LoadingCache
Infinispan as user1516873 suggests
A plain ConcurrentHashMap from the JDK if you don't want more dependencies
etc.
I think this is what you are looking for:
Spring: hibernate + ehcache
As you will see my recommendation is to use hibernate second level cache and your problem will be fixed at configuration level.
Try Infinispan.
It modern, open source and easy to use cache. You can embed it and use directly, or use it as second level cache in hibernate.
I currently use JBossCache 3.2 as a Hibernate 2nd level cache for clustered caching of entities in a EJB3 environment (GlassFish 2) which runs fine. Now I'm evaluating using Infinispan 4.2.1 instead and to my surprise I get NamedCacheNotFoundExceptions.
I use annotations at entity level like
#Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL, region = "myRegion")
and the exeptions are like
org.infinispan.CacheException: org.infinispan.manager.NamedCacheNotFoundException: Cache: myRegion
at org.infinispan.remoting.rpc.RpcManagerImpl.invokeRemotely(RpcManagerImpl.java:115)
at org.infinispan.remoting.rpc.RpcManagerImpl.invokeRemotely(RpcManagerImpl.java:126)
at org.infinispan.remoting.rpc.RpcManagerImpl.invokeRemotely(RpcManagerImpl.java:231)
at org.infinispan.remoting.rpc.RpcManagerImpl.invokeRemotely(RpcManagerImpl.java:218)
at org.infinispan.remoting.rpc.RpcManagerImpl.broadcastRpcCommand(RpcManagerImpl.java:201)
at org.infinispan.remoting.rpc.RpcManagerImpl.broadcastRpcCommand(RpcManagerImpl.java:194)
at org.infinispan.interceptors.InvalidationInterceptor.invalidateAcrossCluster(InvalidationInterceptor.java:220)
....
Looking via jmx I can see a corresponding MBean but it's CacheName is like myRegion(invalidation_sync). The suffix (invalidation_sync) seems to be added by Infinispan.
Is this the root cause of my problem? How to come around it? My Infinispan config is largely the one that comes with hibernate-infinsipan but with jmx enabled.