The Unified and Autonomous Development Platform
Improve commits page performance
Robin Shene740c37fad7b
diff --git a/server-core/src/main/java/io/onedev/server/git/command/BlameCommand.java b/server-core/src/main/java/io/onedev/server/git/command/BlameCommand.javaindex fc83e1b68b..c41a618357 100644--- a/server-core/src/main/java/io/onedev/server/git/command/BlameCommand.java+++ b/server-core/src/main/java/io/onedev/server/git/command/BlameCommand.java@@ -84,7 +84,10 @@ public class BlameCommand extends GitCommand<Collection<BlameBlock>> {String cacheKey = commitHash + ":" + file + ":" + (range!=null?range.toString():"");- Collection<BlameBlock> cached = cache.get(cacheKey);+ Collection<BlameBlock> cached;+ synchronized (cache) {+ cached = cache.get(cacheKey);+ }if (cached != null)return cached;@@ -181,8 +184,9 @@ public class BlameCommand extends GitCommand<Collection<BlameBlock>> {block.getRanges().add(new LinearRange(beginLine.get(), endLine.get()-1));}- if (System.currentTimeMillis()-time > CACHE_THRESHOLD)+ if (System.currentTimeMillis()-time > CACHE_THRESHOLD) synchronized (cache) {cache.put(cacheKey, blocks.values());+ }return blocks.values();}diff --git a/server-core/src/main/java/io/onedev/server/model/Project.java b/server-core/src/main/java/io/onedev/server/model/Project.javaindex 4dacc1ee0d..6f495d92f9 100644--- a/server-core/src/main/java/io/onedev/server/model/Project.java+++ b/server-core/src/main/java/io/onedev/server/model/Project.java@@ -45,6 +45,8 @@ import javax.persistence.Table;import javax.persistence.UniqueConstraint;import javax.validation.Validator;+import org.apache.commons.collections4.map.ReferenceMap;+import org.apache.commons.collections4.map.AbstractReferenceMap.ReferenceStrength;import org.apache.commons.lang3.SerializationUtils;import org.apache.shiro.authz.Permission;import org.apache.tika.mime.MediaType;@@ -246,7 +248,10 @@ public class Project extends AbstractEntity {public static void pop() {stack.get().pop();}-++ private static final ReferenceMap<ObjectId, Optional<BuildSpec>> buildSpecCache =+ new ReferenceMap<>(ReferenceStrength.HARD, ReferenceStrength.SOFT);+@ManyToOne(fetch=FetchType.LAZY)@JoinColumn(nullable=true)@Api(description="Represents the project from which this project is forked. Remove this property if "@@ -387,8 +392,6 @@ public class Project extends AbstractEntity {private transient Map<String, Optional<ObjectId>> objectIdCache;- private transient Map<ObjectId, Optional<BuildSpec>> buildSpecCache;-private transient Map<ObjectId, Map<String, Collection<StatusInfo>>> commitStatusCache;private transient Map<ObjectId, Optional<RevCommit>> commitCache;@@ -858,9 +861,10 @@ public class Project extends AbstractEntity {*/@Nullablepublic BuildSpec getBuildSpec(ObjectId commitId) {- if (buildSpecCache == null)- buildSpecCache = new HashMap<>();- Optional<BuildSpec> buildSpec = buildSpecCache.get(commitId);+ Optional<BuildSpec> buildSpec;+ synchronized (buildSpecCache) {+ buildSpec = buildSpecCache.get(commitId);+ }if (buildSpec == null) {Blob blob = getBlob(new BlobIdent(commitId.name(), BuildSpec.BLOB_PATH, FileMode.TYPE_FILE), false);if (blob != null) {@@ -872,7 +876,9 @@ public class Project extends AbstractEntity {elsebuildSpec = Optional.absent();}- buildSpecCache.put(commitId, buildSpec);+ synchronized (buildSpecCache) {+ buildSpecCache.put(commitId, buildSpec);+ }}return buildSpec.orNull();}