ASP.net 2.0 implements a caching policy that relies on Oracle

Source: Internet
Author: User
Tags current time datetime end oracle database visual studio
asp.net|oracle| Policies | Caching in Cache asp.net 2.0 provides support for SQL dependencies, meaning that when data in a table or row in a SQL Server database is changed, the page in the cache is invalidated, otherwise the page output can remain 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)
{
Displays the current time to determine if the page is in the cache
This. Label1.Text = "Cachebyoracledependency:" + DateTime.Now.ToString ();
Cache dependent on file TextFile.txt
String filedependencypath = Server.MapPath ("TextFile.txt");
Response.addfiledependency (Filedependencypath);
Set the cache expiration time to 120 seconds.
Response.Cache.SetExpires (DateTime.Now.AddSeconds (120));
Response.Cache.SetCacheability (Httpcacheability.public);
Response.Cache.SetValidUntilExpires (TRUE);
}
2, in the E:\CSharp\CacheByOracleDependncy directory to create a new TextFile.txt file.

   second, create triggers in an 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.

2. Create a trigger for the dependent table (Scott User's Dept table): When the data in the Dept table changes, the trigger writes the current system time to the TextFile.txt file.

CREATE OR REPLACE TRIGGER
"SCOTT". " Test_cache_by_oracle_dependncy "After
INSERT
OR UPDATE
OR DELETE of "DEPTNO", "Dname", "LOC" on "SCOTT". " DEPT "DECLARE
File_handle Utl_file.file_type;
BEGIN
--Open File
File_handle: = Utl_file.fopen (' FILEPATH ', ' TextFile.txt ', ' W ');
--Writes the current system time to the file
IF Utl_file.is_open (File_handle) THEN
Utl_file.put_line (File_handle,to_char (sysdate, ' Yyyy-mm-dd hh24:mi:ss '));
End IF;
--Close the file
Utl_file.fclose (File_handle);
EXCEPTION
When others THEN
BEGIN
IF Utl_file.is_open (File_handle) THEN
Utl_file.fclose (File_handle);
End IF;
EXCEPTION
When others THEN
NULL;
End;
End;
   third, testing

Once the previous two steps are complete, the caching policies that rely on Oracle are set up. As soon as the data in the Dept table changes, the trigger modifies the contents of the TextFile.txt file, TextFile.txt the file is modified, the page in the cache is automatically invalidated, and when the page is accessed again, the request is sent to the server to regenerate the page.

When you debug a program in Visual Studio 2005, the Open Default.aspx page is constantly refreshed, and the page appears every 120 seconds before it changes once. This is because the cache expiration time is set to 120 seconds. At this point, as soon as we manually modify the data in the Scott User's Dept table, the time displayed on the page changes as soon as the page is refreshed again. This means that the caching strategy we have set up to rely on Oracle is successful.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.