Monday, 30 March 2015

Guava cache example

Please fine below the Google Guava example below:

You can also watch my tutorial at:
https://youtu.be/FCE_AlYjgJ8
https://youtu.be/2AN4H6yF0-Y

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;

public class GuavaTest {

    private LoadingCache<String, String> cache;
   
    private static GuavaTest gt = new GuavaTest();
    public static GuavaTest getInstance(){ return gt; }
    private GuavaTest() {
        cache = CacheBuilder.newBuilder()
                .refreshAfterWrite(2, TimeUnit.SECONDS)
                .build(new CacheLoader<String, String>(

                ) {
                    @Override
                    public String load(String arg0) throws Exception {
                        // TODO Auto-generated method stub
                        return addcache(arg0);
                    }
                   
                    @Override
                    public Map<String, String> loadAll(Iterable<? extends String> keys) throws Exception {
                        System.out.println("inside load all");
                        return addcacheAll(keys);
                    }

                });
    }

    private String addcache(String arg0) {
        System.out.println("adding cache");
        return arg0.toUpperCase();
    }
   
    private Map<String, String> addcacheAll(Iterable<? extends String> keys) {
        Map<String, String> map = new HashMap<String, String>();
        for(String s: keys){
            map.put(s, s.toUpperCase());
        }
        return map;
    }
   
    public String getEntry(String args) throws ExecutionException{
        System.out.println(cache.size());
        return cache.get(args);
    }
   
    public ImmutableMap<String, String> getEntryAll(String args) throws ExecutionException{
        List<String> list = new ArrayList<String>();
        list.add(args);
        return cache.getAll(list);
    }
   
    public static void main(String[] args) {
        GuavaTest gt = GuavaTest.getInstance();
        try {
            System.out.println(gt.getEntry("Suvendu"));
            System.out.println(gt.getEntry("Suvendu"));
            Thread.sleep(2100);
            System.out.println(gt.getEntry("Suvendu"));
            System.out.println(gt.getEntry("Suvendu"));
           
            System.out.println(gt.getEntryAll("Suvendu1"));
            System.out.println(gt.getEntry("Suvendu1"));
           
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}