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
12 import java.util.concurrent.TimeUnit;
13
14 /**
15 * Configuration parameters for JVM-wide repository cache used by JGit.
16 *
17 * @since 4.4
18 */
19 public class RepositoryCacheConfig {
20
21 /**
22 * Set cleanupDelayMillis to this value in order to switch off time-based
23 * cache eviction. Expired cache entries will only be evicted when
24 * RepositoryCache.clearExpired or RepositoryCache.clear are called.
25 */
26 public static final long NO_CLEANUP = 0;
27
28 /**
29 * Set cleanupDelayMillis to this value in order to auto-set it to minimum
30 * of 1/10 of expireAfterMillis and 10 minutes
31 */
32 public static final long AUTO_CLEANUP_DELAY = -1;
33
34 private long expireAfterMillis;
35
36 private long cleanupDelayMillis;
37
38 /**
39 * Create a default configuration.
40 */
41 public RepositoryCacheConfig() {
42 expireAfterMillis = TimeUnit.HOURS.toMillis(1);
43 cleanupDelayMillis = AUTO_CLEANUP_DELAY;
44 }
45
46 /**
47 * Get the time an unused repository should be expired and be evicted from
48 * the RepositoryCache in milliseconds.
49 *
50 * @return the time an unused repository should be expired and be evicted
51 * from the RepositoryCache in milliseconds. <b>Default is 1
52 * hour.</b>
53 */
54 public long getExpireAfter() {
55 return expireAfterMillis;
56 }
57
58 /**
59 * Set the time an unused repository should be expired and be evicted from
60 * the RepositoryCache in milliseconds.
61 *
62 * @param expireAfterMillis
63 * the time an unused repository should be expired and be evicted
64 * from the RepositoryCache in milliseconds.
65 */
66 public void setExpireAfter(long expireAfterMillis) {
67 this.expireAfterMillis = expireAfterMillis;
68 }
69
70 /**
71 * Get the delay between the periodic cleanup of expired repository in
72 * milliseconds.
73 *
74 * @return the delay between the periodic cleanup of expired repository in
75 * milliseconds. <b>Default is minimum of 1/10 of expireAfterMillis
76 * and 10 minutes</b>
77 */
78 public long getCleanupDelay() {
79 if (cleanupDelayMillis < 0) {
80 return Math.min(expireAfterMillis / 10,
81 TimeUnit.MINUTES.toMillis(10));
82 }
83 return cleanupDelayMillis;
84 }
85
86 /**
87 * Set the delay between the periodic cleanup of expired repository in
88 * milliseconds.
89 *
90 * @param cleanupDelayMillis
91 * the delay between the periodic cleanup of expired repository
92 * in milliseconds. Set it to {@link #AUTO_CLEANUP_DELAY} to
93 * automatically derive cleanup delay from expireAfterMillis.
94 * <p>
95 * Set it to {@link #NO_CLEANUP} in order to switch off cache
96 * expiration.
97 * <p>
98 * If cache expiration is switched off the JVM still can evict
99 * cache entries when the JVM is running low on available heap
100 * memory.
101 */
102 public void setCleanupDelay(long cleanupDelayMillis) {
103 this.cleanupDelayMillis = cleanupDelayMillis;
104 }
105
106 /**
107 * Update properties by setting fields from the configuration.
108 * <p>
109 * If a property is not defined in the configuration, then it is left
110 * unmodified.
111 *
112 * @param config
113 * configuration to read properties from.
114 * @return {@code this}.
115 */
116 public RepositoryCacheConfig fromConfig(Config config) {
117 setExpireAfter(
118 config.getTimeUnit("core", null, "repositoryCacheExpireAfter", //$NON-NLS-1$//$NON-NLS-2$
119 getExpireAfter(), TimeUnit.MILLISECONDS));
120 setCleanupDelay(
121 config.getTimeUnit("core", null, "repositoryCacheCleanupDelay", //$NON-NLS-1$ //$NON-NLS-2$
122 AUTO_CLEANUP_DELAY, TimeUnit.MILLISECONDS));
123 return this;
124 }
125
126 /**
127 * Install this configuration as the live settings.
128 * <p>
129 * The new configuration is applied immediately.
130 */
131 public void install() {
132 RepositoryCache.reconfigure(this);
133 }
134 }