All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.ebeaninternal.server.cache.DefaultServerCacheConfig Maven / Gradle / Ivy

There is a newer version: 15.8.0
Show newest version
package io.ebeaninternal.server.cache;

import io.ebean.cache.QueryCacheEntryValidate;
import io.ebean.cache.ServerCacheConfig;
import io.ebean.cache.ServerCacheOptions;
import io.ebeaninternal.server.cache.DefaultServerCache.CacheEntry;

import java.lang.ref.SoftReference;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public final class DefaultServerCacheConfig {

  private final ServerCacheConfig config;
  private final int maxSize;
  private final int maxIdleSecs;
  private final int maxSecsToLive;
  private final int trimFrequency;
  private final Map> map;

  public DefaultServerCacheConfig(ServerCacheConfig config) {
    this(config,  new ConcurrentHashMap<>());
  }

  public DefaultServerCacheConfig(ServerCacheConfig config, Map> map) {
    this.config = config;
    this.map = map;

    ServerCacheOptions options = config.getCacheOptions();
    this.maxIdleSecs = options.getMaxIdleSecs();
    this.maxSecsToLive = options.getMaxSecsToLive();
    this.trimFrequency = options.getTrimFrequency();
    this.maxSize = options.getMaxSize();
  }

  public QueryCacheEntryValidate getQueryCacheEntryValidate() {
    return config.getQueryCacheEntryValidate();
  }

  public String getName() {
    return config.getCacheKey();
  }

  public String getShortName() {
    return config.getShortName();
  }

  public Map> getMap() {
    return map;
  }

  public int getMaxSize() {
    return maxSize;
  }

  public int getMaxIdleSecs() {
    return maxIdleSecs;
  }

  public int getMaxSecsToLive() {
    return maxSecsToLive;
  }

  /**
   * Determine a good trimFrequency as half of maxIdleSecs (or maxSecsToLive).
   */
  public int determineTrimFrequency() {
    if (trimFrequency > 0) {
      return trimFrequency;
    }
    if (maxIdleSecs > 0) {
      return maxIdleSecs / 2 - 1;
    }
    if (maxSecsToLive > 0) {
      return maxSecsToLive / 2 - 1;
    }
    return 0;
  }

  /**
   * Determine the number of mutations/puts required to trigger a runEviction() in the foreground.
   */
  public long determineTrimOnPut() {
    if (maxSize > 0) {
      return maxSize / 10;
    }
    return 1000;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy