Java: WeakHashMap must be reported

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.

+3
2

WeakReferences SoftReferences.

, WeakReference, . , , .

WeakReference SoftReference, : , .

java SoftHashMap. - MapMaker. , . "":

  • :

    , . , .

  • expireAfterWrite expireAfterAccess.

, . , Track TrackData, . - , Track - , , .

Tracks TrackData, Track . , :

  • Map<Integer, TrackData> ( MapMaker);
  • Track --> TrackData Track --> Id (int). Id --> TrackData.
+2

TrackData Track. , TrackData Track.

 public class Track [

   @Override
   public int hashcode() {
     ... make hashcode that will be the same for
     ... tracks sharing the same track data.
   }

   @Override
   public boolean equals() {
     ... ensure that if A.hashcode == B.hashcode then A.equals(B)
   }

 }

 public class TrackDataManager {

   private WeakHashMap<Track,TrackData> cache = new WeakHashMap<Track,TrackData>();

   public TrackData getTrackData(Track track) {

     // Track.hashcode()/equals() ensures two tracks that
     // share track data will get the same object back
     TrackData data = cache.get(track);

     if (data == null) {

       data = constructDataFromTrackFile(track);

       cache.put(track, data);

     }

     return data;

   }

   private TrackData constructDataFromTrackFile(Track track) {
     ... read data from file and create that object.
   }

 }

TrackData , , :

 public class TrackData {

   @Override
   public int hashcode() {
     ... make hashcode that will be the same for same track data.
   }

   @Override
   public boolean equals() {
     ... ensure that if A.hashcode == B.hashcode then A.equals(B)
   }

 }

 public class TrackDataCache {

   private WeakHashMap<Integer,TrackData> cache = new WeakHashMap<Integer,TrackData>();

   public TrackData getTrackData(Track track) {

     // cache contains shared TrackData instances, we may throw away
     // the Track instance in favour of the shared one.

     Integer key = track.getTrackData().hashcode();

     TrackData data = cache.get(key);

     if (data == null) {

       cache.put(key, track.getTrackData());
       data = track.getTrackData();

     } else {

       // ensure we're using the shared instance, not the local one.
       // deliberate object reference comparison  
       if (data != track.getTrackData()) {
         track.setTrackData(data);
       } 

     }

     return data;

   }

 }

, WeakHashMap , Track, TrackData. , WeakReference Track, , TrackData, , , .

+1

Source: https://habr.com/ru/post/1778507/


All Articles