The cache in ASP.net 2.0 provides support for SQL dependencies, meaning that when the data in a table or row in a SQL Server database is changed, the page in the cache is invalidated, otherwise the page output remains in the cache. This is really handy for programmers. But Microsoft has always been very petty, only for programmers who use their own products SQL Server, what about ASP.net programmers who use Oracle databases?
In fact, there is no need to worry, because the cache in ASP.net 2.0 also provides support for file dependencies, that is, the cache relies on a file that is modified to invalidate the pages in the cache. It is easy to implement a caching strategy that relies on Oracle as long as the ASP.net 2.0 file dependency caching policy and the triggers in Oracle are cleverly exploited. The idea is simply to set the caching policy of the page to rely on a single file, and then add a trigger to the tables in Oracle that need to be relied upon, and modify the contents of the file on which the cache depends when the data in the table is changed.
Here is a small example to specify:
Test purpose: The caching of Default.aspx pages relies on the Dept table of Scott users in the Oracle database, where the data in the table is changed and the pages in the cache are invalidated. The cache expires at 120 seconds.
I. Setting the caching of Web pages depends on the file TextFile.txt
1. Open Visual Studio 2005 and create a new Web project in the E:\CSharp\CacheByOracleDependncy directory. Add a Label control on its Default.aspx page to show when the page was generated to determine if the page was regenerated when it was refreshed, and to set the page cache to depend on the file E:\CSharp\CacheByOracleDependncy\ TextFile.txt.
protected void Page_Load(object sender, EventArgs e)
{
//显示当前时间,以便判断是否为缓存中页面
this.Label1.Text = "CacheByOracleDependency:" + DateTime.Now.ToString();
//缓存依赖于文件TextFile.txt
string fileDependencyPath = Server.MapPath("TextFile.txt");
Response.AddFileDependency(fileDependencyPath);
// 设置缓存的过期时间为120秒。
Response.Cache.SetExpires(DateTime.Now.AddSeconds(120));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
}
2. Create a new TextFile.txt file in the E:\CSharp\CacheByOracleDependncy directory.
Two. Creating triggers in the Oracle database
1. Execute PL/SQL code block when trigger is triggered. Pl/sql code blocks directly read and write files in the operating system, you need to call the built-in Utl_file package. This requires you to first modify the Oracle initialization parameter file Init.ora, where you can add parameter Utl_file_dir to specify the directory for the file. After you modify the Init.ora file, you need to restart the Oracle database for the parameters set to take effect.
Add the following line to the Init.ora file:
Utl_file_dir= ' E:\CSharp\CacheByOracleDependncy '
can also be set to utl_file_dir=*, without specifying a specific directory, that is, any directory can be.
In the case of Oracle 9i databases, there is also a way to do the same: Create a directory under the SYS user (in effect adding a corresponding os_path to the dir$ table under the SYS user), and then read/ Permission granted to the write operation is given to public.
Create or replace directory FILEPATH as ' E:\CSharp\CacheByOracleDependncy ';
Grant Read on directory FILEPATH to public;
Here I use the second method.