You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RepositoryCacheConfig.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2016 Ericsson
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.lib;
  44. import java.util.concurrent.TimeUnit;
  45. /**
  46. * Configuration parameters for JVM-wide repository cache used by JGit.
  47. *
  48. * @since 4.4
  49. */
  50. public class RepositoryCacheConfig {
  51. /**
  52. * Set cleanupDelayMillis to this value in order to switch off time-based
  53. * cache eviction. Expired cache entries will only be evicted when
  54. * RepositoryCache.clearExpired or RepositoryCache.clear are called.
  55. */
  56. public static final long NO_CLEANUP = 0;
  57. /**
  58. * Set cleanupDelayMillis to this value in order to auto-set it to minimum
  59. * of 1/10 of expireAfterMillis and 10 minutes
  60. */
  61. public static final long AUTO_CLEANUP_DELAY = -1;
  62. private long expireAfterMillis;
  63. private long cleanupDelayMillis;
  64. /** Create a default configuration. */
  65. public RepositoryCacheConfig() {
  66. expireAfterMillis = TimeUnit.HOURS.toMillis(1);
  67. cleanupDelayMillis = AUTO_CLEANUP_DELAY;
  68. }
  69. /**
  70. * @return the time an unused repository should expired and be evicted from
  71. * the RepositoryCache in milliseconds. <b>Default is 1 hour.</b>
  72. */
  73. public long getExpireAfter() {
  74. return expireAfterMillis;
  75. }
  76. /**
  77. * @param expireAfterMillis
  78. * the time an unused repository should expired and be evicted
  79. * from the RepositoryCache in milliseconds.
  80. */
  81. public void setExpireAfter(long expireAfterMillis) {
  82. this.expireAfterMillis = expireAfterMillis;
  83. }
  84. /**
  85. * @return the delay between the periodic cleanup of expired repository in
  86. * milliseconds. <b>Default is minimum of 1/10 of expireAfterMillis
  87. * and 10 minutes</b>
  88. */
  89. public long getCleanupDelay() {
  90. if (cleanupDelayMillis < 0) {
  91. return Math.min(expireAfterMillis / 10,
  92. TimeUnit.MINUTES.toMillis(10));
  93. }
  94. return cleanupDelayMillis;
  95. }
  96. /**
  97. * @param cleanupDelayMillis
  98. * the delay between the periodic cleanup of expired repository
  99. * in milliseconds. Set it to {@link #AUTO_CLEANUP_DELAY} to
  100. * automatically derive cleanup delay from expireAfterMillis.
  101. * <p>
  102. * Set it to {@link #NO_CLEANUP} in order to switch off cache
  103. * expiration.
  104. * <p>
  105. * If cache expiration is switched off the JVM still can evict
  106. * cache entries when the JVM is running low on available heap
  107. * memory.
  108. */
  109. public void setCleanupDelay(long cleanupDelayMillis) {
  110. this.cleanupDelayMillis = cleanupDelayMillis;
  111. }
  112. /**
  113. * Update properties by setting fields from the configuration.
  114. * <p>
  115. * If a property is not defined in the configuration, then it is left
  116. * unmodified.
  117. *
  118. * @param config
  119. * configuration to read properties from.
  120. * @return {@code this}.
  121. */
  122. public RepositoryCacheConfig fromConfig(Config config) {
  123. setExpireAfter(
  124. config.getTimeUnit("core", null, "repositoryCacheExpireAfter", //$NON-NLS-1$//$NON-NLS-2$
  125. getExpireAfter(), TimeUnit.MILLISECONDS));
  126. setCleanupDelay(
  127. config.getTimeUnit("core", null, "repositoryCacheCleanupDelay", //$NON-NLS-1$ //$NON-NLS-2$
  128. AUTO_CLEANUP_DELAY, TimeUnit.MILLISECONDS));
  129. return this;
  130. }
  131. /**
  132. * Install this configuration as the live settings.
  133. * <p>
  134. * The new configuration is applied immediately.
  135. */
  136. public void install() {
  137. RepositoryCache.reconfigure(this);
  138. }
  139. }