Alex Rivera | Logout

Is it there any LRU implementation of IDictionary?

Asked 2009-04-15T23:55:56.143
42

I would like to implement a simple in-memory LRU cache system and I was thinking about a solution based on an IDictionary implementation which could handle an hashed LRU mechanism. Coming from java, I have experiences with LinkedHashMap, which works fine for what I need: I can't find anywhere a similar solution for .NET.

Has anyone developed it or has anyone had experiences like this?

Edit
Report

1 Answer

2

I like Lawrence's implementation. Hashtable + LinkedList is a good solution.

Regarding threading, I would not lock this with[MethodImpl(MethodImplOptions.Synchronized)], but rather use ReaderWriterLockSlim or spin lock (since contention usually fast) instead.

In the Get function I would check if it's already the 1st item first, rather than always removing and adding. This gives you the possibility to keep that within a reader lock that is not blocking other readers.

answered 2010-11-06T00:11:13.683

Your Answer