Skip to main content

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?

java - how best list nodes cache implementation - stack overflow

learn, share, build

each month, on 50 million developers come stack overflow learn, share knowledge, , build careers.

join world’s largest developer community.

sign up

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?

share|improve question
    
it's not clear request/response data is, or why linked list required - why not have hashmap? if want more sophisticated caching, suggest @ guava – jon skeet jul 7 '12 @ 7:31
    
linked list there have 'least used' replacement policy hashing algorithm. req/response can strings, 'hash["currencies"] = "[yen, dollar (us), euro]". guava looks pretty - give shot, still interested if want doable pre-defined java collections – rollie jul 7 '12 @ 18:25
1  
i don't think you'd want map then linked list value. @ linkedhashmap , "special constructor" want you. – jon skeet jul 7 '12 @ 18:29
    
that looks wanted implement actually, although sub-classing hashmap<> , adding functionality in derived class, makes more sense version (composition) methods (size, etc) don't need re-implemented. – rollie jul 7 '12 @ 18:51
    
you may try ehcache supports lru, timetoidleseconds, timetoliveseconds, local/distributed cache. – ragnor jul 8 '12 @ 10:44

your answer

 
discard

posting answer, agree privacy policy , terms of service.

browse other questions tagged or ask own question.


Comments