|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.imagepipeline.cache |
| 9 | + |
| 10 | +/** |
| 11 | + * Enables producers to find larger cached variants of the same image content to avoid redundant |
| 12 | + * network fetches. Implementations track known cache keys grouped by content identity (groupKey) |
| 13 | + * and find the smallest entry that is still larger than the requested dimensions. |
| 14 | + * |
| 15 | + * All gating logic (e.g. which image types or caller modules are eligible) is the responsibility of |
| 16 | + * the implementation — producers simply call [findLargerMemoryKey]/[findLargerDiskKey] and act on |
| 17 | + * the result. |
| 18 | + */ |
| 19 | +interface SimilarImageLookup { |
| 20 | + |
| 21 | + /** |
| 22 | + * Find a larger variant of the same image in the memory cache key tracker. |
| 23 | + * |
| 24 | + * Producer contract: the consuming producer must NOT call this for cache-only requests (e.g. |
| 25 | + * [ImageRequest.RequestLevel.BITMAP_MEMORY_CACHE]). Cache-only callers expect exact-match |
| 26 | + * semantics — returning a resized approximate would violate that contract. The returned result is |
| 27 | + * a raw cache key; the producer is responsible for any resize or re-cache of the larger bitmap. |
| 28 | + * |
| 29 | + * @param cacheKeyString the exact cache key string of the requested image |
| 30 | + * @param groupKey content identity key grouping different resolutions of the same image. This is |
| 31 | + * typically the base cache key string without dimensions (e.g., normalized URI or |
| 32 | + * server-provided content ID). All cached variants of the same media share the same groupKey. |
| 33 | + * Pass null if grouping is not available — implementations should return null in that case. |
| 34 | + * @param width requested image width |
| 35 | + * @param height requested image height |
| 36 | + * @param callerContext app-specific caller context (e.g., IG passes IgCallerContext). |
| 37 | + * Implementations downcast to their app's type and return null for unrecognized types. |
| 38 | + * @return the cache key of a larger variant, or null if none found or lookup is disabled |
| 39 | + */ |
| 40 | + fun findLargerMemoryCacheKey( |
| 41 | + cacheKeyString: String, |
| 42 | + groupKey: String?, |
| 43 | + width: Int, |
| 44 | + height: Int, |
| 45 | + callerContext: Any?, |
| 46 | + ): SimilarImageResult? |
| 47 | + |
| 48 | + /** |
| 49 | + * Find a larger variant of the same image in the disk cache key tracker. |
| 50 | + * |
| 51 | + * Producer contract: the consuming producer must NOT call this when all attached requests are |
| 52 | + * cache-only (no network-permitted requests). Same cache-only semantics as |
| 53 | + * [findLargerMemoryCacheKey]. |
| 54 | + * |
| 55 | + * @param diskCacheKey the disk cache key string (expected format: "groupKey_width_height") |
| 56 | + * @param callerContext app-specific caller context (e.g., IG passes IgCallerContext). |
| 57 | + * Implementations downcast to their app's type and return null for unrecognized types. |
| 58 | + * @return the cache key of a larger variant, or null if none found or lookup is disabled |
| 59 | + */ |
| 60 | + fun findLargerDiskCacheKey(diskCacheKey: String, callerContext: Any?): SimilarImageResult? |
| 61 | + |
| 62 | + /** |
| 63 | + * Track a memory cache key for future similarity lookups. Called on cache insertion. |
| 64 | + * |
| 65 | + * Producers must only call this after a successful decode — not for intermediate or failed |
| 66 | + * results. |
| 67 | + * |
| 68 | + * @param cacheKeyString the exact cache key string of the decoded image |
| 69 | + * @param groupKey content identity key (same as [findLargerMemoryCacheKey]). Pass null if not |
| 70 | + * available. |
| 71 | + * @param width decoded image width |
| 72 | + * @param height decoded image height |
| 73 | + */ |
| 74 | + fun trackMemoryCacheKey(cacheKeyString: String, groupKey: String?, width: Int, height: Int) |
| 75 | + |
| 76 | + /** |
| 77 | + * Track a disk cache key for future similarity lookups. Called on disk cache write. |
| 78 | + * |
| 79 | + * @param diskCacheKey the disk cache key string |
| 80 | + * @param isFullImage true if the image is fully decoded (all scans complete). Partial progressive |
| 81 | + * scan data must NOT be tracked, as it would pollute the similarity index with incomplete |
| 82 | + * images that cannot serve as valid "larger" sources. |
| 83 | + */ |
| 84 | + fun trackDiskCacheKey(diskCacheKey: String, isFullImage: Boolean = true) |
| 85 | + |
| 86 | + /** Remove a tracked memory key. Called on cache eviction. */ |
| 87 | + fun removeMemoryCacheKey(cacheKeyString: String, groupKey: String?, width: Int, height: Int) |
| 88 | +} |
| 89 | + |
| 90 | +/** Result of a similar-image lookup — the cache key of a larger cached variant. */ |
| 91 | +data class SimilarImageResult( |
| 92 | + val cacheKeyString: String, |
| 93 | + val groupKey: String?, |
| 94 | + val width: Int, |
| 95 | + val height: Int, |
| 96 | +) |
0 commit comments