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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:26:48+00:00 2026-05-31T07:26:48+00:00

I have the following code which I’m trying to write a LRU Cache. I

  • 0

I have the following code which I’m trying to write a LRU Cache. I have a runner class that I’m running against random capacity of the cache. However, Cache size is exceeding it is capacity. When I make the FixLRU method synchronized, it becomes more accurate when the cache size is more than 100 however it gets slower. When I remove the synchronized keyword, cache is becoming less accurate.

Any ideas how to make this work properly? more accurate?

import java.util.concurrent.ConcurrentHashMap;

public abstract class Cache<TKey, TValue> implements ICache<TKey,TValue>{

    private final ConcurrentHashMap<TKey,TValue> _cache;

    protected Cache()
    {
        _cache=  new ConcurrentHashMap<TKey, TValue>();
    }

    protected Cache(int capacity){
        _cache = new ConcurrentHashMap<TKey, TValue>(capacity);
    }

    @Override
    public void Put(TKey key, TValue value) {
        _cache.put(key, value);
    }

    @Override
    public TValue Get(TKey key) {
        TValue value = _cache.get(key);

        return value;
    }

    @Override
    public void Delete(TKey key) {
        _cache.remove(key);
    }

    @Override
    public void Purge() {
        for(TKey key : _cache.keySet()){
            _cache.remove(key);
        }
    }

    public void IterateCache(){

        for(TKey key: _cache.keySet()){
            System.out.println("key:"+key+" , value:"+_cache.get(key));
        }

    }

    public int Count()
    {
        return _cache.size();
    }


}


import java.util.concurrent.ConcurrentLinkedQueue;

public class LRUCache<TKey,TValue> extends Cache<TKey,TValue> implements ICache<TKey, TValue> {

    private ConcurrentLinkedQueue<TKey> _queue;
    private int capacity;
    public LRUCache(){
        _queue = new ConcurrentLinkedQueue<TKey>();
    }

    public LRUCache(int capacity){
        this();
        this.capacity = capacity;
    }

    public void Put(TKey key, TValue value)
    { 
        FixLRU(key);

        super.Put(key, value);  
    }

    private void FixLRU(TKey key)
    {
        if(_queue.contains(key))
        {
            _queue.remove(key);
            super.Delete(key);
        }

        _queue.offer(key);

        while(_queue.size() > capacity){
            TKey keytoRemove =_queue.poll();
            super.Delete(keytoRemove);
        }
    }

    public TValue Get(TKey key){

        TValue _value = super.Get(key);

        if(_value == null){
            return null;
        }

        FixLRU(key);

        return _value;
    }

    public void Delete(TKey key){

        super.Delete(key);
    }

}

public class RunningLRU extends Thread{

    static LRUCache<String, String> cache = new LRUCache<String, String>(50);

    public static void main(String [ ] args) throws InterruptedException{

        Thread t1 = new RunningLRU();
        t1.start();
        Thread t2 = new RunningLRU();
        t2.start();
        Thread t3 = new RunningLRU();
        t3.start();
        Thread t4 = new RunningLRU();
        t4.start();
        try {
            t1.join();
            t2.join();
            t3.join();
            t4.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println(cache.toString());
        cache.IterateCache();
        System.out.println(cache.Count());

    }

    @Override
    public void run() {
        for(int i=0;i<100000;i++)
            cache.Put("test"+i, "test"+i);
    }

}
  • 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-31T07:26:49+00:00Added an answer on May 31, 2026 at 7:26 am

    I would clean up additional entries after adding your entry. This minimises the time that the cache will be larger than you wanted. You can also trigger size() to perform a cleanup.

    Any ideas how to make this work properly?

    Does your test reflect how your application behaves? It may be that the cache behaves properly (or much closer to it) when you have not hammering it. 😉

    If this test does reflect your application behaviour then perhaps an LRUCache is not the best choice.

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

Sidebar

Related Questions

I'm trying to use opengl in C#. I have following code which fails with
I have following code which works as is, but does not work when cache=T
I have the following code which is trying to stop propagation, works every where
I have the following code which creates a dynamic object that is assigned to
I have the following code which I want to be running asynchronously, but the
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code which works just fine when the method is POST,
I have the following code which works fine. However, I only want to return
I have the following code which should put programs startable in Bash. if [
I have the following code which reads in the follow file, append a \r\n

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.