views:

275

answers:

1

I've created a custom resource provider which returns strings from our database to use for whatever.

The issue is I haven't found a way to "bust" the cache on these items and reload them from the database. Ideally I'd do this every x minutes or manually when we update the cache and want to reload it.

Technically, that's easy. I'm storing everything in a hashtable so just nuke that and reload when needed. However since the ResourceProviderFactory handles the loading of each resource class and I'm not sure how to enumerate the classes it has created.

An overview of the code:

public class ResourceProvider : IResourceProvider
{

 private Dictionary<string, Dictionary<string, object>> _resourceCache
  = new Dictionary<string, Dictionary<string, object>>();

 private string _virtualPath;
 private string _className;

 public ResourceProvider(string virtualPath, string className)
 {
  _virtualPath = virtualPath;
  _className = className;
 }

 public object GetObject(string resourceKey, CultureInfo culture)
 {
  ...
 }
}


public class ResourceProviderFactory :System.Web.Compilation.ResourceProviderFactory
{
 public override IResourceProvider CreateGlobalResourceProvider(string classKey)
 {
  return new Cannla.Business.Resource.ResourceProvider(string.Empty, classKey);
 }
  ...
  ...
}

What I was planning to do was add a reference every time CreateGlobalResourceProvider is called (e.g. add the new object to a collection and then enumerate that and nuke everything inside that object when I need to) but I'm unsure if this is going to do something weird to the ResourceProviderFactory.

Is there any ResourceProviderFactory IEnumerable method or something that will give me all those objects in a easy way before I go building all this code?

A: 

If you can use ASP.Net cache system Just use it. You can access it using HttpRuntime.Cache and add timed invalidation check the example below:

var Key = "Resources:" + ResourceType + ResourceKey + Cutlure.Name;
HttpRuntime.Cache.Insert(Key, ValueToCache,
    null, DateTime.Now.AddMinutes(1d), 
    System.Web.Caching.Cache.NoSlidingExpiration);

If you can't use ASP.Net cache system

You can put the logic that triggers cache invalidation inside of IResourceProvider. ie. just check in GetObject if the cache expired.

Piotr Czapla
it's not the timing that's the issue, i'm was looking for how to get references to each object created. thanks for the idea however.
marshall