How to pass parameters to load method in addition to KEY

2,506 views
Skip to first unread message

saiye...@gmail.com

unread,
Aug 29, 2013, 7:37:40 PM8/29/13
to guava-...@googlegroups.com

I have written a program to cache objects using Google Guava. My problem is how to pass additional parameters to Guava Load method. Here is the code. Below you see in this line - I have made fileId and pageno as key - cache.get(fileID+pageNo);. Now when cache.get is called and when that key is not in the cache - guava will call the load method of class PreviewCacheLoader which I have given below as well.

public class PreviewCache {
    static final LoadingCache<String, CoreObject> cache = CacheBuilder.newBuilder()
          .maximumSize(1000)
          .expireAfterWrite(5, TimeUnit.MINUTES) 
          .build(new PreviewCacheLoader());

   public CoreObject getPreview(String strTempPath, int pageNo, int requiredHeight, String fileID, String strFileExt,  String ssoId) throws IOException 
    {
        CoreObject coreObject = null;
        try {
            coreObject = cache.get(fileID+pageNo, HOW TO PASS pageNO and requiredHeight because I want to keep key as ONLY fileID+pageNo );
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return coreObject;
    }
}

How to pass parameters from above which are int and String to below Load method in addition to key parameter

public class PreviewCacheLoader extends CacheLoader<String, CoreObject> {

@Override
public CoreObject load(String fileIDpageNo, HOW TO GET pageNO and requiredHeight) throws Exception {

    CoreObject coreObject = new CoreObject();
    // MAKE USE OF PARAMETERS pageNO and requiredHeight
    // Populate coreObject here 
    return coreObject;


}

Louis Wasserman

unread,
Aug 29, 2013, 9:01:30 PM8/29/13
to saiye...@gmail.com, guava-discuss
You cannot do it like that, but one possible alternative is for you to use Cache.get(K, Callable<V>), and to construct the Callable with all the extra information that would be necessary.
--
--
guava-...@googlegroups.com
Project site: http://guava-libraries.googlecode.com
This group: http://groups.google.com/group/guava-discuss
 
This list is for general discussion.
To report an issue: http://code.google.com/p/guava-libraries/issues/entry
To get help: http://stackoverflow.com/questions/ask (use the tag "guava")
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/guava-discuss/0c282341-42d8-44cc-ae84-fe4b86177be6%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

saiye...@gmail.com

unread,
Aug 29, 2013, 10:33:26 PM8/29/13
to guava-...@googlegroups.com, saiye...@gmail.com
Thank You Louis - So in that case do I need these two things. 1.  .build(new PreviewCacheLoader()); 2. And entire PreviewCacheLoader class. What I understood is I need to move the logic of load method in  PreviewCacheLoader class to the callable call method. Is that correct ? 

saiye...@gmail.com

unread,
Aug 29, 2013, 10:53:46 PM8/29/13
to guava-...@googlegroups.com, saiye...@gmail.com
So your suggestion worked but I still don't understand. Really appreciate if you clear my concepts. So do I still need . 1.  line ----.build(new PreviewCacheLoader()); 2. And entire PreviewCacheLoader class. We don't need to call PreviewCacheLoader now as we don't need load method because call method is doing it. Please clear. 
Reply all
Reply to author
Forward
0 new messages