I assume that I am another person trying to make some kind of cache with WeakHashMap. And I need some help.
I have a bunch of objects TrackDatacontaining information about audio tracks. Then there are objects Trackthat reference TrackDatainside. Multiple tracks may point to the same TrackData. Then I have a class TrackDataCachethat looks like this:
public class TrackDataCache {
private static TrackDataCache instance = new TrackDataCache();
public static TrackDataCache getInstance() {
return instance;
}
private WeakHashMap<TrackData, WeakReference<TrackData>> cache = new WeakHashMap<TrackData, WeakReference<TrackData>>();
public void cache(Track track) {
TrackData key = track.getTrackData();
WeakReference<TrackData> trackData = cache.get(key);
if (trackData == null) {
cache.put(key, new WeakReference<TrackData>(key));
} else {
track.setTrackData(trackData.get());
}
}
}
Therefore, when I download a track, I call TrackDataCache.cache(), and if its track data has not been downloaded before, it is cached or replaced with a cached copy otherwise ( TrackDataoverrides the equals () method to check the location and index of the subnets). I want to use weak links, so I don’t have to care when I delete tracks.
, WeakHashMap, , ? . WeakHashMap getEntry() public, , : (
PS. , apache, google - , 2Mb.