package io.refectoring.cache.embedded;

import com.hazelcast.config.Config;
import com.hazelcast.config.MapConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
import io.refectoring.cache.embedded.rest.Car;
import org.springframework.stereotype.Component;

@Component
public class HazelcastNode {

    public static final String CARS = "cars";
    private final HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance(createConfig());

    public String put(String number, Car car){
        IMap<Object, Object> map = hzInstance.getMap(CARS);
        return (String) map.put(number, car);
    }

    public Car get(String key){
        IMap<Object, Object> map = hzInstance.getMap(CARS);
        return (Car) map.get(key);
    }

    public Config createConfig() {
        Config config = new Config();
        MapConfig mapConfig = new MapConfig(CARS);
        mapConfig.setTimeToLiveSeconds(20);
        mapConfig.setMaxIdleSeconds(360);
        config.addMapConfig(mapConfig);
        return config;
    }
}

@@ -65,53 +65,9 @@
 fig.
-Config;%0Aimport com.hazelcast.config.MapConfig
+*
 ;%0Aim
@@ -597,16 +597,24 @@
  map.put
+IfAbsent
 (number,
@@ -834,16 +834,124 @@
 nfig();%0A
+        config.addMapConfig(mapConfig());%0A        return config;%0A    %7D%0A%0A    private MapConfig mapConfig() %7B%0A
         
@@ -985,32 +985,87 @@
 apConfig(CARS);%0A
+        mapConfig.setEvictionConfig(evictionConfig());%0A
         mapConfi
@@ -1146,37 +1146,238 @@
     
-config.addMapConfig(mapConfig
+return mapConfig;%0A    %7D%0A%0A    private EvictionConfig evictionConfig() %7B%0A        EvictionConfig evictionConfig = new EvictionConfig();%0A        evictionConfig.setEvictionPolicy(EvictionPolicy.LFU);%0A        evictionConfig.setSize(2000
 );%0A 
@@ -1382,32 +1382,40 @@
 %0A        return 
-c
+evictionC
 onfig;%0A    %7D%0A%7D%0A

Add eviction configuration for the Map.