Cool things with System.Web.Caching.CacheDependency. This simple example stores some settings for a table of contents control in an ASP.NET application. The settings in the configuration file apply to all users. One requirement is that the admin must be able to edit these settings and make them available to the application without interruption. This we achieve with the CacheDependency class.
|
/// <summary> /// Key for the TableOfContentsSettings stored in /// ASP.NET application’s Cache. /// </summary> private const string _tocSettingsIdentifier = “TableOfContentsSettingsIdentifier”;
/// <summary> /// Gets or sets the table of contents settings. /// </summary> /// <value>The table of contents settings.</value> private NameValueCollection TableOfContentsSettings { get { object settingsCollectionObject = this.Page.Cache[_tocSettingsIdentifier]; if (settingsCollectionObject != null) { return settingsCollectionObject as NameValueCollection; } else { // Read the TOC settings stored in our XML file NameValueCollection settingsCollection = ReadConfiguration();
// Support “real time” updates where admin edits // config file. We’ll set up a dependency on the // the XML config file. If the file is modified in any // way, it will be purged from the cache, forcing // this block to run the next time the // TableOfContentsSettings is requested. Cool. string filename = this.GetConfigurationFilePath(); CacheDependency dependency = new CacheDependency(filename, DateTime.Now); this.Page.Cache.Insert(_tocSettingsIdentifier, settingsCollection, dependency);
// Set the changed flag so we know to update // the browser. Don’t set it if first time loading. if (this.Page.IsPostBack || this.Page.IsCallback) { this.HasSettingsChanged = true; } }
return config; } set { this.Page.Cache[_tocSettingsIdentifier] = value; } } |
Read up on the options the CacheDependency class gives you. You can set up dependencies on other objects, files, arrays, etc.