Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to cache data in a C# dll?

I've written a DLL that may be used in a number of ways (referenced by ASP.NET web sites, WinForms, etc.). It needs to load its data from several delimited files that will be automatically updated on a semi-regular basis. For performance reasons, I'm loading the data into a static object (a generic List), and only want to go back to the files when the data changes. (The amount of data doesn't take up an unreasonable amount of memory, but it does take too long to read from the file on every access). I've tried using a FileSystemWatcher, but that's proven unreliable - it periodically misses file updates. Since I can't count on the DLL running inside a web site, the ASP.NET CacheDependency option doesn't seem appropriate. Has anyone found a good solution to this sort of thing?

like image 307
Jeremy Gruenwald Avatar asked Jan 20 '09 21:01

Jeremy Gruenwald


People also ask

What is the best caching strategy?

A cache-aside cache is the most common caching strategy available. The fundamental data retrieval logic can be summarized as follows: When your application needs to read data from the database, it checks the cache first to determine whether the data is available.

What is caching in C?

Caching is a separate memory, yes, and it has it's own addresses, but when the CPU caches memory lines from RAM, it keeps a record of what RAM-addresses the memory was on, and keeps a map between RAM-address and cache-address. From the point of view of your program, the address is the same.

What is the best caching strategy that ensures that data is always fresh?

Write-through ensures that data is always fresh, but can fail with empty nodes and can populate the cache with superfluous data. By adding a time to live (TTL) value to each write, you can have the advantages of each strategy. At the same time, you can and largely avoid cluttering up the cache with extra data.

What is a good memory cache?

Some people say that you need about 1MB of cache if you are just browsing the Internet, whereas others say that 8MB should be more than enough. It really depends on what you do with your computer most of the time. If you are a gamer, then you might want to increase the cache to 12MB at least.


3 Answers

The ASP.NET Cache and associated CacheDependency has no dependency on ASP.NET, IIS, or a web server. You are free to use it for this exact situation. It requires much less effort to get working than the Enterprise Library Caching Application Block and is more widely used and documented.

Some additional reference: http://aspalliance.com/1705_A_New_Approach_to_HttpRuntimeCache_Management.all

If you want to use the ASP.NET Cache logic in your DLL, simply wrap it and reference it like so:

using System.Web.Caching;
...
var cache = HttpRuntime.Cache;
cache.Insert("foo", foo);

This code, along with any FileSystemDependency you care to add to it, will work equally well in your library regardless of whether or not it is running in a web, winform, console, or service context.

You can also do this if you want to be sure to use the same cache as the web, but it's optional:

HttpContext context = HttpContext.Current;
            if(context != null)
            {
                _cache = context.Cache;
            }
            else
            {
                _cache = HttpRuntime.Cache;
            }
like image 174
ssmith Avatar answered Oct 17 '22 09:10

ssmith


Have you looked at the Enterprise Library Caching application block? It allows for cache invalidation on a number of different events,including file changes (AFAIK), and does not require ASP.NET (and yet, integrates well with it).

like image 27
casperOne Avatar answered Oct 17 '22 08:10

casperOne


You could save the last date you loaded from the the file as another file in the same directory, or in a static date time in your class. I think the last solution is probably not what you are looking for since you can't count on your class being loaded. I would also consider saving and comparing an MD5 hash of the file to see when it changes.

like image 29
Anthony D Avatar answered Oct 17 '22 09:10

Anthony D