diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java b/lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java index 07454f2f8c..bfe55c38b6 100644 --- a/lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java +++ b/lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java @@ -151,6 +151,10 @@ public static LottieResult fromUrlSync(Context context, Strin */ @WorkerThread public static LottieResult fromUrlSync(Context context, String url, @Nullable String cacheKey) { + final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey); + if (cachedComposition != null) { + return new LottieResult<>(cachedComposition); + } LottieResult result = L.networkFetcher(context).fetchSync(context, url, cacheKey); if (cacheKey != null && result.getValue() != null) { LottieCompositionCache.getInstance().put(cacheKey, result.getValue()); @@ -213,6 +217,10 @@ public static LottieResult fromAssetSync(Context context, Str */ @WorkerThread public static LottieResult fromAssetSync(Context context, String fileName, @Nullable String cacheKey) { + final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey); + if (cachedComposition != null) { + return new LottieResult<>(cachedComposition); + } try { if (fileName.endsWith(".zip") || fileName.endsWith(".lottie")) { return fromZipStreamSync(context, new ZipInputStream(context.getAssets().open(fileName)), cacheKey); @@ -282,6 +290,10 @@ public static LottieResult fromRawResSync(Context context, @R */ @WorkerThread public static LottieResult fromRawResSync(Context context, @RawRes int rawRes, @Nullable String cacheKey) { + final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey); + if (cachedComposition != null) { + return new LottieResult<>(cachedComposition); + } try { BufferedSource source = Okio.buffer(source(context.getResources().openRawResource(rawRes))); if (isZipCompressed(source)) { @@ -376,8 +388,6 @@ public static LottieTask fromJsonString(final String json, @N */ @WorkerThread public static LottieResult fromJsonStringSync(String json, @Nullable String cacheKey) { - - ByteArrayInputStream stream = new ByteArrayInputStream(json.getBytes()); return fromJsonReaderSync(JsonReader.of(buffer(source(stream))), cacheKey); } @@ -399,6 +409,10 @@ public static LottieResult fromJsonReaderSync(com.airbnb.lott private static LottieResult fromJsonReaderSyncInternal( com.airbnb.lottie.parser.moshi.JsonReader reader, @Nullable String cacheKey, boolean close) { try { + final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey); + if (cachedComposition != null) { + return new LottieResult<>(cachedComposition); + } LottieComposition composition = LottieCompositionMoshiParser.parse(reader); if (cacheKey != null) { LottieCompositionCache.getInstance().put(cacheKey, composition); @@ -516,6 +530,10 @@ private static LottieResult fromZipStreamSyncInternal(Context Map fonts = new HashMap<>(); try { + final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey); + if (cachedComposition != null) { + return new LottieResult<>(cachedComposition); + } ZipEntry entry = inputStream.getNextEntry(); while (entry != null) { final String entryName = entry.getName(); diff --git a/lottie/src/test/java/com/airbnb/lottie/LottieCompositionFactoryTest.java b/lottie/src/test/java/com/airbnb/lottie/LottieCompositionFactoryTest.java index de7463560b..38140999a3 100644 --- a/lottie/src/test/java/com/airbnb/lottie/LottieCompositionFactoryTest.java +++ b/lottie/src/test/java/com/airbnb/lottie/LottieCompositionFactoryTest.java @@ -18,6 +18,7 @@ import static junit.framework.Assert.assertNull; import static okio.Okio.buffer; import static okio.Okio.source; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertSame; @SuppressWarnings("ReferenceEquality") @@ -45,6 +46,20 @@ public void testLoadJsonString() { assertNotNull(result.getValue()); } + @Test + public void testLoadJsonStringHitsCache() { + LottieResult result1 = LottieCompositionFactory.fromJsonStringSync(JSON, "json"); + LottieResult result2 = LottieCompositionFactory.fromJsonStringSync(JSON, "json"); + assertEquals(result1, result2); + } + + @Test + public void testLoadDifferentJsonStringsDoesntHitsCache() { + LottieResult result1 = LottieCompositionFactory.fromJsonStringSync(JSON, "jso1"); + LottieResult result2 = LottieCompositionFactory.fromJsonStringSync(JSON, "json2"); + assertNotEquals(result1, result2); + } + @Test public void testLoadInvalidJsonString() { LottieResult result = LottieCompositionFactory.fromJsonStringSync(NOT_JSON, "not_json");