View Javadoc
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  
45  import java.util.concurrent.TimeUnit;
46  
47  /**
48   * Configuration parameters for JVM-wide repository cache used by JGit.
49   *
50   * @since 4.4
51   */
52  public class RepositoryCacheConfig {
53  
54  	/**
55  	 * Set cleanupDelayMillis to this value in order to switch off time-based
56  	 * cache eviction. Expired cache entries will only be evicted when
57  	 * RepositoryCache.clearExpired or RepositoryCache.clear are called.
58  	 */
59  	public static final long NO_CLEANUP = 0;
60  
61  	/**
62  	 * Set cleanupDelayMillis to this value in order to auto-set it to minimum
63  	 * of 1/10 of expireAfterMillis and 10 minutes
64  	 */
65  	public static final long AUTO_CLEANUP_DELAY = -1;
66  
67  	private long expireAfterMillis;
68  
69  	private long cleanupDelayMillis;
70  
71  	/**
72  	 * Create a default configuration.
73  	 */
74  	public RepositoryCacheConfig() {
75  		expireAfterMillis = TimeUnit.HOURS.toMillis(1);
76  		cleanupDelayMillis = AUTO_CLEANUP_DELAY;
77  	}
78  
79  	/**
80  	 * Get the time an unused repository should be expired and be evicted from
81  	 * the RepositoryCache in milliseconds.
82  	 *
83  	 * @return the time an unused repository should be expired and be evicted
84  	 *         from the RepositoryCache in milliseconds. <b>Default is 1
85  	 *         hour.</b>
86  	 */
87  	public long getExpireAfter() {
88  		return expireAfterMillis;
89  	}
90  
91  	/**
92  	 * Set the time an unused repository should be expired and be evicted from
93  	 * the RepositoryCache in milliseconds.
94  	 *
95  	 * @param expireAfterMillis
96  	 *            the time an unused repository should be expired and be evicted
97  	 *            from the RepositoryCache in milliseconds.
98  	 */
99  	public void setExpireAfter(long expireAfterMillis) {
100 		this.expireAfterMillis = expireAfterMillis;
101 	}
102 
103 	/**
104 	 * Get the delay between the periodic cleanup of expired repository in
105 	 * milliseconds.
106 	 *
107 	 * @return the delay between the periodic cleanup of expired repository in
108 	 *         milliseconds. <b>Default is minimum of 1/10 of expireAfterMillis
109 	 *         and 10 minutes</b>
110 	 */
111 	public long getCleanupDelay() {
112 		if (cleanupDelayMillis < 0) {
113 			return Math.min(expireAfterMillis / 10,
114 					TimeUnit.MINUTES.toMillis(10));
115 		}
116 		return cleanupDelayMillis;
117 	}
118 
119 	/**
120 	 * Set the delay between the periodic cleanup of expired repository in
121 	 * milliseconds.
122 	 *
123 	 * @param cleanupDelayMillis
124 	 *            the delay between the periodic cleanup of expired repository
125 	 *            in milliseconds. Set it to {@link #AUTO_CLEANUP_DELAY} to
126 	 *            automatically derive cleanup delay from expireAfterMillis.
127 	 *            <p>
128 	 *            Set it to {@link #NO_CLEANUP} in order to switch off cache
129 	 *            expiration.
130 	 *            <p>
131 	 *            If cache expiration is switched off the JVM still can evict
132 	 *            cache entries when the JVM is running low on available heap
133 	 *            memory.
134 	 */
135 	public void setCleanupDelay(long cleanupDelayMillis) {
136 		this.cleanupDelayMillis = cleanupDelayMillis;
137 	}
138 
139 	/**
140 	 * Update properties by setting fields from the configuration.
141 	 * <p>
142 	 * If a property is not defined in the configuration, then it is left
143 	 * unmodified.
144 	 *
145 	 * @param config
146 	 *            configuration to read properties from.
147 	 * @return {@code this}.
148 	 */
149 	public RepositoryCacheConfig fromConfig(Config config) {
150 		setExpireAfter(
151 				config.getTimeUnit("core", null, "repositoryCacheExpireAfter", //$NON-NLS-1$//$NON-NLS-2$
152 						getExpireAfter(), TimeUnit.MILLISECONDS));
153 		setCleanupDelay(
154 				config.getTimeUnit("core", null, "repositoryCacheCleanupDelay", //$NON-NLS-1$ //$NON-NLS-2$
155 						AUTO_CLEANUP_DELAY, TimeUnit.MILLISECONDS));
156 		return this;
157 	}
158 
159 	/**
160 	 * Install this configuration as the live settings.
161 	 * <p>
162 	 * The new configuration is applied immediately.
163 	 */
164 	public void install() {
165 		RepositoryCache.reconfigure(this);
166 	}
167 }