简体   繁体   中英

Best structure for cache like data structure in C#?

this was more a conceptual question, but I was wondering what the best C# data structure is for a cache like structure. I'll explain what I'm looking for.

I'm making a game that uses sprites for graphics. Sprites are based on Images (SFML library images, not standard C# images, but I don't think that really matters). These images need to be loaded from a file on disk, and this can get costly (time wise) when many sprites need to be created in rapid succession. To reduce some of this load time, I want to make use of a cahce like structure to store the images files, and when they were last updated (I'll be using this update time to determine when to evict stuff from the cache). The cache will be indexed using a combination of the sprite name and a few other details. this index will map to a structure like below:

struct ImageEntry
{
    Image image;
    TickCount lastupdate;
    TickCount evictionTime;
}

When the game requests a sprite, the request will be issued to a content manager who will look for a match in the cache structure. If a match is found, a structure like above will be found. The content manager will use the image to make the sprite, then update the lastupdate time to the time the structure was called. If a match is not found, a new strucut will be made and the image loaded from the image file (and used to make the sprite). The lastupdate time will be set to the current time. eviction time is set based on a predefined constant, or an optional component in the request.

Now heres what I need to do: periodically, I need to run through the whole cache, compare each struct's lastupdate to the current game time. If the different between those two is greater then the eviction time, I want to evict the struct entry from the cache. Additionally, if a sprite request issues a new struct, and the size of the cache is greater then a predefined value, I need to find the least recently updated struct and evict it to make room for the new struct.

essentially, I'm looking for the ideal data structure the has quick lookups for indexing keys, and quick iteration for the time updates/size overflow evictions.

Making a Dictionary was my first thought, and that certainly has fast indexing, but given I'm going to be removing elements and adding new ones fairly regularly, I'm concerned about the performance. List> could work, but then lookup is going to take time.

For reference, I was likely going to be limiting the cahce to about 30-40 images at a time, and range anywhere from 2 kb to 500 kb and eviction time will typically be about ~6 minutes. I'd like to be able to check for evictions at least every 1-2 seconds

I was wondering if anyone could provide some insight into the best type of data structure to use for this.

Thanks

For your stated use case (40 items in cache, eviction check every 1 second) a dictionary should be a completely fine data structure.

If you are developing against .NET 4 you could also try the built-in MemoryCache class: http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx

Either way, you should do some performance testing to make sure your application runs smoothly.

A class in a library I wrote might give you some inspiration - it's similar but doesn't have the time-based cache eviction that you describe:

https://github.com/xpaulbettsx/ReactiveXaml/blob/master/ReactiveXaml/MemoizingMRUCache.cs#L31

The core thing to remember though, is that keeping 2 separate data structures isn't really the worst thing since your ImageEntry is a class (hint: don't make it a struct) and the raw image data will be orders of magnitude bigger than the extra overhead of maintaining both a dictionary and a list for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM