2
votes

I would like to use Guava as cache but I can't seem to find Guava has the capability of allowing me to load multiple items and get multiple items.

I see CacheLoader has the following:

    @Override
    public Value load(String key) {
        return getKey();
    }

And what I need to load is:

    @Override
    public List<Value> load(List<String> keys) {
        return getKeys();
    }

I would also expect to get one or a list of items from the cache, but I am happy even if I had to wrap that one item into a list just to get it.

I'm new to Guava and I'm not sure if Guava has such functionality?

3
That's the CacheLoader class right?DPM
Correct, CacheLoader. Is there any other cache provided by guava that I'm unaware of?user3019766
Not as far as I can see. I'm not familiar with this class, but I wanted to take a look. As a general rule I'd put as much info in a question as necessary.DPM
Appreciated for trying to help.user3019766

3 Answers

4
votes

You can use CacheLoader.loadAll() to load multiple items, and LoadingCache.getAll() to get them.

For example:

new CacheLoader<String, Value>() {
    @Override
    public Value load(String key) {
        return getKey();
    }

    @Override
    public Map<String, Value> load(Iterable<? extends String> keys) {
        return getKeys();
    }
}
//...
List<String> keys = Arrays.asList("key1", "key2", "key3");
ImmutableMap<String, Value> values = cache.getAll(keys);
0
votes

You can create a LoadingCache(just for e.g.) as:

private final LoadingCache<String, Object> cache;

where String could be your key's datatype and Object could be your value's datatype.

You can then initialise it using CacheBuilder as:

cache = CacheBuilder.newBuilder().
                initialCapacity(10).
                maximumSize(50).
                recordStats().
                build(new CacheLoader<String, Object>() {
                    @Override
                    public Object load(String s) throws Exception {
                        return null;
                    }
                });

and further more implement methods to get a value from the cache based on the key and put a value into the cache for a key value pair in somewhat this format:

public Object get(String key) {
    try {
        return cache.getIfPresent(key);
    } catch (Exception e) {
        System.out.println(e.getMessage());  
        return null;
    }
}

public boolean put(String key, Object object) {
    cache.put(key, object);
    return true;
}
0
votes
Public class Cache { 
  private Cache<Key, Value> cache;
  prviate DataDAO cataDao;

  public Cache(DataDAO dataDao) {
     _dataDao = DataDAO;
     cache = CacheBuilder.newBuilder().build();
  }

  public Value getValue(Key key) {
     Value value;
     if (cache.getIfPresent(key) == null) {
         value = dataDao.getById(key);
         cache.put(key, value);
         return value;
     }else{
         return cache.getIfPresent(key);
     } 
  }

  Public List<Value> getValues(List<Key> keys) {
     List<Value> values = new ArrayList<>();
     List<Key> notInCacheKeys = new ArrayList<>();
     for (Key key: keys) {
        if (cache.getIfPresent(key)) == null) {
           notInCacheKeys.add(key);
        }
     }
     List<Value> newlyRetrievedValues = _dataDao.getByIds(notInCacheKeys);
     //Store Keys and Values in order
     //Return value and list of values from cache
  }
}   

I have decided to abandon CacheLoader and LoadingCache and just work with cache directly.