简体   繁体   中英

Sharing static classes data accross web garden/farm

Scenario:

  • ASP.NET web site
  • Global static classes containing TONS of configuration data
  • the static data is mostly read-only, but it can change every once in a while
  • data in static classes MUST BE real time, at all times

now, this works really great in a single worker process , all the configuration data is in memory at all times (writing changes to disk only when modifications are made to the data), and since it is used a LOT, it speeds up the application by orders of magnitude (compared to having it stored on the DB and querying against it on every request)

how would you go about converting this application so that it could be used in a web garden, or even web farm environment?

what I'm looking for is a real time synchronization mechanism

the issue, obviously, is that each worker would have it's own copy of the static classes data; I have some ideas on the approach that I would take, but would like to see what other ideas people have

You could check the date on a shared file on the file system. Every time the configuration data changes, simply touch the file to change the modification date. Compare the date your program last loaded the data with the file's date. If the file has a newer date, reload the data, and update your LastLoaded date.

It is light weight, and it allows you to easily trigger data reloads. So long as the file hasn't changed in a while, the OS should have most of the file metadata in ram anyway.

While I haven't done performance comparisons, I've always found this way to be the a robust and easy to implement way to pass the "Reload your cache" message around to all clients.

DateTime cacheDate = DateTime.Now.AddHours(-1);
DateTime fileDate = System.IO.File.GetLastWriteTime(@"C:\cachefile.txt");
if(fileDate > cacheDate)
{
    //Reload cache
    cacheDate = DateTime.Now;
}
//Get Cached Data

You could also use the HttpContext cache.

Context.Cache.Add(key, val, 
    new System.Web.Caching.CacheDependency(@"C:\cachefile.txt"),  
    System.Web.Caching.Cache.NoAbsoluteExpiration, 
    System.Web.Caching.Cache.NoSlidingExpiration, 
    System.Web.Caching.CacheItemPriority.Default, 
    null/* or your Callback for loading the cache.*/);

I haven't used this before in this way, so I don't know much about how it works.

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