Skip to content

Commit

Permalink
add max cache size
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkaW committed Oct 4, 2017
1 parent bb4a10d commit a6214d5
Showing 1 changed file with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
import android.util.LruCache;

import java.io.File;
import java.io.FileDescriptor;
import java.util.HashMap;
import java.util.Map;

import moe.shizuku.fontprovider.font.Font;
Expand All @@ -31,21 +31,32 @@ public class FontProviderService extends Service {

private IFontProviderBinder mBinder;

private static Map<String, MemoryFile> sCache;
private static final LruCache<String, MemoryFile> sCache;
private static final Map<String, Integer> sFileSize;

private static final int MAX_CACHE = 1024 * 1024 * 100;

static {
sCache = new HashMap<>();
sCache = new LruCache<String, MemoryFile>(MAX_CACHE) {

@Override
protected void entryRemoved(boolean evicted, String key, MemoryFile oldValue, MemoryFile newValue) {
if (evicted) {
oldValue.close();
}
}

@Override
protected int sizeOf(String key, MemoryFile value) {
return value.length();
}
};

sFileSize = BuildConfig.BUILT_IN_FONTS_SIZE;
}

public static void closeAll() {
for (Map.Entry<String, MemoryFile> entry: sCache.entrySet()) {
entry.getValue().close();
}

sCache.clear();
sCache.trimToSize(0);
}

@Override
Expand Down

0 comments on commit a6214d5

Please sign in to comment.