Implement a PerThreadCache
This commit implements a per-request cache as a per-thread cache. PerThreadCache is intended to cache objects that have a high instantiation cost, are specific to the current request and potentially need to be instantiated multiple times while serving a request. The implementation uses a ThreadLocal which limits the cache to the serving thread. Given that fan-outs (like ChangeJson's executor) run with high parallelization, it seems acceptable to re-do some instantiation there in lieu of this cache. This commit uses a the new cache in DefaultPermissionBackend to cache a ProjectControl instance. Change-Id: Id5de21ed8941c7e08eb37197b16ce2cadece7aea
This commit is contained in:
@@ -31,6 +31,7 @@ import com.google.gerrit.server.CurrentUser;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.PeerDaemonUser;
|
||||
import com.google.gerrit.server.account.CapabilityCollection;
|
||||
import com.google.gerrit.server.cache.PerThreadCache;
|
||||
import com.google.gerrit.server.project.NoSuchProjectException;
|
||||
import com.google.gerrit.server.project.ProjectCache;
|
||||
import com.google.gerrit.server.project.ProjectState;
|
||||
@@ -108,7 +109,15 @@ public class DefaultPermissionBackend extends PermissionBackend {
|
||||
try {
|
||||
ProjectState state = projectCache.checkedGet(project);
|
||||
if (state != null) {
|
||||
return projectControlFactory.create(user, state).asForProject().database(db);
|
||||
PerThreadCache perThreadCache = PerThreadCache.get();
|
||||
if (perThreadCache == null) {
|
||||
return projectControlFactory.create(user, state).asForProject().database(db);
|
||||
}
|
||||
PerThreadCache.Key<ProjectControl> cacheKey =
|
||||
PerThreadCache.Key.create(ProjectControl.class, project, user.getCacheKey());
|
||||
ProjectControl control =
|
||||
perThreadCache.get(cacheKey, () -> projectControlFactory.create(user, state));
|
||||
return control.asForProject().database(db);
|
||||
}
|
||||
return FailedPermissionBackend.project("not found", new NoSuchProjectException(project));
|
||||
} catch (IOException e) {
|
||||
|
Reference in New Issue
Block a user