enzom83
enzom83

Reputation: 8310

How can I implement a "swapping" list?

I would need to manage some lists with timer: each element of these lists is associated with a timer, so when the timer expires, the corresponding element must be removed from the list.

In this way the length of the list does not grow too much because, as time goes on, the elements are progressively removed. The speed with which the list length increases also depends on the rate of addition of new elements.

However I need to add the following constraint: the amount of RAM used by the list does not exceed a certain limit, ie the user must specify the maximum number of items that can be stored in RAM. Therefore, if the rate of addition of the elements is low, all items can be stored in RAM. If, however, the rate of addition of elements is high, old items are likely to be lost before the expiration of their timers.

Intuitively, I thought about taking a cue from swapping technique used by operating systems.

class SwappingList
{
    private List<string> _list;
    private SwapManager _swapManager;

    public SwappingList(int capacity, SwapManager swapManager)
    {
        _list = new List<string>(capacity);
        _swapManager = swapManager;

        // TODO

    }
}

One of the lists that I manage is made ​​up of strings of constant length, and it must work as a hash table, so I should use HashMap, but how can I define the maximum capacity of a HashMap object?

Basically I would like to implement a caching mechanism, but I wish that the RAM used by the cache is limited to a number of items or bytes, which means that old items that are not expired yet, must be moved to a file.

Upvotes: 1

Views: 131

Answers (2)

Yahia
Yahia

Reputation: 70369

According to the comments above you want a caching mechanism.

.NET 4 has this build-in (see http://msdn.microsoft.com/en-us/library/system.runtime.caching.aspx) - it comes with configurable caching policy which you can use to configure expiration among other things... it provides even some events which you can assign delegates to that are called prior removing a cache entry to customize this process even further...

Upvotes: 5

Mike Nakis
Mike Nakis

Reputation: 61993

You cannot specify the maximum capacity of a HashMap. You need to implement a wrapper around it, which, after each insertion, checks to see if the maximum count has been reached.

It is not clear to me whether that's all you are asking. If you have more questions, please be sure to state them clearly and use a question mark with each one of them.

Upvotes: 1

Related Questions