Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand ASP.NET database cache dependency

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand ASP.NET database cache dependency". In daily operation, I believe many people have doubts about how to understand ASP.NET database cache dependency. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to understand ASP.NET database cache dependency". Next, please follow the editor to study!

The details are as follows:

Generally speaking, in ASP.NET, the coolest feature of Cache class is that it can control its behavior according to various dependencies. File-based dependencies are the most useful, and file dependencies are added by using Cache.Insert and providing CacheDependency objects that reference files

The copy code is as follows:

Cache.Insert ("MyData", Source, new CacheDependency (Server.MapPath ("authors.xml"))

But what should we do when we want the cache to invalidate and rebuild the cache based on changes in the tables in the database-a scenario that exists in many applications. Asp.net does not provide built-in direct caching support for monitoring changes in database tables. This can be achieved by using SQL Server's less commonly used system stored procedure sp_makewebtask, which is originally used to generate web pages from a query, but with a little modification-using it in a trigger, we can obtain a reasonable and effective way to modify a particular file when the record of a table in the database is updated, deleted, or modified. This causes the file monitoring process in the CacheDependency instance to detect changes to the file, thus invalidating the cache. In fact, because the CacheDependency class works on the UNC file protocol, we can deploy this solution on the entire WebFarm, and the copy of the application on each machine on the WebFarm monitors the same file on a single machine in the WebFarm through the UNC file path.

Cut the crap, let's create a simple web application to demonstrate how it works. First, we will use the Northwind sample database that we all trust in SQL Server. Create a simple DataGrid to display the records in the Employees table. The first thing we need to do is create a trigger.

The copy code is as follows:

CREATE TRIGGER WriteCacheDepFile ON [dbo]. [Employees]

FOR INSERT, UPDATE, DELETE

AS

EXEC sp_makewebtask'\ peter\ C$\ Cache\ mycache.txt', 'SELECT top 1 FirstName FROM employees'

The above stored procedure simply tells SQL Server that if there is any change in the Employee table, update the "mycache.txt" file according to a simple query. This simple query statement is actually sufficient. As long as it is a valid T-SQL statement, SQL Server will gladly update that file.

Next, we need to create a directory and set it as a share. You may want to update the access to the file so that it can be written. Note that I am using administrator share "C $" here. In addition, you need to create an empty text file, mycache.txt.

OK, now we can create our application. First, enter the name of the dependency file in the web.config file, which eliminates the need to redeploy the application when we modify the dependency file.

At the root of the web.config file, add the appSettings configuration section:

The copy code is as follows:

Now, let's set up a caching mechanism in the Global class so that we don't need to write specific code in any page

The copy code is as follows:

Public class Global: System.Web.HttpApplication

{

Cache _ cache = null

Public static bool blnReflash = false

Public const string ConnStr = "server=localhost;database=Northwind;uid=sa;pwd="

Public const string strSQL = "SELECT EmployeeID, lastname, firstname FROM Employees"

Protected void Application_Start (Object sender, EventArgs e)

{

_ cache = Context.Cache

RefreshCahe (null,null,0)

}

Protected void Session_Start (Object sender, EventArgs e)

{

If (HttpContext.Current.Cache ["Employees"] = = null)

RefreshCache (null,null,0)

}

Static void RefreshCache (string key,object item,CacheItemRemoveReason reason)

{

SqlDataAdapter adapter = new SqlDataAdapter (strSQL,ConnStr)

DataSet ds = new DataSet ()

Adapter.Fill (ds, "Employees")

CacheItemRemovedCallback onRemove = new CacheItemRemovedCallback (RefreshCache)

}

At this point, the study on "how to understand ASP.NET database cache dependency" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report