c# - Best data structure for caching objects with a composite unique id -


i have slow function makes expensive trip server retrieve recordhdr objects. these objects sorted rid first , aid. returned in batches of 5.

    | rid | aid |     -------------->     | 1   | 1   | >     | 1   | 3   | >       | 1   | 5   | > batch of 5 returned     | 1   | 6   | >       | 2   | 2   | >     -------------->     | 2   | 3   |     | 2   | 4   |     | 3   | 1   |     | 3   | 2   |     | 3   | 5   |     | 3   | 6   |     | 4   | 1   |     | 4   | 2   |     | 4   | 5   |     | 4   | 6   | 

after retrieve objects, have wrap them in class called wrappedrecordhdr. i'm wondering best data structure can use maintain cache of wrappedrecordhdr objects such if i'm asked object rid , aid, return particular object it. if i'm asked rid, should return objects have rid.

so far have created 2 structures each scenario (this may not best way, it's i'm using now):

    // key: (rid, aid)     private cachemap<int, int, wrappedrecordhdr> m_ridaidcache =         new cachemap<int, int, wrappedrecordhdr>();      // key: (rid)     private cachemap<int, wrappedrecordhdr[]> m_ridcache =         new cachemap<int, wrappedrecordhdr[]>(); 

also, i'm wondering if there way can rewrite more efficient. right have number of records need wrap within object. then, need group them in dictionary id if asked rid can return objects have same rid. records have been sorted, i'm hoping groupby doesn't attempt sort them beforehand.

    recordhdr[] records = server.getrecordhdrs(sessid, batch_size) // expensive call server.      // after recordhdr objects retrieved, loop through received objects. each recordhdr object wrappedrecordhdr object has created.     wrappedrecordhdr[] wrappedrecords = new wrappedrecordhdr[records.length];      (int = 0; < wrappedrecords.length; i++)     {         if (records[i] == null || records[i].aid == 0 || records[i].rid == 0) continue; // skip invalid results.          wrappedrecords[i] = new wrappedrecordhdr(accessormanager, records[i], projectid);     }      // group records found in dictionary of rid => array of wrappedrecordhdrs, records same      // rid returned.     objects associated particular rid.     dictionary<int, wrappedrecordhdr[]> dict = wrappedrecords.groupby(obj => obj.rid).todictionary(gdc => gdc.key, gdc => gdc.toarray());      m_ridcache = dict; 

as data structure, think there 2 different questions here:

  1. what structure use;
  2. should there 1 or 2 caches;

it seems me want 1 cache, typed memorycache. key rid, , value dictionary, key aid , value header.

this has following advantages:

  1. the wrappedrecordhdrs stored once;
  2. the memorycache has of caching logic implemented, don't need rewrite that;
  3. when provided rid, know aid of each wrappedrecordhdr (which don't array in initial post);

these things compromises, has disadvantages of course:

  1. cache access (get or set) requires constructing string each time;
  2. rid + aid lookups require indexing twice (as opposed writing fast hashing function takes rid , aid , returns single key cache, require either have 2 caches (one rid only, 1 rid + aid) or store same wrappedrecordhdr twice per aid (once rid + aid , once null + aid));

Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -