Add --disable_cache option to disable cache (#3245)

This commit is contained in:
Naoki Takezoe
2023-02-24 09:42:17 +09:00
committed by GitHub
parent 445329c07a
commit 6b8c4cf8d0
2 changed files with 49 additions and 26 deletions

View File

@@ -111,6 +111,9 @@ public class JettyLauncher {
case "--jetty_idle_timeout":
jettyIdleTimeout = dim[1];
break;
case "--disable_cache":
System.setProperty("gitbucket.disableCache", dim[1]);
break;
}
}
}

View File

@@ -1,7 +1,6 @@
package gitbucket.core.util
import java.io._
import gitbucket.core.service.RepositoryService
import org.eclipse.jgit.api.Git
import Directory._
@@ -18,10 +17,10 @@ import org.eclipse.jgit.treewalk.filter._
import org.eclipse.jgit.diff.DiffEntry.ChangeType
import org.eclipse.jgit.errors.{ConfigInvalidException, IncorrectObjectTypeException, MissingObjectException}
import org.eclipse.jgit.transport.RefSpec
import java.util.Date
import java.util.concurrent.TimeUnit
import org.cache2k.Cache2kBuilder
import org.cache2k.{Cache, Cache2kBuilder}
import org.eclipse.jgit.api.errors._
import org.eclipse.jgit.diff.{DiffEntry, DiffFormatter, RawTextComparator}
import org.eclipse.jgit.dircache.DirCacheEntry
@@ -40,6 +39,9 @@ object JGitUtil {
private implicit val objectDatabaseReleasable: Releasable[ObjectDatabase] =
_.close()
private def isCacheEnabled(): Boolean =
ConfigUtil.getConfigValue[Boolean]("gitbucket.disableCache").getOrElse(false)
/**
* The repository data.
*
@@ -284,18 +286,25 @@ object JGitUtil {
revCommit
}
private val cache = new Cache2kBuilder[String, Int]() {}
private val cache: Cache[String, Int] = if (isCacheEnabled()) {
Cache2kBuilder
.of(classOf[String], classOf[Int])
.name("commit-count")
.expireAfterWrite(24, TimeUnit.HOURS)
.entryCapacity(10000)
.build()
} else null
private val objectCommitCache = new Cache2kBuilder[ObjectId, RevCommit]() {}
private val objectCommitCache: Cache[ObjectId, RevCommit] = if (isCacheEnabled()) {
Cache2kBuilder
.of(classOf[ObjectId], classOf[RevCommit])
.name("object-commit")
.entryCapacity(10000)
.build()
} else null
def removeCache(git: Git): Unit = {
if (isCacheEnabled()) {
val dir = git.getRepository.getDirectory
val keyPrefix = dir.getAbsolutePath + "@"
@@ -305,6 +314,7 @@ object JGitUtil {
}
})
}
}
/**
* Returns the number of commits in the specified branch or commit.
@@ -312,6 +322,8 @@ object JGitUtil {
*/
def getCommitCount(git: Git, branch: String, max: Int = 10001): Int = {
val dir = git.getRepository.getDirectory
if (isCacheEnabled()) {
val key = dir.getAbsolutePath + "@" + branch
val entry = cache.getEntry(key)
@@ -323,6 +335,11 @@ object JGitUtil {
} else {
entry.getValue
}
} else {
val commitId = git.getRepository.resolve(branch)
val commitCount = git.log.add(commitId).call.iterator.asScala.take(max).size
commitCount
}
}
/**
@@ -444,7 +461,7 @@ object JGitUtil {
(id, mode, name, path, opt, None)
} else if (commitCount < 10000) {
(id, mode, name, path, opt, Some(getCommit(path)))
} else {
} else if (isCacheEnabled()) {
// Use in-memory cache if the commit count is too big.
val cached = objectCommitCache.getEntry(id)
if (cached == null) {
@@ -454,6 +471,9 @@ object JGitUtil {
} else {
(id, mode, name, path, opt, Some(cached.getValue))
}
} else {
val commit = getCommit(path)
(id, mode, name, path, opt, Some(commit))
}
}
}