Practical asp.net cache

Source: Internet
Author: User

If (! IsPostBack) // load for the first time
{
Stopwatch watch = new Stopwatch (); // defines the cache.
Watch. Start ();
If (Cache ["grda"] = null) // if the cache is empty
{
GRDA grda = new GRDA ();
DataTable dt = grda. GetGrda (); // obtain database information

// The easiest way to assign values to the cache
Cache ["grda"] = dt;

// You can set the cache value Assignment Method for the expiration time
Cache. Insert ("grda", dt, null, DateTime. Now. AddSeconds (30), TimeSpan. Zero );
// Create an associated file
CacheDependency file = new CacheDependency ("c: \ 11.txt ");

Cache. Insert ("grda", dt, file, DateTime. Now. AddMinutes (30), TimeSpan. Zero );

// (Cache name, Data, associated file, session time, click to add a ten-minute validity period (generally not used ))

}

This. GridView1.DataSource = (DataTable) Cache ["grda"]; // retrieves data from the Cache
This. GridView1.DataBind ();
Watch. Stop (); // Stop caching
Double totaltime = watch. Elapsed. TotalSeconds;
This. Label1.Text = totaltime. ToString (); // display the expiration time

 

// The above analysis is personal opinion. If there are differences, please add Q472613400 for discussion.



From the liuningshiwoa Column

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.