RepositoryCacheConfig.java

  1. /*
  2.  * Copyright (C) 2016 Ericsson and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.lib;

  11. import java.util.concurrent.TimeUnit;

  12. /**
  13.  * Configuration parameters for JVM-wide repository cache used by JGit.
  14.  *
  15.  * @since 4.4
  16.  */
  17. public class RepositoryCacheConfig {

  18.     /**
  19.      * Set cleanupDelayMillis to this value in order to switch off time-based
  20.      * cache eviction. Expired cache entries will only be evicted when
  21.      * RepositoryCache.clearExpired or RepositoryCache.clear are called.
  22.      */
  23.     public static final long NO_CLEANUP = 0;

  24.     /**
  25.      * Set cleanupDelayMillis to this value in order to auto-set it to minimum
  26.      * of 1/10 of expireAfterMillis and 10 minutes
  27.      */
  28.     public static final long AUTO_CLEANUP_DELAY = -1;

  29.     private long expireAfterMillis;

  30.     private long cleanupDelayMillis;

  31.     /**
  32.      * Create a default configuration.
  33.      */
  34.     public RepositoryCacheConfig() {
  35.         expireAfterMillis = TimeUnit.HOURS.toMillis(1);
  36.         cleanupDelayMillis = AUTO_CLEANUP_DELAY;
  37.     }

  38.     /**
  39.      * Get the time an unused repository should be expired and be evicted from
  40.      * the RepositoryCache in milliseconds.
  41.      *
  42.      * @return the time an unused repository should be expired and be evicted
  43.      *         from the RepositoryCache in milliseconds. <b>Default is 1
  44.      *         hour.</b>
  45.      */
  46.     public long getExpireAfter() {
  47.         return expireAfterMillis;
  48.     }

  49.     /**
  50.      * Set the time an unused repository should be expired and be evicted from
  51.      * the RepositoryCache in milliseconds.
  52.      *
  53.      * @param expireAfterMillis
  54.      *            the time an unused repository should be expired and be evicted
  55.      *            from the RepositoryCache in milliseconds.
  56.      */
  57.     public void setExpireAfter(long expireAfterMillis) {
  58.         this.expireAfterMillis = expireAfterMillis;
  59.     }

  60.     /**
  61.      * Get the delay between the periodic cleanup of expired repository in
  62.      * milliseconds.
  63.      *
  64.      * @return the delay between the periodic cleanup of expired repository in
  65.      *         milliseconds. <b>Default is minimum of 1/10 of expireAfterMillis
  66.      *         and 10 minutes</b>
  67.      */
  68.     public long getCleanupDelay() {
  69.         if (cleanupDelayMillis < 0) {
  70.             return Math.min(expireAfterMillis / 10,
  71.                     TimeUnit.MINUTES.toMillis(10));
  72.         }
  73.         return cleanupDelayMillis;
  74.     }

  75.     /**
  76.      * Set the delay between the periodic cleanup of expired repository in
  77.      * milliseconds.
  78.      *
  79.      * @param cleanupDelayMillis
  80.      *            the delay between the periodic cleanup of expired repository
  81.      *            in milliseconds. Set it to {@link #AUTO_CLEANUP_DELAY} to
  82.      *            automatically derive cleanup delay from expireAfterMillis.
  83.      *            <p>
  84.      *            Set it to {@link #NO_CLEANUP} in order to switch off cache
  85.      *            expiration.
  86.      *            <p>
  87.      *            If cache expiration is switched off the JVM still can evict
  88.      *            cache entries when the JVM is running low on available heap
  89.      *            memory.
  90.      */
  91.     public void setCleanupDelay(long cleanupDelayMillis) {
  92.         this.cleanupDelayMillis = cleanupDelayMillis;
  93.     }

  94.     /**
  95.      * Update properties by setting fields from the configuration.
  96.      * <p>
  97.      * If a property is not defined in the configuration, then it is left
  98.      * unmodified.
  99.      *
  100.      * @param config
  101.      *            configuration to read properties from.
  102.      * @return {@code this}.
  103.      */
  104.     public RepositoryCacheConfig fromConfig(Config config) {
  105.         setExpireAfter(
  106.                 config.getTimeUnit("core", null, "repositoryCacheExpireAfter", //$NON-NLS-1$//$NON-NLS-2$
  107.                         getExpireAfter(), TimeUnit.MILLISECONDS));
  108.         setCleanupDelay(
  109.                 config.getTimeUnit("core", null, "repositoryCacheCleanupDelay", //$NON-NLS-1$ //$NON-NLS-2$
  110.                         AUTO_CLEANUP_DELAY, TimeUnit.MILLISECONDS));
  111.         return this;
  112.     }

  113.     /**
  114.      * Install this configuration as the live settings.
  115.      * <p>
  116.      * The new configuration is applied immediately.
  117.      */
  118.     public void install() {
  119.         RepositoryCache.reconfigure(this);
  120.     }
  121. }