AudienceManager { private void refreshCache(Map<String, Set<String>> tags, @Nullable TagGroupResponse cachedResponse) throws Exception { Map<String, Set<String>> requestTags; if (requestTagsCallback != null) { requestTags = TagGroupUtils.union(tags, requestTagsCallback.getTags()); } else { requestTags = tags; } if (cachedResponse != null && !requestTags.equals(getCachedRequestTags())) { cachedResponse = null; } TagGroupResponse response = client.lookupTagGroups(airshipChannel.getId(), requestTags, cachedResponse); if (response == null) { Logger.error("Failed to refresh the cache."); return; } if (response.status != 200) { Logger.error("Failed to refresh the cache. Status: %s", response); return; } Logger.verbose("Refreshed tag group with response: %s", response); setCachedResponse(response, requestTags); } AudienceManager(@NonNull AirshipRuntimeConfig runtimeConfig,
                           @NonNull AirshipChannel airshipChannel,
                           @NonNull NamedUser namedUser,
                           @NonNull PreferenceDataStore dataStore); @VisibleForTesting  AudienceManager(@NonNull TagGroupLookupApiClient client, @NonNull AirshipChannel airshipChannel,
                    @NonNull NamedUser namedUser, @NonNull AudienceHistorian historian,
                    @NonNull PreferenceDataStore dataStore, @NonNull Clock clock); void setEnabled(boolean enabled); boolean isEnabled(); void setCacheMaxAgeTime(@IntRange(from = 0) long duration, @NonNull TimeUnit unit); long getCacheMaxAgeTimeMilliseconds(); void setCacheStaleReadTime(@IntRange(from = MIN_CACHE_MAX_AGE_TIME_MS) long duration, @NonNull TimeUnit unit); long getCacheStaleReadTimeMilliseconds(); void setRequestTagsCallback(@Nullable RequestTagsCallback callback); long getPreferLocalTagDataTime(); void setPreferLocalTagDataTime(@IntRange(from = 0) long duration, @NonNull TimeUnit unit); @NonNull @WorkerThread synchronized TagGroupResult getTags(@NonNull Map<String, Set<String>> tags); @NonNull List<TagGroupsMutation> getTagOverrides(); @NonNull List<AttributeMutation> getAttributeOverrides(); static final long MIN_CACHE_MAX_AGE_TIME_MS; static final long DEFAULT_CACHE_MAX_AGE_TIME_MS; static final long DEFAULT_CACHE_STALE_READ_TIME_MS; static final long DEFAULT_PREFER_LOCAL_DATA_TIME_MS; }
@Test public void testRefreshCache() { manager.setCacheMaxAgeTime(AudienceManager.MIN_CACHE_MAX_AGE_TIME_MS, TimeUnit.MILLISECONDS); TagGroupResponse response = new TagGroupResponse(200, clientResponseTags, "lastModifiedTime"); when(mockClient.lookupTagGroups(channelId, getExpectedClientRequestTags(), null)) .thenReturn(response); TagGroupResult result = manager.getTags(requestTags); assertTrue(result.success); verify(mockClient, times(1)).lookupTagGroups(channelId, getExpectedClientRequestTags(), null); clock.currentTimeMillis += AudienceManager.MIN_CACHE_MAX_AGE_TIME_MS + 1; when(mockClient.lookupTagGroups(channelId, getExpectedClientRequestTags(), response)) .thenReturn(response); result = manager.getTags(requestTags); assertTrue(result.success); verify(mockClient, times(1)).lookupTagGroups(channelId, getExpectedClientRequestTags(), response); }