java - How best to get List nodes for a cache implementation -
okay first preface "i very new java" (i.e., few days in), programmer trade.
i have come across situation want load data. however, cache data prevent extraneous calls api (or, whatever data source may be). after thinking bit, have come cache scheme seems pretty reasonable me: idea datacache class has 2 collections: hash table key type "string" , value type "cachedata". cachedata has 2 data members - actual result of api call in string form, , ref (listiterator?) node of linked list. brings 2nd collection - linked list of keys. idea when request comes in data, see if it's in hash. if not, fetch api, add resulting key front of linked list, , store data object in hash containing result, along ref first node of linked list (the 1 added). if data found in hash, break node out of linked list, put front, , return data cachedata. benefit, every operation guaranteed execute in o(1), if i'm understanding correctly.
can store integer hash value of 'request' in linked list instead of string (request) whole? if so, how can access result in hashmap given integer? (none of methods seem take 'int' param). also...is approach situation sound? or there perhaps in java make easier?
okay first preface "i very new java" (i.e., few days in), programmer trade. i have come across situation want load data. however, cache data prevent extraneous calls api (or, whatever data source may be). after thinking bit, have come cache scheme seems pretty reasonable me: idea datacache class has 2 collections: hash table key type "string" , value type "cachedata". cachedata has 2 data members - actual result of api call in string form, , ref (listiterator?) node of linked list. brings 2nd collection - linked list of keys. idea when request comes in data, see if it's in hash. if not, fetch api, add resulting key front of linked list, , store data object in hash containing result, along ref first node of linked list (the 1 added). if data found in hash, break node out of linked list, put front, , return data cachedata. benefit, every operation guaranteed execute in o(1), if i'm understanding correctly. can store integer hash value of 'request' in linked list instead of string (request) whole? if so, how can access result in hashmap given integer? (none of methods seem take 'int' param). also...is approach situation sound? or there perhaps in java make easier? | |||
hashmap
? if want more sophisticated caching, suggest @ guava – jon skeet jul 7 '12 @ 7:31linkedhashmap
, "special constructor" want you. – jon skeet jul 7 '12 @ 18:29