Is it there any LRU implementation of IDictionary?

前端 未结 10 1797
走了就别回头了
走了就别回头了 2020-11-29 21:20

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 mec

10条回答
  •  难免孤独
    2020-11-29 21:37

    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.

提交回复
热议问题