Say I have some function:

public Foo slowGetFoo(String id){
     //This process may take a long time

In order improve overall application responsiveness, I'd like to preload the results of this query, for a given list of ids, and store them in some collection, ready for immediate retrieval later.

eg. Let's say a Foo is a person, and we know that if a user is looking at a person, then they're also likely to look at their friends, so we want preload all friends.

public Foo getFoo(String id){

    Foo f = cache.retrieve(id);    //First attempt to retrieve it from the cache
    if (f == null) {
        f = service.slowGetFoo(id);      //if it's not there, then retrieve slowly 

    cache.populate(f.getFriends());   //preload f's friends 

    return f; 

I started writing my own server side cache (see here) but it feels like I'm reinventing the wheel, and I'm wondering if there's already a library that does this.

  • 1
    Do you need it to be distributed (among several servers)? – Nicolas Raoul May 9 '16 at 7:13
  • Also, should it be persistent (survive restarts of the program)? And will ever results get invalid? – Nicolas Raoul May 9 '16 at 7:14
  • @NicolasRaoul - No doesn't need to be distributed or persistant, though ideally with contain an expiry timeout mechanism, or a way to prevent it from hoggging too many resources. Re: invalid results, yes - ideally it should be able to recover/store exceptions, though this isn't essential. – dwjohnston May 9 '16 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.