Cache Not Found responses for retrieving avatar images
If an avatar image is not found it is likely that a moment later it is still not found. Allowing to cache the Not Found response saves requests to the server. If an avatar image gets available it is okay to have some delay until it is refetched from the server. Change-Id: I24ed6be3ea730fc604be036542638cad5118d6d4 Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
@@ -31,4 +31,10 @@ public class ResourceNotFoundException extends RestApiException {
|
||||
public ResourceNotFoundException(IdString id) {
|
||||
super(id.get());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public ResourceNotFoundException caching(CacheControl c) {
|
||||
return super.caching(c);
|
||||
}
|
||||
}
|
||||
|
@@ -16,6 +16,7 @@ package com.google.gerrit.server.account;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.gerrit.extensions.registration.DynamicItem;
|
||||
import com.google.gerrit.extensions.restapi.CacheControl;
|
||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||
import com.google.gerrit.extensions.restapi.Response;
|
||||
import com.google.gerrit.extensions.restapi.RestReadView;
|
||||
@@ -24,6 +25,8 @@ import com.google.inject.Inject;
|
||||
|
||||
import org.kohsuke.args4j.Option;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
class GetAvatar implements RestReadView<AccountResource> {
|
||||
private final DynamicItem<AvatarProvider> avatarProvider;
|
||||
|
||||
@@ -41,12 +44,14 @@ class GetAvatar implements RestReadView<AccountResource> {
|
||||
throws ResourceNotFoundException {
|
||||
AvatarProvider impl = avatarProvider.get();
|
||||
if (impl == null) {
|
||||
throw new ResourceNotFoundException();
|
||||
throw (new ResourceNotFoundException())
|
||||
.caching(CacheControl.PUBLIC(1, TimeUnit.DAYS));
|
||||
}
|
||||
|
||||
String url = impl.getUrl(rsrc.getUser(), size);
|
||||
if (Strings.isNullOrEmpty(url)) {
|
||||
throw new ResourceNotFoundException();
|
||||
throw (new ResourceNotFoundException())
|
||||
.caching(CacheControl.PUBLIC(1, TimeUnit.HOURS));
|
||||
} else {
|
||||
return Response.redirect(url);
|
||||
}
|
||||
|
Reference in New Issue
Block a user