lookup(String userId) {
return memoryCache.get(userId);
}
- /**
- * Migrate legacy user profiles if found.
- *
- * Note: this will overwrite a newer `UserProfile` cache in the unlikely event that a legacy cache and new cache
- * both exist on disk.
- */
- @VisibleForTesting
- void migrateLegacyUserProfiles() {
- JSONObject legacyUserProfilesJson = legacyDiskCache.load();
-
- if (legacyUserProfilesJson == null) {
- logger.info("No legacy user profiles to migrate.");
- return;
- }
-
- try {
- Iterator userIdIterator = legacyUserProfilesJson.keys();
- while (userIdIterator.hasNext()) {
- String userId = userIdIterator.next();
- JSONObject legacyUserProfileJson = legacyUserProfilesJson.getJSONObject(userId);
-
- Map> experimentBucketMap = new ConcurrentHashMap<>();
- Iterator experimentIdIterator = legacyUserProfileJson.keys();
- while (experimentIdIterator.hasNext()) {
- String experimentId = experimentIdIterator.next();
- String variationId = legacyUserProfileJson.getString(experimentId);
- Map decisionMap = new ConcurrentHashMap<>();
- decisionMap.put(variationIdKey, variationId);
- experimentBucketMap.put(experimentId, decisionMap);
- }
-
- Map userProfileMap = new ConcurrentHashMap<>();
- userProfileMap.put(userIdKey, userId);
- userProfileMap.put(experimentBucketMapKey, experimentBucketMap);
- save(userProfileMap);
- }
- } catch (JSONException e) {
- logger.warn("Unable to deserialize legacy user profiles. Will delete legacy user profile cache file.", e);
- } finally {
- legacyDiskCache.delete();
- }
- }
-
/**
* Remove a user profile.
*
@@ -218,9 +173,6 @@ void save(Map userProfileMap) {
* Load the cache from disk to memory.
*/
void start() {
- // Migrate legacy user profiles if found.
- migrateLegacyUserProfiles();
-
try {
JSONObject userProfilesJson = diskCache.load();
Map> userProfilesMap = UserProfileCacheUtils.convertJSONObjectToMap
@@ -295,71 +247,4 @@ protected Boolean doInBackground(Void[] params) {
task.executeOnExecutor(executor);
}
}
-
- /**
- * Stores a map of userIds to a map of expIds to variationIds in a file.
- *
- * @deprecated This class is only used to migrate legacy user profiles to the new {@link UserProfileCache}.
- */
- static class LegacyDiskCache {
-
- private static final String FILE_NAME = "optly-user-profile-%s.json";
- @NonNull private final Cache cache;
- @NonNull private final Executor executor;
- @NonNull private final Logger logger;
- @NonNull private final String projectId;
-
- LegacyDiskCache(@NonNull Cache cache, @NonNull Executor executor, @NonNull Logger logger,
- @NonNull String projectId) {
- this.cache = cache;
- this.executor = executor;
- this.logger = logger;
- this.projectId = projectId;
- }
-
- @VisibleForTesting
- String getFileName() {
- return String.format(FILE_NAME, projectId);
- }
-
- /**
- * Load legacy user profiles from disk if found.
- */
- @Nullable
- JSONObject load() {
- String cacheString = cache.load(getFileName());
-
- if (cacheString == null) {
- logger.info("Legacy user profile cache not found.");
- return null;
- }
-
- try {
- return new JSONObject(cacheString);
- } catch (JSONException e) {
- logger.warn("Unable to parse legacy user profiles. Will delete legacy user profile cache file.", e);
- delete();
- return null;
- }
- }
-
- /**
- * Delete the legacy user profile cache from disk in a background thread.
- */
- void delete() {
- AsyncTask task = new AsyncTask() {
- @Override
- protected Boolean doInBackground(Void[] params) {
- Boolean deleted = cache.delete(getFileName());
- if (deleted) {
- logger.info("Deleted legacy user profile from disk.");
- } else {
- logger.warn("Unable to delete legacy user profile from disk.");
- }
- return deleted;
- }
- };
- task.executeOnExecutor(executor);
- }
- }
}