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