bitfaster/BitFaster.Caching

Usage as global Dictionary within Website

nhaberl opened this issue · 3 comments

Hi, looking for a better Collection than ConcurrentDictionary for Read Heavy website in terms of memory usage. I have several million items of string / objects
Is this what you target for ?

If it is read-only, why not just use a regular array?

Because its a KV like structure, needs constant lookup time and has to be thread safe.

I originally wrote ConcurrentLru for this exact scenario, to replace use of MemoryCache in a web server.

If you have many more objects than will fit into memory, use of a ConcurrentDictionary would eventually result in out of memory as more objects are fetched. ConcurrentLRU uses a simple pseudo LRU algorithm to determine which objects to discard, such that the cache has a bounded size. In this sense, memory usage is better since growth is constrained.

ConcurrentLru internally uses ConcurrentDictionary, so for the same number of objects ConcurrentLru will use very slightly more memory since it maintains state to perform additional bookkeeping. This is very unlikely to be noticeable.