Best Choice for a Growing, Fixed Performance Sequence

I am trying to keep a list of records in memory in the .NET cache by submitting the last N HTTP requests to my application. What is the best .NET sequence used for this?

Requirements

  • Fixed number of items (e.g. 50)
  • Serializable (need to add sequence to .NET cache)
  • When I try to add a max + 1 entry, it automatically deletes the oldest item to make room
  • Really do not care about the order of things.
  • You need to be able to get all the elements in one operation in order to perform aggregate calculations.
  • thread safe
  • Non-historical (e.g. List<T>, not Dictionary<TKey,TValue>). I could hit the URL /foo10 times, which is not unique, but everything needs to be added to the sequence.

Up to the head, I thought I could use it Queue<T>, and when enqueuing just checks the length, and if it falls into the container, delete the old one. But worry about thread safety ( ConcurrentQueue<T>maybe?) And the best approach, since this is the “hot” area of ​​my application that should be optimal.

Thank!

+4
source share
1 answer

It really depends on what you specifically mean by "oldest record."

FIFO, ConcurrentQueue<T>, ( ). ( ).

public class FixedSizedQueue<T> : ConcurrentQueue<T>
{
    private readonly object syncObject = new object();

    public int Size { get; private set; }

    public FixedSizedQueue(int size)
    {
        Size = size;
    }

    public new void Enqueue(T obj)
    {
        base.Enqueue(obj);
        lock (syncObject)
        {
            while (base.Count > Size)
            {
                T outObj;
                base.TryDequeue(out outObj);
            }
        }
    }
}

, , , ( System.Runtime.Caching), (LRU).

.NET, LurchTable CSharpTest.Net.Collections, NuGet.

LurchTable # LinkedHashMap

.

+4

Source: https://habr.com/ru/post/1687274/


All Articles