I have tried Hazelcast as cache (using #Cacheable and #CacheEvict) with Spring Boot (using hazelcast-spring artifact in Spring boot 1.5.2.RELEASE) and my application crashed because it ran out of memory. I have two questions:
1) Default behaviour seems to be filling all the cache until there is no more memory and then throw an exception. Am I right? It looks weird to me that there is no default LRU o LFU criteria for caches.
2) How can I configure in hazelcast.xml max size, ttl, etc for ALL maps in my application? (Something like a default config and then extend it for particular maps)
Thanks.
#italktothewind
1 - By default, there is no eviction configured, either time or size based. But you can configure any of them or both. Please see http://docs.hazelcast.org/docs/latest-development/manual/html/Distributed_Data_Structures/Map/Map_Eviction.html
2 - If you configure the default map & no special config per map, then all maps will share the same config. But if you decide to add a map specific config, then you need to add all necessary config options since it won't inherit the other non-configured params from default map config.
Related
Is there a simple way to set management.server.port programmatically, based on the management.server value? I would like to set
management.server.port=${server.port + 2}
What I was thinking is creating http connector manually for actuator but it is a lot of afford.
You can tune the environment on startup using an EnvironmentPostProcessor. This will work as long as you don't see server.port to 0. When you do that, the underlying connector is responsible to allocate a random, free port and that will happen after the environment has been prepared.
The idea behind the implementation is to read the value of server.port, apply whatever logic you need and set a management.server.port property.
There is more explanation and a sample in the Spring Boot Reference Guide.
I've got an application that uses Spring Cache (Ehcache), but now we need to add a 2nd node (same application). Can the cache be shared between the nodes or each with their own instance but synched?
Or do I need to look at a different solution?
Thanks.
That depends on your cache implementation - not on Spring, which only provides an abstract caching API. You are using EhCache as your caching implementation, which comes with a Terracotta server for basic clustering support and is open source. See http://www.ehcache.org/documentation/3.1/clustered-cache.html#clustering-concepts for more details
I have a project that supports 10's of concurrent users.
My project is a spring + hibernate project with a MYSQL DB.
I would like to keep a cache for some of my entities (i.e. Player)
I have a couple of questions:
1) how do I exactly work with a cache (when i have one) ? if i have to persist a Player do i change the cache and immediately persist with hibernate?
2) Does spring support a cache mechanism ? if so how does one work with it?
Hibernate
Hibernate has Second level cache. To start work with you need to:
choose some cache provider (EHCache, Infinispan, ...).
configure cache region (and choose corresponding strategy depending on your situation)
enable cache for some entity
It is declarative, most of the time you do not need to change any application code.
Spring
Spring has cache abstraction. There are some common steps (choose chache provider, configure cache regions). But it is more general pourpose cache, not related to Hibernate entities and transactions. So you must do more work (annotate all necessary methods with annotations).
In general if your Player class is a Hibernate entity then it will be better to go with Hibernate cache. It may be not true if you have some special demands.
Hope it helps.
I am afraid I have got some pretty basic questions about ehcache. I would like to use caching mechanism on clustered Glassfish without any significant infrastrucure.
As I know using ditributed cache with ehcache means that I have to use the terracotta server array, don't?
I am not so experienced in caching so could I use the ehcache on clustered glassfish that I just put some JAR into the classpath of Glassfish or deploy a WAR or something onto Glassfish and that's it? Do I have to use an external cache server anyway?
The replicated cache in ehcache doesn't need the terracotta server array, do it?
I would like to store a java Map object in the store which is going to be changed quite often. In this case the replicated cache is not best choice, as I know. The Hazelcast distributed cache needs any external cache server?
Thank you very much for your help in advance!
Have a nice day, experts!
Hazelcast doesn't need any externel server if you are running Java.
Basically add hazelcast.jar into your classpath. And from your application creata an Hazelcast instance:
HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(new Config());
then to get a distributed map:
Map map = hazelcast.getMap("myMap");
that's it. In this example I provided the default config which uses Multicast to discovery of the nodes. You can update and change any parameter.
For more information see Quick Start Tutorial
The replication feature in Ehcache does not require any server. You simply add the Ehcache jar to your web application and configure Ehcache to replicate to all cluster nodes. You can choose whether to automatically discover all GlassFish nodes using multicast or you can manually tell Ehcache where to find the other nodes. You can find the Ehcache replication configuration instructions here: http://ehcache.org/documentation/replication/rmi-replicated-caching#configuring-the-peer-provider
Hazelcast works similarly. See here for documentation: http://hazelcast.org/docs/3.0/manual/html/ch12s02.html
I want to dynamically change the configuration of the EhCache instance we're using, specifically the maxElementsInMemory setting. We are using EhCache 1.5 and I can see that it is possible API-wise:
cache.getCacheConfiguration().setMaxElementsInMemory(num);
But the documentation of EhCache says:
After a Cache has been started its
configuration is not generally
changeable. However, since Ehcache
2.0, certain aspects of cache configuration can modified dynamically
at runtime...
So my question is: can I use this API in EhCache 1.5 or is it not guaranteed to work?
Thanks.
There are only certain properties you can modify (see Modifying Ehcache configuration at runtime), maxElementsInMemory should be one of them... for me that works in ehcache 2.4!
As per Ehcache 2.8, this is possible:
Cache cache = manager.getCache("sampleCache");
CacheConfiguration config = cache.getCacheConfiguration();
config.setTimeToIdleSeconds(60);
config.setTimeToLiveSeconds(120);
config.setmaxEntriesLocalHeap(10000);
config.setmaxEntriesLocalDisk(1000000);
source:
http://www.ehcache.org/documentation/2.8/configuration/configuration#dynamically-changing-cache-configuration