An in depth look at the cachedependency objects and their surroundings in cache dependencies

  • 2020-05-12 03:08:06
  • OfStack

Recently, I have been learning about cache dependency. Now I have sorted out some learning materials as follows:
Cache dependencies mainly provide the following functions:
1. The SQL cache dependency can be used for application caching and page output caching.
2. SQL cache dependencies are available in SQL Server 7.0 and later.
3. You can use SQL cache dependencies in a network park (multiple processors on one server) or a network farm (multiple servers running the same application).
4. The database operations associated with the SQL cache dependencies are simple and therefore do not impose high processing costs on the server.

Now let's take the following collection of objects related to cache dependencies and analyze the differences to a certain extent...
Cache dependencies are mainly implemented by three core classes: CacheDependency, AggregateCacheDependency, and SqlCacheDependency. The CacheDependency class is the parent of the AggregateCacheDependency class and the SqlCacheDependency class.

CacheDependency tracks cache dependencies, which can be files, directories, or keys to other objects in the application's Cache and can be used to implement custom cache dependencies. // works on documents.

The SqlCacheDependency class monitors a specific SQL Server database table on all supported SQL Server versions (7.0, 2000, 2005) so that when the table changes, items associated with the table are automatically deleted from Cache. When a database table changes, the cached entries are automatically dropped and a new version of the entry is added to Cache. In the use of SQL Server 2005 database, SqlCacheDependency class also support and System. Data. SqlClient. SqlDependency class for integration. Use SQL Server 2005's query notification mechanism to detect data changes that invalidate SQL query results. Any associated with SQL query cache entry will be from System. Web. Caching. Cache removed. When using SQL Server 2005, you can use the SqlCacheDependency class to add items to the Cache of your application that depend on SQL Server database tables or SQL queries. // support for data tables

The AggregateCacheDependency class monitors the collection of dependency objects so that when any dependency object changes, the cached item is automatically removed. The objects in the array can be CacheDependency or SqlCacheDependency objects, custom objects derived from CacheDependency, or any combination of these objects.

The AggregateCacheDependency class differs from the CacheDependency class in that it allows you to associate multiple dependencies of different types with a single cached item. For example, if you create a page that imports data from the SQL Server database table and the XML file, you can create an SqlCacheDependency object to represent the database table's dependencies, and an CacheDependency to represent the XML file's dependencies. Instead of calling the Cache.Insert method for each dependency, you can create an instance of the AggregateCacheDependency class and add each dependency to the class. You can then make the page dependent on the AggregateCacheDependency instance using a single Insert call.


 Among them, this chapter mainly describes CacheDependency The use of.  
CacheDependency There are several overload, the role of the following. 
// Assume that the cached source file is in the current directory data.xml file 
// Cache the dependent file path 
    CacheDependency mydep = new CacheDependency("data.xml");
// There can be multiple cache dependent files 
    CacheDependency mydep1=new CacheDependency(new string []{"data.xml","data1.xml"});
// Check the timing of cache dependency changes 
    CacheDependency mydep2 = new CacheDependency("data.xml", DateTime.Now);
// Check the timing of multiple dependent file changes 
    CacheDependency mydep3 = new CacheDependency(new string[] { "data.xml", "data1.xml" }, DateTime.Now);
// Checking depends on multiple files and also on multiple cache key values 
    CacheDependency mydep4 = new CacheDependency(new string[] { "data.xml", "data1.xml" },new string[] { "Category", "Category1" });
// Associated dependencies, can also depend on another 1 File cache dependencies 
    CacheDependency mydep5 = new CacheDependency(new string[] { "data.xml", "data1.xml" },new string[] { "Category", "Category1" }, mydep);
// The last time the file and key values were modified 
    CacheDependency mydep6 = new CacheDependency(new string[] { "data.xml", "data1.xml" },new string[] { "Category", "Category1" }, DateTime.Now);
// File, the other 1 Cache dependencies and key values were last modified based on time 
    CacheDependency mydep6 = new CacheDependency(new string[] { "data.xml", "data1.xml" },new string[] { "Category", "Category1" }, mydep,DateTime.Now);


public partial class CacheDependencyPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetData();
        }
    }
    private void GetData()
    {
        DataTable tableData = new DataTable();
        if (Cache["data"] == null)// The cache key used to reference the item. If the cache changes, then true ; Details of baidu c# the Cache Object! 
        {
            DataSet ds = new DataSet();
            string filePath = Server.MapPath("~/App_Data/XMLFile.xml");// Server data absolute address 
            ds.ReadXml(filePath);// Read the data in the file and save it in ds In the 
            tableData = ds.Tables[0];
            CacheDependency cdy = new CacheDependency(filePath,DateTime.Now);
            Cache.Insert("data", tableData, cdy);// Through the use of  Insert( overloading Insert methods ) Method to add an item to the cache 
            //if (cdy.HasChanged)
            //{
                System.Diagnostics.Debug.WriteLine("Xml Have been changed ");
            //}
        }
        else
        {
            tableData = (DataTable)Cache["data"];// Adds an item to the cache by specifying its key and value 
        }
        grvCS.DataSource = tableData;
        grvCS.DataBind();
        //DataSet mds = new DataSet();// Validation data binding is used because xml The problem of hierarchy led to the failure of data binding; 
        //mds.ReadXml(Server.MapPath("~/App_Data/XMLFile.xml")); 
        //grvCS.DataSource = mds;
        //grvCS.DataBind();
    }
}

Among them
1. Remember to bind the corresponding data fields for gridview on the view page.
2. Pay attention to the format of xml. The author himself failed to read the data binding due to the problem of Xml data format in the experiment.


Related articles: