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": case "--jetty_idle_timeout":
jettyIdleTimeout = dim[1]; jettyIdleTimeout = dim[1];
break; break;
case "--disable_cache":
System.setProperty("gitbucket.disableCache", dim[1]);
break;
} }
} }
} }

View File

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