This article summarizes the usage methods of the database dependency class SqlCacheDependency in C# cache. The specific content is as follows:
1. Database dependency class SqlCacheDependency
Database cache dependencies mainly solve the problem of how to promptly notify the cache and update the data in the cache when the content of the database changes.
Syntax definition:
The main constructor of the SqlCacheDependency class is as follows:
public SqlCacheDependency(string database,string table)
Parameter 1 represents the database to enable cache, and parameter 2 represents the cached table. In actual use, you only need to specify the cached database and tables.
The method is an application of attributes (the code is similar to CacheDependency), but Sql needs to configure and configure the database cache first before using the SqlCacheDependency cache class
First, the configuration is as follows:
<!--Connect to database statement--> <configuration> <connectionStrings> <add name="Config" connectionString="Data Source=.;Initial Catalog=CacheData;Persist Security Info=True;User ID=sa;Password=123" providerName=""/> </connectionStrings> <!--Add under node--> <!--Things to note:In configurationadd nameThe value is the database name,connectionStringNameThe name of the field to connect to the database must be the same--> <caching> <sqlCacheDependency enabled="true" pollTime="1000"> <databases> <add name="CacheData" connectionStringName="Config" pollTime="1000"/> </databases> </sqlCacheDependency> </caching>
2. Vs cache configuration:
Open the "Start" | "All Programs" | "Microsoft Visual Studio 2010" | "Visual Studio Tools" | "Visual Studio 2010 Naming Tips" menu command.
Enter: aspnet_regsql.exe -S SqlServer Server -U <Username> -P <Password> -ed -d Database Name -et -t Table Name
If there is no authentication input: aspnet_regsql.exe -S SqlServer Server -ed -d Database Name -et -t Table Name
Just execute the command;
3. Page code;
private static SqlCacheDependency MyDep; protected void Page_Load(object sender, EventArgs e) { = (); if (!IsPostBack) { //Cache is the database name, T_SqlCache is the cache table DataSet ds = GetSet(); if (Cache["SqlCon"] == null) { //Add cache SqlCon, the cache value is the content of the database table. MyDep = new SqlCacheDependency("Cache", "T_SqlCache"); ("SqlCon", ds, MyDep, (60), , , null); } } } protected void Button1_Click(object sender, EventArgs e) { if () {//Remind when the database value changes; ("The database modification time is:"+); } if (Cache["SqlCon"] == null) {// When the cache expires or the database value is modified, the cache is loaded from the new load MyDep = new SqlCacheDependency("Ajax", "T_AjaxLD"); DataSet ds = GetSet(); ("SqlCon", ds, MyDep, (60), , , null); } this. = Cache["SqlCon"];//Bind data this.(); } /// <summary> /// Generate Dataset /// </summary> /// <returns></returns> private DataSet GetSet() { DataSet ds = new DataSet(); string sql = "select * from T_SqlCache"; string Config = ["Config"].ConnectionString;//Connect database statement using (SqlConnection cnn = new SqlConnection(Config)) { using (SqlCommand cmm = new SqlCommand(sql, cnn)) { SqlDataAdapter dapter = new SqlDataAdapter(cmm); (ds); } } return ds; }
The basic content of C# cache is almost the same. Some applications need to be summarized in practice. Here we will analyze the difference between session and cache:
The difference between Session and Cache:
In the past, there were many ways to implement data caching, including client cookies, server-side sessions and application. Among them, cookies are a set of data stored on the client, which is mainly used to save usernames and other personal information. Session saves conversation information. Application is information stored in the entire application scope, equivalent to a global variable. The most frequently used session is usually the Session, so what is the difference between Session and Cache?
This section combines experience with the use of the user and introduces the difference between Session cache and Cache cache in detail as follows:
(1) The biggest difference is that Cache provides cache dependencies to update data, while Session can only rely on the defined cache time to determine whether the cached data is valid.
(2) Even if the application terminates, as long as the cache time defined in the method has not expired, the cached data will still exist the next time the application is opened. The Session cache only exists in a session, and after the session ends, the data will be invalid.
(3) Session is easily lost, resulting in data uncertainty, and this will not happen to Cache.
(4) Since the Session is loaded every time the session, it is not suitable to store a large amount of information, otherwise it will lead to a degradation of the server's performance. Cache is mainly used to store large-capacity information, such as multiple tables in the database.
(5) The beta version of VS2005 provides parameters for saving cache on the hard disk, but this function has been cancelled in the official version, and it is estimated that it will be re-implemented in a future version. The Session can only be stored in memory at present, which has an impact on its performance.
In addition, special attention should be paid to:In order to improve the effective utilization of Cache, it is recommended to use Cache for data that are not frequently modified.