LRU caches in C

Posted by lazyconfabulator on Stack Overflow See other posts from Stack Overflow or by lazyconfabulator
Published on 2010-06-12T04:25:54Z Indexed on 2010/06/12 4:32 UTC
Read the original article Hit count: 339

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about c

    Related posts about caching