PerThreadCache: Add static convenience method
Change-Id: Id599161b12c7b0e2c39ed28f01ff769d9f71b91e
This commit is contained in:
@@ -104,6 +104,11 @@ public class PerThreadCache implements AutoCloseable {
|
||||
return CACHE.get();
|
||||
}
|
||||
|
||||
public static <T> T getOrCompute(Key<T> key, Supplier<T> loader) {
|
||||
PerThreadCache cache = get();
|
||||
return cache != null ? cache.get(key, loader) : loader.get();
|
||||
}
|
||||
|
||||
private final Map<Key<?>, Object> cache = Maps.newHashMapWithExpectedSize(10);
|
||||
|
||||
private PerThreadCache() {}
|
||||
|
@@ -109,14 +109,10 @@ public class DefaultPermissionBackend extends PermissionBackend {
|
||||
try {
|
||||
ProjectState state = projectCache.checkedGet(project);
|
||||
if (state != null) {
|
||||
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));
|
||||
PerThreadCache.getOrCompute(
|
||||
PerThreadCache.Key.create(ProjectControl.class, project, user.getCacheKey()),
|
||||
() -> projectControlFactory.create(user, state));
|
||||
return control.asForProject().database(db);
|
||||
}
|
||||
return FailedPermissionBackend.project("not found", new NoSuchProjectException(project));
|
||||
|
Reference in New Issue
Block a user