Caching in Java with Singleton Pattern

I want to send some file(s) to cache and read them from cache in Java. I’ve been searching for this on the internet but there are many advanced stuff for caching. I’ve downloaded ehcache but couldn’t find a way to work things out. Is there a library for just caching and reading files from cache in java? I’m kinda new in Java, therfore I don’t understand advanced stuff easily.

Thank you for your help.

Answer

Google Guava provide simple way to implement cache in your application.

LoadingCache<Key, Graph> graphs = CacheBuilder.newBuilder()
       .maximumSize(1000)
       .expireAfterWrite(10, TimeUnit.MINUTES)
       .removalListener(MY_LISTENER)
       .build(
           new CacheLoader<Key, Graph>() {
             public Graph load(Key key) throws AnyException {
               return createExpensiveGraph(key);
             }
           });

Hope this helps

Leave a Reply

Your email address will not be published. Required fields are marked *