Skip to content

Commit

Permalink
fix(android): change to using global image cache
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 committed Jul 12, 2024
1 parent a35a34d commit 9cead15
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
public class ImageDataPool extends BasePool<ImageDataKey, ImageRecycleObject> {

private static final int DEFAULT_IMAGE_POOL_SIZE = 16;
private final Object mLock = new Object();
private LruCache<ImageDataKey, ImageRecycleObject> mPools;

public ImageDataPool() {
Expand Down Expand Up @@ -50,15 +51,17 @@ protected void entryRemoved(boolean evicted, @NonNull ImageDataKey key,
@Override
@Nullable
public ImageRecycleObject acquire(@NonNull ImageDataKey key) {
ImageRecycleObject data = mPools.get(key);
if (data != null && data.isScraped()) {
// Bitmap may have been recycled, must be removed from the cache and not
// returned to the component.
mPools.remove(key);
data.evicted();
return null;
synchronized (mLock) {
ImageRecycleObject data = mPools.get(key);
if (data != null && data.isScraped()) {
// Bitmap may have been recycled, must be removed from the cache and not
// returned to the component.
mPools.remove(key);
data.evicted();
return null;
}
return data;
}
return data;
}

@Override
Expand All @@ -71,18 +74,24 @@ public void release(@NonNull ImageRecycleObject data) {

@Override
public void release(@NonNull ImageDataKey key, @NonNull ImageRecycleObject data) {
mPools.put(key, data);
data.cached();
synchronized (mLock) {
mPools.put(key, data);
data.cached();
}
}

@Override
public void clear() {
mPools.evictAll();
synchronized (mLock) {
mPools.evictAll();
}
}

@Override
public void remove(@NonNull ImageDataKey key) {
mPools.remove(key);
synchronized (mLock) {
mPools.remove(key);
}
}

private void onEntryEvicted(@NonNull ImageRecycleObject data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class RecycleObjectPool extends BasePool<String, RecycleObject> {
private static final String TAG = "RecycleObjectPool";
private final Object mLock = new Object();
private final Map<String, SimplePool<RecycleObject>> mPools = new HashMap<>();
private int mPoolSize = 24;
private int mPoolSize = 12;

public RecycleObjectPool() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class ImageLoader implements ImageLoaderAdapter {
@NonNull
private final HashMap<ImageDataKey, ArrayList<ImageRequestListener>> mListenersMap = new HashMap<>();
@NonNull
private final Pool<ImageDataKey, ImageRecycleObject> mImagePool = new ImageDataPool();
private final static Pool<ImageDataKey, ImageRecycleObject> mImagePool = new ImageDataPool();

public ImageLoader(@NonNull VfsManager vfsManager,
@Nullable ImageDecoderAdapter imageDecoderAdapter) {
Expand Down

0 comments on commit 9cead15

Please sign in to comment.