您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RepositoryCacheConfig.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. /**
  65. * Create a default configuration.
  66. */
  67. public RepositoryCacheConfig() {
  68. expireAfterMillis = TimeUnit.HOURS.toMillis(1);
  69. cleanupDelayMillis = AUTO_CLEANUP_DELAY;
  70. }
  71. /**
  72. * Get the time an unused repository should be expired and be evicted from
  73. * the RepositoryCache in milliseconds.
  74. *
  75. * @return the time an unused repository should be expired and be evicted
  76. * from the RepositoryCache in milliseconds. <b>Default is 1
  77. * hour.</b>
  78. */
  79. public long getExpireAfter() {
  80. return expireAfterMillis;
  81. }
  82. /**
  83. * Set the time an unused repository should be expired and be evicted from
  84. * the RepositoryCache in milliseconds.
  85. *
  86. * @param expireAfterMillis
  87. * the time an unused repository should be expired and be evicted
  88. * from the RepositoryCache in milliseconds.
  89. */
  90. public void setExpireAfter(long expireAfterMillis) {
  91. this.expireAfterMillis = expireAfterMillis;
  92. }
  93. /**
  94. * Get the delay between the periodic cleanup of expired repository in
  95. * milliseconds.
  96. *
  97. * @return the delay between the periodic cleanup of expired repository in
  98. * milliseconds. <b>Default is minimum of 1/10 of expireAfterMillis
  99. * and 10 minutes</b>
  100. */
  101. public long getCleanupDelay() {
  102. if (cleanupDelayMillis < 0) {
  103. return Math.min(expireAfterMillis / 10,
  104. TimeUnit.MINUTES.toMillis(10));
  105. }
  106. return cleanupDelayMillis;
  107. }
  108. /**
  109. * Set the delay between the periodic cleanup of expired repository in
  110. * milliseconds.
  111. *
  112. * @param cleanupDelayMillis
  113. * the delay between the periodic cleanup of expired repository
  114. * in milliseconds. Set it to {@link #AUTO_CLEANUP_DELAY} to
  115. * automatically derive cleanup delay from expireAfterMillis.
  116. * <p>
  117. * Set it to {@link #NO_CLEANUP} in order to switch off cache
  118. * expiration.
  119. * <p>
  120. * If cache expiration is switched off the JVM still can evict
  121. * cache entries when the JVM is running low on available heap
  122. * memory.
  123. */
  124. public void setCleanupDelay(long cleanupDelayMillis) {
  125. this.cleanupDelayMillis = cleanupDelayMillis;
  126. }
  127. /**
  128. * Update properties by setting fields from the configuration.
  129. * <p>
  130. * If a property is not defined in the configuration, then it is left
  131. * unmodified.
  132. *
  133. * @param config
  134. * configuration to read properties from.
  135. * @return {@code this}.
  136. */
  137. public RepositoryCacheConfig fromConfig(Config config) {
  138. setExpireAfter(
  139. config.getTimeUnit("core", null, "repositoryCacheExpireAfter", //$NON-NLS-1$//$NON-NLS-2$
  140. getExpireAfter(), TimeUnit.MILLISECONDS));
  141. setCleanupDelay(
  142. config.getTimeUnit("core", null, "repositoryCacheCleanupDelay", //$NON-NLS-1$ //$NON-NLS-2$
  143. AUTO_CLEANUP_DELAY, TimeUnit.MILLISECONDS));
  144. return this;
  145. }
  146. /**
  147. * Install this configuration as the live settings.
  148. * <p>
  149. * The new configuration is applied immediately.
  150. */
  151. public void install() {
  152. RepositoryCache.reconfigure(this);
  153. }
  154. }