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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:04:16+00:00 2026-05-27T15:04:16+00:00

I created a class which is used as a cache provider. It uses a

  • 0

I created a class which is used as a cache provider. It uses a Map, timestamped map entries and it spawns a Thread which performs cleanup every so often. This class is used in a web application. This web application had a problem where POST would take 30 seconds. I traced the problem to this cache class, eliminating it resolves the problem.

I have tried my best to find the error in this class but I can’t. Please help me out here.
Assume User class is some kind of POJO describing the user.

public class UserStore implements Thread.UncaughtExceptionHandler {
private static volatile UserStore instance;
private static Thread cleanUpThread;
private static Map<String, TimeStampedToken<User>> tokenMap = new HashMap<String, TimeStampedToken<User>>();
public static UserStore getInstance() {
    if (instance == null) {
         synchronized(UserStore.class) {
             if (instance == null) {
                 instance = new UserStore();
                 cleanUpThread = new Thread(new CleanUpWorker());
                 cleanUpThread.setUncaughtExceptionHandler(instance);
                 cleanUpThread.start();
             }
         }
    }
    return instance;
}
public void uncaughtException(Thread thread, Throwable throwable) {
    if (throwable instanceof ThreadDeath) {
         cleanUpThread = new Thread(new CleanUpWorker());
         cleanUpThread.setUncaughtExceptionHandler(this);
         cleanUpThread.start();
         throw (ThreadDeath)throwable;
    }

}
private static class CleanUpWorker implements Runnable {
    private static final long CLEANUP_CYCLE_MS = 300000;
    private static final long OBJECT_LIVE_TIME = 299900;
    public void run() {
        long sleepRemaining; 
        long sleepStart = System.currentTimeMillis();
        sleepRemaining = CLEANUP_CYCLE_MS; 
        while (true) {
            try {
                sleepStart = System.currentTimeMillis();
                Thread.sleep(sleepRemaining);
                cleanUp();
                sleepRemaining = CLEANUP_CYCLE_MS;
            } catch (InterruptedException e) {
                sleepRemaining = System.currentTimeMillis() - sleepStart;
            }
        }
    }
    private void cleanUp() {
        Long currentTime = System.currentTimeMillis();
        synchronized(tokenMap) {
            for (String user : tokenMap.keySet()) {
                TimeStampedToken<User> tok = tokenMap.get(user);
                if (tok.accessed + OBJECT_LIVE_TIME < currentTime) {
                    tokenMap.remove(user);
                }
            }
        }
    }

}
public void addToken(User tok) {
    synchronized(tokenMap) {
        tokenMap.put(tok.getUserId(), new TimeStampedToken<User>(tok));
    }
}
public User getToken(String userId) {
    synchronized(tokenMap) {
        TimeStampedToken<User> user = tokenMap.get(userId);
        if (user != null) {
            user.accessed = System.currentTimeMillis();
            return user.payload;
        } else {
            return null;
        }

    }
}
private static class TimeStampedToken<E> {
    public TimeStampedToken(E payload) {
        this.payload = payload;
    }
    public long accessed = System.currentTimeMillis();
    public E payload; 
}
}
  • 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-27T15:04:17+00:00Added an answer on May 27, 2026 at 3:04 pm

    Here is how I would approach it. With multi-threaded code, simplicity is often the best approach as its more likely to work.

    (the third parameter trueof the LinkedHashMap means that iterators over this Map follow the order of access rather than order of insertion)

    public enum UserStore {
        ;
    
        interface User {
            String getUserId();
        }
    
        // a LRU cache with a timestamp.
        private static final Map<String, TimeStampedToken<User>> tokenMap = new LinkedHashMap<String, TimeStampedToken<User>>(16, 0.7f, true);
        private static final long OBJECT_LIVE_TIME = 299900;
    
        public static synchronized void addToken(User tok) {
            final long now = System.currentTimeMillis();
            // clean up as we go
            for (Iterator<Map.Entry<String, TimeStampedToken<User>>> iter = tokenMap.entrySet().iterator(); iter.hasNext(); ) {
                final Map.Entry<String, TimeStampedToken<User>> next = iter.next();
                if (next.getValue().accessed + OBJECT_LIVE_TIME >= now)
                    // the map is ordered by access time so there are no more to clean up.
                    break;
                iter.remove();
            }
            // add a new entry
            tokenMap.put(tok.getUserId(), new TimeStampedToken<User>(tok, now));
        }
    
        public static synchronized User getToken(String userId) {
            final long now = System.currentTimeMillis();
            TimeStampedToken<User> user = tokenMap.get(userId);
            if (user == null)
                return null;
    
            user.accessed = now;
            return user.payload;
        }
    
        static class TimeStampedToken<E> {
            long accessed;
            final E payload;
    
            TimeStampedToken(E payload, long now) {
                this.payload = payload;
                accessed = now;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I create class libraries, some which are used by others around the world, and
I want to create a class that initializes a timer which will be used
I recently created a class which has a constructor taking 3 enumerations as arguments.
I am using C#. I have created a class which can be included in
I am using the apache library. I have created a class which sends a
I've created a class library which exposes my back-end object model. I don't wish
I created a simple class in C++ which has a private dynamic array. In
I have created a class for a dashboard item which will hold information such
I have created a class library called AddServiceLibrary in which I have a method
Tearing my hair out. I created an as3 class - blah.Foo, which extends MovieClip.

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.