Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1056169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:40:37+00:00 2026-05-16T17:40:37+00:00

let’s say we have a CountryList object in our application that should return the

  • 0

let’s say we have a CountryList object in our application that should return the list of countries. The loading of countries is a heavy operation, so the list should be cached.

Additional requirements:

  • CountryList should be thread-safe
  • CountryList should load lazy (only on demand)
  • CountryList should support the invalidation of the cache
  • CountryList should be optimized considering that the cache will be invalidated very rarely

I came up with the following solution:

public class CountryList {
    private static final Object ONE = new Integer(1);

    // MapMaker is from Google Collections Library    
    private Map<Object, List<String>> cache = new MapMaker()
        .initialCapacity(1)
        .makeComputingMap(
            new Function<Object, List<String>>() {
                @Override
                public List<String> apply(Object from) {
                    return loadCountryList();
                }
            });

    private List<String> loadCountryList() {
        // HEAVY OPERATION TO LOAD DATA
    }

    public List<String> list() {
        return cache.get(ONE);
    }

    public void invalidateCache() {
        cache.remove(ONE);
    }
}

What do you think about it? Do you see something bad about it? Is there other way to do it? How can i make it better? Should i look for totally another solution in this cases?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:40:38+00:00Added an answer on May 16, 2026 at 5:40 pm

    Thanks you all guys, especially to user “gid” who gave the idea.

    My target was to optimize the performance for the get() operation considering the invalidate() operation will be called very rare.

    I wrote a testing class that starts 16 threads, each calling get()-Operation one million times. With this class I profiled some implementation on my 2-core maschine.

    Testing results

    Implementation              Time
    no synchronisation          0,6 sec
    normal synchronisation      7,5 sec
    with MapMaker               26,3 sec
    with Suppliers.memoize      8,2 sec
    with optimized memoize      1,5 sec
    

    1) “No synchronisation” is not thread-safe, but gives us the best performance that we can compare to.

    @Override
    public List<String> list() {
        if (cache == null) {
            cache = loadCountryList();
        }
        return cache;
    }
    
    @Override
    public void invalidateCache() {
        cache = null;
    }
    

    2) “Normal synchronisation” – pretty good performace, standard no-brainer implementation

    @Override
    public synchronized List<String> list() {
        if (cache == null) {
            cache = loadCountryList();
        }
        return cache;
    }
    
    @Override
    public synchronized void invalidateCache() {
        cache = null;
    }
    

    3) “with MapMaker” – very poor performance.

    See my question at the top for the code.

    4) “with Suppliers.memoize” – good performance. But as the performance the same “Normal synchronisation” we need to optimize it or just use the “Normal synchronisation”.

    See the answer of the user “gid” for code.

    5) “with optimized memoize” – the performnce comparable to “no sync”-implementation, but thread-safe one. This is the one we need.

    The cache-class itself:
    (The Supplier interfaces used here is from Google Collections Library and it has just one method get(). see http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/base/Supplier.html)

    public class LazyCache<T> implements Supplier<T> {
        private final Supplier<T> supplier;
    
        private volatile Supplier<T> cache;
    
        public LazyCache(Supplier<T> supplier) {
            this.supplier = supplier;
            reset();
        }
    
        private void reset() {
            cache = new MemoizingSupplier<T>(supplier);
        }
    
        @Override
        public T get() {
            return cache.get();
        }
    
        public void invalidate() {
            reset();
        }
    
        private static class MemoizingSupplier<T> implements Supplier<T> {
            final Supplier<T> delegate;
            volatile T value;
    
            MemoizingSupplier(Supplier<T> delegate) {
                this.delegate = delegate;
            }
    
            @Override
            public T get() {
                if (value == null) {
                    synchronized (this) {
                        if (value == null) {
                            value = delegate.get();
                        }
                    }
                }
                return value;
            }
        }
    }
    

    Example use:

    public class BetterMemoizeCountryList implements ICountryList {
    
        LazyCache<List<String>> cache = new LazyCache<List<String>>(new Supplier<List<String>>(){
            @Override
            public List<String> get() {
                return loadCountryList();
            }
        });
    
        @Override
        public List<String> list(){
            return cache.get();
        }
    
        @Override
        public void invalidateCache(){
            cache.invalidate();
        }
    
        private List<String> loadCountryList() {
            // this should normally load a full list from the database,
            // but just for this instance we mock it with:
            return Arrays.asList("Germany", "Russia", "China");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.