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 8528635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:50:49+00:00 2026-06-11T08:50:49+00:00

We have a simple but very much used cache, implemented by a ConcurrentHashMap. Now

  • 0

We have a simple but very much used cache, implemented by a ConcurrentHashMap. Now we want to refresh all values at regular times (say, every 15 minutes).

I would like code like this:

 private void regularCacheCleanup() {
        final long now = System.currentTimeMillis();
        final long delta = now - cacheCleanupLastTime;
        if (delta < 0 || delta > 15 * 60 * 1000) {
            cacheCleanupLastTime = now;
            clearCache();
        }
  }

Except it should be:

  • Thread safe
  • Non-blocking and extremely performant if the cache isn’t going to be cleared
  • No dependencies except on java.* classes (so no Google CacheBuilder)
  • Rock-solid 😉
  • Can’t start new threads

Right now I think to implement a short timer in a ThreadLocal. When this expires, the real timer will be checked in a synchronized way. That’s an awfull lot of code, however, so a more simple idea would be nice.

  • 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-06-11T08:50:51+00:00Added an answer on June 11, 2026 at 8:50 am

    The mainstream way to tackle this issue would be by using some timer thread to refresh your cache on specified intervals. However, since you don’t need to create new threads, a possible implementation that i can think of is that of a pseudo-timed cache refresh. Basically, i would insert checks in cache accessors (put and get methods) and each time clients would use this methods, i would check if the cache needs to be refreshed before performing the put or get action. This is the rough idea:

    class YourCache {
    
      // holds the last time the cache has been refreshed in millis
      private volatile long lastRefreshDate;
    
      // indicates that cache is currently refreshing entries
      private volatile boolean cacheCurrentlyRefreshing;
    
      private Map cache = // Your concurrent map cache...
    
      public void put(Object key, Object element) {
        if (cacheNeedsRefresh()) {
          refresh();
        }
        map.put(key, element);
      }
    
      public Object get(Object key) {
        if (cacheNeedsRefresh()) {
          refresh();
        }
        return map.get(key);
      }
    
      private boolean cacheNeedsRefresh() {
        // make sure that cache is not currently being refreshed by some
        // other thread.
        if (cacheCurrentlyRefreshing) {
          return false;
        }
        return (now - lastRefreshDate) >= REFRESH_INTERVAL;
      } 
    
      private void refresh() {
        // make sure the cache did not start refreshing between cacheNeedsRefresh()
        // and refresh() by some other thread.
        if (cacheCurrentlyRefreshing) {
          return;
        }
    
        // signal to other threads that cache is currently being refreshed.
        cacheCurrentlyRefreshing = true;
    
        try {
          // refresh your cache contents here
        } finally {
           // set the lastRefreshDate and signal that cache has finished
           // refreshing to other threads.
           lastRefreshDate = System.currentTimeMillis();
           cahceCurrentlyRefreshing = false;
        }
      }
    }
    

    Personally i wouldn’t consider doing it like so, but if you don’t want or can’t create timer threads then this could be an option for you.

    Note that although this implementation avoids locks, it is still prone to duplicate refreshes due to race events. If this is ok for your requirements then it should be no problem. If however you have stricter requirements then you need to put locking in order to properly synchronise the threads and avoid race events.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a very simple task in every language I have ever used, but
I have a very simple scenario but poor experience in this area and googling
I have a very simple problem but cannot find a nice solution. I have
I have a very simple arithmetic operator but am at my wits end why
I have a very simple problem, but I can't seem to solve it. I
I have a very simple task I am trying to do in Groovy but
I have a very simple table with two columns, but has 4.5M rows. CREATE
I have a very simple .net application for testing SMTP on .net. But i
So I have a very simple class that has a method called getThumbUrl() but
I'm sure this is very simple. I have gotten colorbox to work before, but

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.