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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:37:35+00:00 2026-05-15T12:37:35+00:00

I need to cache a large (but variable) number of smallish (1 kilobyte to

  • 0

I need to cache a large (but variable) number of smallish (1 kilobyte to 10 megabytes) files in memory, for a C application (in a *nix environment). Since I don’t want to eat all my memory, I’d like to set hard memory limit (say, 64 megabytes) and push files into a hash table with the file name as the key and dispose of the entries with the least use. What I believe I need is an LRU cache.

Really, I’d rather not roll my own so if someone knows where I can find a workable library, please point the way? Failing that, can someone provide a simple example of an LRU cache in C? Related posts indicated that a hash table with a doubly-linked list, but I’m not even clear on how a doubly-linked list keeps LRU.

Side note: I realize this is almost exactly the function of memcache, but it’s not an option for me. I also took a look at the source hoping to enlighten myself on LRU caching, with no success.

  • 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-15T12:37:36+00:00Added an answer on May 15, 2026 at 12:37 pm

    Related posts indicated that a hash table with a doubly-linked list, but I’m not even clear on how a doubly-linked list keeps LRU.

    I’m just taking a guess here, but you could do something like this (using pseudo-C here because I’m lazy). Here are the basic data structures:

    struct File
    {
        // hash key
        string Name;
    
        // doubly-linked list
        File* Previous;
        File* Next;
    
        // other file data...
    }
    
    struct Cache
    {
        HashTable<string, File*> Table // some existing hashtable implementation
        File* First; // most recent
        File* Last;  // least recent
    }
    

    And here’s how you’d open and close a file:

    File* Open(Cache* cache, string name)
    {
        if (look up name in cache->Table succeeds)
        {
            File* found = find it from the hash table lookup
            move it to the front of the list
        }
        else
        {
            File* newFile = open the file and create a new node for it
    
            insert it at the beginning of the list
    
            if (the cache is full now)
            {
                remove the last file from the list
                close it
                remove it from the hashtable too
            }
        }
    }
    

    The hashtable lets you find nodes by name quickly, and the linked-list lets you maintain them in use order. Since they point to the same nodes, you can switch between them. This lets you look a file up by name, but then move it around in the list afterwards.

    But I could be totally wrong about all of this.

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

Sidebar

Related Questions

I have a need to hold a very large number of XMLs in memory
I need the browser to cache a large, mostly static .php file. I open
I need a cache that responds to memory pressure like the one build-into ASP.NET.
I need to invalidate cache in a web application when related data is updated
Hi I need to clear IE8 browser cache before loading my cd-based php application.
I am in need of an in-memory general-purpose cache for my iOS app that
I have something I need a 2D array for, but for better cache performance,
In my application, at one point I need to perform calculations on a large
In the project I need to cache the database data fetched from each request.So
I'm moving away from Serializable objects (need to cache and move things around -

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.