Skip to content

Commit 82e7f87

Browse files
authored
Merge pull request #66 from nativescript-community/copilot/fix-image-cache-issues
Fix cache key mismatch and add prefetch options support for Android/iOS
2 parents 30486c0 + 4d2bb78 commit 82e7f87

18 files changed

+712
-353
lines changed

demo-snippets/vue/Pipeline.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ export default {
5555
},
5656
async prefetchMem() {
5757
try {
58-
await getImagePipeline().prefetchToMemoryCache(this.url);
58+
await getImagePipeline().prefetchToMemoryCache(this.url, {
59+
decodeWidth: 300,
60+
decodeHeight: 300,
61+
});
5962
this.addLog('prefetchToMemoryCache success');
6063
this.result = 'prefetchToMemoryCache success';
6164
} catch (err) {

demo-snippets/vue/install.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,19 @@ import CrossFadeTest from './CrossFadeTest.vue';
2929
export function installPlugin() {
3030
installMixins();
3131
initialize({
32-
usePersistentCacheKeyStore: true
32+
usePersistentCacheKeyStore: true,
33+
// memoryCacheScreens: 8
3334
});
3435
Vue.use(ImageModule);
3536
Vue.use(ZoomImageModule);
36-
getImagePipeline()
37-
.isInDiskCache('https://images.pexels.com/photos/842711/pexels-photo-842711.jpeg')
38-
.then((r) => {
39-
console.log('installPlugin test pipeline:', r);
40-
});
37+
setTimeout(() => {
38+
// we need to wait for the diskcache to be set
39+
getImagePipeline()
40+
.isInDiskCache('https://images.pexels.com/photos/842711/pexels-photo-842711.jpeg')
41+
.then((r) => {
42+
console.log('installPlugin test pipeline:', r);
43+
});
44+
}, 1000);
4145
}
4246

4347
export const demos = [

gradle/wrapper/gradle-wrapper.jar

44.6 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.1-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

gradlew

Lines changed: 248 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/image/platforms/android/java/com/nativescript/image/CacheKeyStore.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public static class StoredKeys {
2323
public final Class<?> decodedResourceClass;
2424
public final Options options; // may be null or placeholder for in-memory
2525
public final byte[] optionsKeyBytes; // raw bytes from options.updateDiskCacheKey
26-
public final Key engineKey; // optional - in-memory only
2726

2827
public StoredKeys(
2928
Key sourceKey,
@@ -34,8 +33,7 @@ public StoredKeys(
3433
byte[] transformationKeyBytes,
3534
Class<?> decodedResourceClass,
3635
Options options,
37-
byte[] optionsKeyBytes,
38-
Key engineKey) {
36+
byte[] optionsKeyBytes) {
3937
this.sourceKey = sourceKey;
4038
this.signature = signature;
4139
this.width = width;
@@ -45,7 +43,6 @@ public StoredKeys(
4543
this.decodedResourceClass = decodedResourceClass;
4644
this.options = options;
4745
this.optionsKeyBytes = optionsKeyBytes;
48-
this.engineKey = engineKey;
4946
}
5047
}
5148

packages/image/platforms/android/java/com/nativescript/image/CapturingEngineKeyFactory.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ com.bumptech.glide.load.engine.EngineKey buildKey(
3333
Class<?> resourceClass,
3434
Class<?> transcodeClass,
3535
Options options) {
36-
Log.d("JS", "CapturingEngineKeyFactory buildKey " + model);
3736
com.bumptech.glide.load.engine.EngineKey engineKey = super.buildKey(model, signature, width, height,
3837
transformations, resourceClass, transcodeClass, options);
3938
if (listener != null) {

packages/image/platforms/android/java/com/nativescript/image/CustomDataCacheKey.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@
33
import androidx.annotation.NonNull;
44
import com.bumptech.glide.load.Key;
55
import java.security.MessageDigest;
6-
7-
/** A cache key for original source data + any requested signature. */
6+
import java.util.Objects;
7+
8+
/**
9+
* A cache key for original source data + any requested signature.
10+
* This mimics Glide's internal DataCacheKey structure to enable proper disk cache lookups.
11+
*
12+
* Based on Glide's DataCacheKey which follows this exact order:
13+
* 1. sourceKey.updateDiskCacheKey(messageDigest)
14+
* 2. signature.updateDiskCacheKey(messageDigest)
15+
*/
816
final class CustomDataCacheKey implements Key {
917

1018
private final Key sourceKey;
1119
private final Key signature;
1220

1321
CustomDataCacheKey(Key sourceKey, Key signature) {
14-
this.sourceKey = sourceKey;
15-
this.signature = signature;
22+
this.sourceKey = Objects.requireNonNull(sourceKey, "sourceKey must not be null");
23+
this.signature = Objects.requireNonNull(signature, "signature must not be null");
1624
}
1725

1826
Key getSourceKey() {
@@ -42,6 +50,8 @@ public String toString() {
4250

4351
@Override
4452
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
53+
// CRITICAL: Match Glide's DataCacheKey exact order
54+
// From Glide source: DataCacheKey updates sourceKey first, then signature
4555
sourceKey.updateDiskCacheKey(messageDigest);
4656
signature.updateDiskCacheKey(messageDigest);
4757
}

0 commit comments

Comments
 (0)