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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:48:05+00:00 2026-06-17T16:48:05+00:00

How to prevent loading the value that is not present in the cache many

  • 0

How to prevent loading the value that is not present in the cache many times simultanously, in the efficient way?

A typical cache usage is the following pseudocode:

Object get(Object key) {
 Object value = cache.get(key);
 if (value == null) {
  value = loadFromService(key);
  cache.set(key,value);
 }
 return value;
}

The problem: before the value is loaded from service (Database, WebService, RemoteEJB or anything else) a second call may be made in the same time, which will make the value loaded once again.

For example, when I’m caching all items for user X, and this user is often viewed, and have many items, there’s high probability of calling the load of his all items simultanously, resulting in heavy load on the server.

I could make get function synchronized, but this would force other searches to wait, making not much sense. I could create new lock for every key, but I don’t know if it’s a good idea to manage such large number of locks in Java (this part is language specific, the reason I’ve tagged it as java).

Or there is another approach I could use? If so, what would be the most efficient?

  • 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-17T16:48:06+00:00Added an answer on June 17, 2026 at 4:48 pm

    Don’t reinvent the wheel, use guava’s LoadingCache or memoizing supplier.

    If you are using Ehcache, read about read-through, this is the pattern you are asking for. You must implement the CacheEntryFactory interface to instruct the cache how to read objects on a cache miss, and you must wrap the Ehcache instance with an instance of SelfPopulatingCache.

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

Sidebar

Related Questions

is there a way with javascript/jquery to prevent images from loading? I am building
I have a set of values that are present on the page loading: eg
Due to timing issues, I'm trying to prevent an applet from loading until a
I would like to prevent unnecessary use of background-size in CSS3. By the way,
Using JNA, I am loading a dll written in C++ and calling function present
I have the following function to get the value in a textarea with the
The problem I am having is that my jquery script is not returning the
Is there any way to stop an iframe re-loading its contents when I change
The following codes works without jquery. I insert data but it does not update
I'm trying to prevent data from loading on the page when its needed. Currently,

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.