I am learning cache dependencies recently, and now I have sorted out some learning materials as follows:
Cache dependencies mainly provide the following functions:
Cache dependencies can be used for application cache and page output cache.
2. SQL cache dependencies can be used in SQL Server 7.0 and later.
3. SQL cache dependencies can be used in a network park (multiple processors exist on one server) or in a network farm (multiple servers run the same application).
4. The database associated with SQL cache dependencies is relatively simple to operate, so it does not cause high processing costs to the server.
Now the object set related to cache dependencies is as follows, and the difference is analyzed to a certain extent...
Cache dependencies are mainly implemented by 3 core classes:CacheDependency, AggregateCacheDependency, SqlCacheDependency. The CacheDependency class is the parent class 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. //It can work on files.
The SqlCacheDependency class monitors specific SQL Server database tables on all supported SQL Server versions (7.0, 2000, 2005) so that the items associated with the table are automatically deleted from the cache when the table changes. When a database table changes, cache items are automatically deleted and a new version of items is added to the Cache. The SqlCacheDependency class also supports integration with classes when using SQL Server 2005 databases. Use SQL Server 2005's query notification mechanism to detect data changes that invalidate SQL query results. Any cache entry associated with the SQL query will be removed from it. When using SQL Server 2005, you can use the SqlCacheDependency class to add items that depend on SQL Server database tables or SQL queries to the application's cache. //Support for data tables
The AggregateCacheDependency class monitors a collection of dependency objects so that the cache item is automatically removed when any dependency objects change. Objects in the array can be CacheDependency or SqlCacheDependency objects, custom objects derived from CacheDependency, or any combination of these objects.
The difference between the AggregateCacheDependency class and the CacheDependency class is that the former allows you to associate multiple dependencies of different types with a single cache entry. For example, if you create a page that imports data from SQL Server database tables and XML files, you can create a SqlCacheDependency object to represent dependencies for the database tables, and a CacheDependency to table dependencies for the XML files. Instead of calling a method for each dependency, you can create an instance of the AggregateCacheDependency class, add each dependency to the class. The page can then be made dependent on the AggregateCacheDependency instance using a single Insert call.
Among them, this chapter mainly talks about the usage of CacheDependency.
CacheDependency has several overloads, each of which functions are as follows.
//Suppose the cached source file is the file in the current directory
//Cache dependency file path
CacheDependency mydep = new CacheDependency("");
//The cache dependent files can have multiple files
CacheDependency mydep1=new CacheDependency(new string []{"",""});
//Check the time of cache dependency changes
CacheDependency mydep2 = new CacheDependency("", );
//Check the time by which multiple dependent files are changed
CacheDependency mydep3 = new CacheDependency(new string[] { "", "" }, );
//Check to rely on multiple files, and also rely on multiple cached key values
CacheDependency mydep4 = new CacheDependency(new string[] { "", "" },new string[] { "Category", "Category1" });
//Associated dependencies, it can also depend on another file cache dependency
CacheDependency mydep5 = new CacheDependency(new string[] { "", "" },new string[] { "Category", "Category1" }, mydep);
//The last time the file and key value was modified is based on the time
CacheDependency mydep6 = new CacheDependency(new string[] { "", "" },new string[] { "Category", "Category1" }, );
//The last time the file, another cache dependency and key value was modified
CacheDependency mydep6 = new CacheDependency(new string[] { "", "" },new string[] { "Category", "Category1" }, mydep,);
public partial class CacheDependencyPage :
{
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 this item. If the cache changes, then true; details of the cache object of Baidu C#!
{
DataSet ds = new DataSet();
string filePath = ("~/App_Data/");//Absolute address of server data
(filePath);//Read the data in the file and save it in ds
tableData = [0];
CacheDependency cdy = new CacheDependency(filePath,);
("data", tableData, cdy);//Add items to cache by using the Insert (overload the Insert method) method
//if ()
//{
("Xml has been changed");
//}
}
else
{
tableData = (DataTable)Cache["data"];//Add items to cache by specifying their keys and values
}
= tableData;
();
//DataSet mds = new DataSet();//For verification of data binding, data binding failed due to the problem of the xml hierarchy;
//(("~/App_Data/"));
// = mds;
//();
}
}
in
1. Remember to bind the corresponding data field to gridview on the view page.
2. Pay attention to the format of xml. The author himself should have the problem of Xml data format when experimenting, resulting in the data binding of the read.