protected EvictionConfig createEvictionConfigWithEntryCountPolicy(int size) { return new EvictionConfig() .setEvictionPolicy(EvictionPolicy.LRU) .setMaximumSizePolicy(EvictionConfig.MaxSizePolicy.ENTRY_COUNT) .setSize(size); }
private static void evictionConfigXmlGenerator(XmlGenerator gen, EvictionConfig e) { if (e == null) { return; } String comparatorClassName = !isNullOrEmpty(e.getComparatorClassName()) ? e.getComparatorClassName() : null; gen.node("eviction", null, "size", e.getSize(), "max-size-policy", e.getMaximumSizePolicy(), "eviction-policy", e.getEvictionPolicy(), "comparator-class-name", comparatorClassName); }
private static CacheConfig createCacheConfig(String cacheName, boolean evictionPolicyComparatorByName) { EvictionConfig evictionConfig = evictionPolicyComparatorByName ? new EvictionConfig().setComparatorClassName(MyEvictionPolicyComparator.class.getName()) : new EvictionConfig().setComparator(new MyEvictionPolicyComparator()); return new CacheConfig() .setName(cacheName) .setEvictionConfig(evictionConfig); }
private static EvictionConfig createEvictionConfig() { return new EvictionConfig() // %90 percentage of native memory can be used .setSize(90) .setMaximumSizePolicy(EvictionConfig.MaxSizePolicy.USED_NATIVE_MEMORY_PERCENTAGE); }
/** * Create a new {@link CacheConfig} with configured settings before {@link CacheManagerAware} implementor. */ private CacheConfig<?, ?> newCacheConfig(final String name) { final CacheConfig<?, ?> config = new CacheConfig<>(name); config.setEvictionConfig(new EvictionConfig().setEvictionPolicy(EvictionPolicy.LRU) .setMaximumSizePolicy(EvictionConfig.MaxSizePolicy.ENTRY_COUNT)); config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(Duration.ETERNAL)); // Post configuration postConfigure(config); return config; }
/** * Sets the maximum size of the Near Cache. * <p> * When the maxSize is reached, the Near Cache is evicted based on the policy defined. * <p> * Accepts any integer between {@code 0} and {@link Integer#MAX_VALUE}. * The value {@code 0} means {@link Integer#MAX_VALUE}. * The default is {@code 0}. * * @param maxSize the maximum size of the Near Cache * @return this Near Cache config instance * @deprecated since 3.8, please use {@link #setEvictionConfig(EvictionConfig)} and {@link EvictionConfig#setSize(int)} */ @Deprecated public NearCacheConfig setMaxSize(int maxSize) { checkNotNegative(maxSize, "maxSize cannot be a negative number!"); this.maxSize = calculateMaxSize(maxSize); this.evictionConfig.setSize(this.maxSize); this.evictionConfig.setMaximumSizePolicy(ENTRY_COUNT); return this; }
/** * @deprecated since 3.8, please use * {@link NearCacheConfig#NearCacheConfig(int, int, boolean, InMemoryFormat, EvictionConfig)} */ @Deprecated public NearCacheConfig(int timeToLiveSeconds, int maxSize, String evictionPolicy, int maxIdleSeconds, boolean invalidateOnChange, InMemoryFormat inMemoryFormat, EvictionConfig evictionConfig) { this.inMemoryFormat = inMemoryFormat; this.invalidateOnChange = invalidateOnChange; this.timeToLiveSeconds = timeToLiveSeconds; this.maxIdleSeconds = maxIdleSeconds; this.maxSize = calculateMaxSize(maxSize); this.evictionPolicy = evictionPolicy; // EvictionConfig is not allowed to be null if (evictionConfig != null) { this.evictionConfig = evictionConfig; } else { this.evictionConfig.setSize(calculateMaxSize(maxSize)); this.evictionConfig.setEvictionPolicy(EvictionPolicy.valueOf(evictionPolicy)); this.evictionConfig.setMaximumSizePolicy(ENTRY_COUNT); } }
System.out.println("max-size: " + evictionConfig.getSize()); System.out.println("max-size-policy: " + evictionConfig.getMaximumSizePolicy()); System.out.println("eviction-policy: " + evictionConfig.getEvictionPolicy());
@Override protected EvictionChecker createNearCacheEvictionChecker(EvictionConfig evictionConfig, NearCacheConfig nearCacheConfig) { MaxSizePolicy maxSizePolicy = evictionConfig.getMaximumSizePolicy(); if (maxSizePolicy != MaxSizePolicy.ENTRY_COUNT) { throw new IllegalArgumentException(format("Invalid max-size policy (%s) for %s! Only %s is supported.", maxSizePolicy, getClass().getName(), MaxSizePolicy.ENTRY_COUNT)); } return new EntryCountNearCacheEvictionChecker(evictionConfig.getSize(), records); }
/** * Sets the eviction policy. * <p> * Valid values are: * <ul> * <li>{@code LRU} (Least Recently Used)</li> * <li>{@code LFU} (Least Frequently Used)</li> * <li>{@code NONE} (no extra eviction, time-to-live-seconds or max-idle-seconds may still apply)</li> * <li>{@code RANDOM} (random entry)</li> * </ul> * <p> * {@code LRU} is the default. * Regardless of the eviction policy used, time-to-live-seconds and max-idle-seconds will still apply. * * @param evictionPolicy the eviction policy for the Near Cache * @return this Near Cache config instance * @deprecated since 3.8, please use {@link #getEvictionConfig()} and {@link EvictionConfig#setEvictionPolicy(EvictionPolicy)} */ @Deprecated public NearCacheConfig setEvictionPolicy(String evictionPolicy) { this.evictionPolicy = checkNotNull(evictionPolicy, "Eviction policy cannot be null!"); this.evictionConfig.setEvictionPolicy(EvictionPolicy.valueOf(evictionPolicy)); this.evictionConfig.setMaximumSizePolicy(ENTRY_COUNT); return this; }
@Override public CacheEvictionConfig setSize(int size) { super.setSize(size); return this; }
@Override public void onCreate(final HazelcastCacheManager cacheManager, final Function<String, CacheConfig<?, ?>> provider) { cacheManager.createCache("nodes", provider.apply("nodes")); cacheManager.createCache("node-parameters", provider.apply("node-parameters")); cacheManager.createCache("services", provider.apply("services")); final CacheConfig<?, ?> tokens = provider.apply("curl-tokens"); tokens.setExpiryPolicyFactory(ModifiedExpiryPolicy.factoryOf(new Duration(HOURS, 10))); tokens.setEvictionConfig(new EvictionConfig() ); cacheManager.createCache("curl-tokens", tokens); cacheManager.createCache("subscription-parameters", provider.apply("subscription-parameters")); cacheManager.createCache("plugin-data", provider.apply("plugin-data")); }
/** * Checks if a {@link EvictionConfig} is valid in its context. * * @param evictionConfig the {@link EvictionConfig} * @param isNearCache {@code true} if the config is for a Near Cache, {@code false} otherwise */ @SuppressWarnings("ConstantConditions") public static void checkEvictionConfig(EvictionConfig evictionConfig, boolean isNearCache) { if (evictionConfig == null) { throw new IllegalArgumentException("Eviction config cannot be null!"); } EvictionPolicy evictionPolicy = evictionConfig.getEvictionPolicy(); String comparatorClassName = evictionConfig.getComparatorClassName(); EvictionPolicyComparator comparator = evictionConfig.getComparator(); checkEvictionConfig(evictionPolicy, comparatorClassName, comparator, isNearCache); }
@Override public CacheEvictionConfig setMaximumSizePolicy(MaxSizePolicy maxSizePolicy) { super.setMaximumSizePolicy(maxSizePolicy); return this; }
public NearCacheConfig(int timeToLiveSeconds, int maxIdleSeconds, boolean invalidateOnChange, InMemoryFormat inMemoryFormat, EvictionConfig evictionConfig) { this.inMemoryFormat = inMemoryFormat; this.invalidateOnChange = invalidateOnChange; this.timeToLiveSeconds = timeToLiveSeconds; this.maxIdleSeconds = maxIdleSeconds; this.maxSize = calculateMaxSize(maxSize); // EvictionConfig is not allowed to be null if (evictionConfig != null) { this.maxSize = evictionConfig.getSize(); this.evictionPolicy = evictionConfig.getEvictionPolicy().toString(); this.evictionConfig = evictionConfig; } }
/** * Checks the merge policy configuration in the context of an {@link ICache}. * * @param inMemoryFormat the {@link InMemoryFormat} of the cache * @param evictionConfig the {@link EvictionConfig} of the cache */ static void checkEvictionConfig(InMemoryFormat inMemoryFormat, EvictionConfig evictionConfig) { if (inMemoryFormat == NATIVE) { MaxSizePolicy maxSizePolicy = evictionConfig.getMaximumSizePolicy(); if (maxSizePolicy == MaxSizePolicy.ENTRY_COUNT) { throw new IllegalArgumentException("Invalid max-size policy " + '(' + maxSizePolicy + ") for NATIVE in-memory format! Only " + MaxSizePolicy.USED_NATIVE_MEMORY_SIZE + ", " + MaxSizePolicy.USED_NATIVE_MEMORY_PERCENTAGE + ", " + MaxSizePolicy.FREE_NATIVE_MEMORY_SIZE + ", " + MaxSizePolicy.FREE_NATIVE_MEMORY_PERCENTAGE + " are supported."); } } }
@Override public CacheEvictionConfig setEvictionPolicy(EvictionPolicy evictionPolicy) { super.setEvictionPolicy(evictionPolicy); return this; }
System.out.println("cache configured with eviction -> size:" + evictionConfig.getSize() + " policy:" + evictionConfig.getEvictionPolicyType());
@Override public CacheEvictionConfig setComparatorClassName(String comparatorClassName) { super.setComparatorClassName(comparatorClassName); return this; }
protected AbstractNearCacheRecordStore(NearCacheConfig nearCacheConfig, NearCacheStatsImpl nearCacheStats, SerializationService serializationService, ClassLoader classLoader) { this.nearCacheConfig = nearCacheConfig; this.timeToLiveMillis = nearCacheConfig.getTimeToLiveSeconds() * MILLI_SECONDS_IN_A_SECOND; this.maxIdleMillis = nearCacheConfig.getMaxIdleSeconds() * MILLI_SECONDS_IN_A_SECOND; this.serializationService = serializationService; this.classLoader = classLoader; this.nearCacheStats = nearCacheStats; this.evictionDisabled = nearCacheConfig.getEvictionConfig().getEvictionPolicy() == EvictionPolicy.NONE; }