make cache soft-reachable, to reclaim memory if necessary

This commit is contained in:
Alexey Kudravtsev
2012-08-08 16:25:10 +04:00
parent 99a51a9cf3
commit 12044f6c55
@@ -23,7 +23,10 @@ import gnu.trove.THashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
@@ -31,10 +34,21 @@ import java.util.Map;
*/
class Win32FsCache {
private final IdeaWin32 myKernel = IdeaWin32.getInstance();
private final Map<String, FileInfo> myCache = new THashMap<String, FileInfo>();
private Reference<Map<String, FileInfo>> myCache;
void clearCache() {
myCache.clear();
myCache = null;
}
@NotNull
private Map<String, FileInfo> getMap() {
Reference<Map<String, FileInfo>> cache = myCache;
Map<String, FileInfo> map = cache == null ? null : cache.get();
if (map == null) {
map = new THashMap<String, FileInfo>();
myCache = new SoftReference<Map<String, FileInfo>>(map);
}
return map;
}
@NotNull
@@ -44,16 +58,17 @@ class Win32FsCache {
return ArrayUtil.EMPTY_STRING_ARRAY;
}
ArrayList<String> names = new ArrayList<String>(fileInfo.length);
List<String> names = new ArrayList<String>(fileInfo.length);
Map<String, FileInfo> map = getMap();
for (FileInfo info : fileInfo) {
if (info.name.equals(".")) {
myCache.put(absolutePath, info);
map.put(absolutePath, info);
continue;
}
if (info.name.equals("..")) {
continue;
}
myCache.put(absolutePath + "/" + info.name, info);
map.put(absolutePath + "/" + info.name, info);
names.add(info.name);
}
@@ -63,13 +78,14 @@ class Win32FsCache {
@Nullable
FileInfo getInfo(@NotNull VirtualFile file) {
String path = file.getPath();
FileInfo info = myCache.get(path);
Map<String, FileInfo> map = getMap();
FileInfo info = map.get(path);
if (info == null) {
info = myKernel.getInfo(path.replace('/', '\\'));
if (info == null) {
return null;
}
myCache.put(path, info);
map.put(path, info);
}
return info;
}