string filePath = @"C:\temp\file.txt"; CacheDependency cacheDependency = new CacheDependency(filePath);
string connectionString = "server=myServerAddress;database=myDataBase;uid=myUsername;pwd=myPassword;"; string tableName = "myTable"; SqlCacheDependencyAdmin.EnableNotifications(connectionString); SqlCacheDependencyAdmin.EnableTableForNotifications(connectionString, tableName); CacheDependency cacheDependency = new SqlCacheDependency(tableName, connectionString);
public class MyCustomClass { public bool IsModified { get; set; } } MyCustomClass myCustomObject = new MyCustomClass(); CacheDependency cacheDependency = new CacheDependency(null, new string[] { "IsModified" }, myCustomObject);In this example, a CacheDependency object is created based on a custom class. The CacheDependency constructor takes in an object and an array of property names to monitor for changes. In this case, any changes to the MyCustomClass object's IsModified property will trigger the cached item to be invalidated. Package/library: The System.Web.Caching.CacheDependency class is part of the System.Web.dll assembly, which is included in the .NET Framework.