3

I have a C# application that handles about 10.000 immutable objects, each object is of 50KB - 1MB size. The application picks about 10-100 objects for every operation. Which objects are picked depend on circumstances and user choices, but there are a few that are very frequently used.

Keeping all object in memory all time, is way too much, but disk access time is pressing. I would like to use a popularity-based cache to reduce disk activity. The cache would contain max. 300 objects. I expect that during the usage patterns decides which one should be cached. I can easily add an access counter to each object. The more popular ones get in, less popular ones have to leave the cache. Is there an easy, ingenious way to do that without coding my butt off?

user256890
  • 3,396
  • 5
  • 28
  • 45

4 Answers4

1

One ready-made solution is to use ASP.NET caching's sliding expiration window.

Brian Gideon
  • 47,849
  • 13
  • 107
  • 150
Aliostad
  • 80,612
  • 21
  • 160
  • 208
1

Well you can use System.Runtime.Caching. Cache the objects, which are constantly used, if the cahced objects changes after some time you can specify how much time the cache is Valid. Once the cahce is invalid, in the event handler you can rebuild the cache.

Make sure you use some thread synchronization mechanism when rebuilding cache.

Sandeep
  • 7,156
  • 12
  • 45
  • 57
  • 1
    +1 for not reinventing the wheel. Probably important to know that the OP wants to use a sliding expiration, which will preferentially keep the most frequently used items in cache. – Chris Shain Apr 17 '12 at 13:12
1

I'd go with WeakReferences: you can build a simple cache manager on top of that in a couple of mins, and let .NET handles the actual memory management by itself.

It may not be the best solution if you need to limit the amount of memory you want your program to use, but otherwise it's definitely worth checking out.

Lâm Tran Duy
  • 804
  • 4
  • 8
0

Sounds like a job for MemCached! This is a free, open-source, high-performance and flexible caching solution. You can download it at http://www.memcached.org.

To get a broad overview, look at the Wikipedia page at https://en.wikipedia.org/wiki/Memcached.

Good luck!

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76