Invalidate premium list cache on update

This will only affect the tools service, the primary use case being (1) I go to
create a domain through nomulus tool, realize it's premium, (2) update the
premium list to not include that domain, (3) kill the tools service instance to
wipe out the cached premium value, then (4) create the domain at standard. This
commit eliminates step 3.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=226180160
This commit is contained in:
mcilwain 2018-12-19 08:52:23 -08:00 committed by Michael Muller
parent 4a4989e2a5
commit 56b61ad5a2
3 changed files with 38 additions and 13 deletions

View file

@ -132,16 +132,22 @@ public final class PremiumList extends BaseDomainLabelList<Money, PremiumList.Pr
* <p>This is cached for a shorter duration because we need to periodically reload this entity to
* check if a new revision has been published, and if so, then use that.
*/
static final LoadingCache<String, PremiumList> cachePremiumLists =
CacheBuilder.newBuilder()
.expireAfterWrite(getDomainLabelListCacheDuration().getMillis(), MILLISECONDS)
.build(
new CacheLoader<String, PremiumList>() {
@Override
public PremiumList load(final String name) {
return ofy().doTransactionless(() -> loadPremiumList(name));
}
});
@NonFinalForTesting
static LoadingCache<String, PremiumList> cachePremiumLists =
createCachePremiumLists(getDomainLabelListCacheDuration());
@VisibleForTesting
static LoadingCache<String, PremiumList> createCachePremiumLists(Duration cachePersistDuration) {
return CacheBuilder.newBuilder()
.expireAfterWrite(cachePersistDuration.getMillis(), MILLISECONDS)
.build(
new CacheLoader<String, PremiumList>() {
@Override
public PremiumList load(final String name) {
return ofy().doTransactionless(() -> loadPremiumList(name));
}
});
}
private static PremiumList loadPremiumList(String name) {
return ofy().load().type(PremiumList.class).parent(getCrossTldKey()).id(name).now();

View file

@ -174,6 +174,12 @@ public final class PremiumListUtils {
ofy().save().entities(newList, newRevision);
return newList;
});
// Invalidate the cache on this premium list so the change will take effect instantly. This only
// clears the cache on the same instance that the update was run on, which will typically be the
// only tools instance.
PremiumList.cachePremiumLists.invalidate(premiumList.getName());
// TODO(b/79888775): Enqueue the oldPremiumList for deletion after at least
// RegistryConfig.getDomainLabelListCacheDuration() has elapsed.
return updated;