Add getCacheKey() to CurrentUser and its implementations

getCacheKey() can be used when grouping objects by user in caches. The
first user of this new method is a PerThreadCache added in a later commit.

Change-Id: I00a9c1528e2fdd2219f511ba749074efe9d967b6
This commit is contained in:
Patrick Hiesel
2018-04-18 09:59:32 +02:00
parent 8138ae974a
commit 867b2aa941
9 changed files with 43 additions and 1 deletions

View File

@@ -26,6 +26,12 @@ public class AnonymousUser extends CurrentUser {
return new ListGroupMembership(Collections.singleton(SystemGroupBackend.ANONYMOUS_USERS)); return new ListGroupMembership(Collections.singleton(SystemGroupBackend.ANONYMOUS_USERS));
} }
@Override
public Object getCacheKey() {
// Treat all anonymous users as a single user
return "anonymous";
}
@Override @Override
public String toString() { public String toString() {
return "ANONYMOUS"; return "ANONYMOUS";

View File

@@ -90,6 +90,12 @@ public abstract class CurrentUser {
*/ */
public abstract GroupMembership getEffectiveGroups(); public abstract GroupMembership getEffectiveGroups();
/**
* Returns a unique identifier for this user that is intended to be used as a cache key. Returned
* object should to implement {@code equals()} and {@code hashCode()} for effective caching.
*/
public abstract Object getCacheKey();
/** Unique name of the user on this server, if one has been assigned. */ /** Unique name of the user on this server, if one has been assigned. */
public Optional<String> getUserName() { public Optional<String> getUserName() {
return Optional.empty(); return Optional.empty();

View File

@@ -382,6 +382,11 @@ public class IdentifiedUser extends CurrentUser {
return effectiveGroups; return effectiveGroups;
} }
@Override
public Object getCacheKey() {
return getAccountId();
}
public PersonIdent newRefLogIdent() { public PersonIdent newRefLogIdent() {
return newRefLogIdent(new Date(), TimeZone.getDefault()); return newRefLogIdent(new Date(), TimeZone.getDefault());
} }

View File

@@ -35,6 +35,11 @@ public class InternalUser extends CurrentUser {
return GroupMembership.EMPTY; return GroupMembership.EMPTY;
} }
@Override
public String getCacheKey() {
return "internal";
}
@Override @Override
public boolean isInternalUser() { public boolean isInternalUser() {
return true; return true;

View File

@@ -40,6 +40,11 @@ public class PeerDaemonUser extends CurrentUser {
return GroupMembership.EMPTY; return GroupMembership.EMPTY;
} }
@Override
public Object getCacheKey() {
return getRemoteAddress();
}
public SocketAddress getRemoteAddress() { public SocketAddress getRemoteAddress() {
return peer; return peer;
} }

View File

@@ -36,4 +36,9 @@ public final class SingleGroupUser extends CurrentUser {
public GroupMembership getEffectiveGroups() { public GroupMembership getEffectiveGroups() {
return groups; return groups;
} }
@Override
public Object getCacheKey() {
return groups.getKnownGroups();
}
} }

View File

@@ -50,6 +50,11 @@ public class UiActionsTest {
throw new UnsupportedOperationException("not implemented"); throw new UnsupportedOperationException("not implemented");
} }
@Override
public Object getCacheKey() {
return new Object();
}
@Override @Override
public boolean isIdentifiedUser() { public boolean isIdentifiedUser() {
return true; return true;

View File

@@ -1012,6 +1012,11 @@ public class RefControlTest {
return groups; return groups;
} }
@Override
public Object getCacheKey() {
return new Object();
}
@Override @Override
public Optional<String> getUserName() { public Optional<String> getUserName() {
return Optional.ofNullable(username); return Optional.ofNullable(username);