Is it there any LRU implementation of IDictionary?

前端 未结 10 1774
走了就别回头了
走了就别回头了 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:35

    I don't believe so. I've certainly seen hand-rolled ones implemented several times in various unrelated projects (which more or less confirms this. If there was one, surely at least one of the projects would have used it).

    It's pretty simple to implement, and usually gets done by creating a class which contains both a Dictionary and a List.

    The keys go in the list (in-order) and the items go in the dictionary.
    When you Add a new item to the collection, the function checks the length of the list, pulls out the last Key (if it's too long) and then evicts the key and value from the dictionary to match. Not much more to it really

提交回复
热议问题