Data Caching


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WACashingTester._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:Timer ID="Timer1" runat="server" Interval="1000">
        </asp:Timer>
    </div>
    </form>
</body>
</html>

*****************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WACashingTester
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = DateTime.Now.ToLongTimeString() + "<br/><br/>";
            List<REGIONS> datas = null;
            if (Cache["datas"] != null)
            {
                datas = Cache["datas"] as List<REGIONS>;
                Response.Write("Data comes from <strong>CACHE</strong>...");
            }
            else
            {
                datas = getData();
                Cache.Insert("datas", datas, null, DateTime.Now.AddSeconds(5), System.Web.Caching.Cache.NoSlidingExpiration);
                Response.Write("Data comes from <strong>SOURCE</strong>...");
            }
            this.GridView1.DataSource = datas;
            this.GridView1.DataBind();
        }

        public static List<REGIONS> getData()
        {
            //EntityFramework is used in this project.
            Entities db = new Entities();
            List<REGIONS> regions = db.REGIONS.OrderBy(f => f.REGION_ID).ToList();

            return regions;
        }
    }

*****************************************************


Comments

Popular posts from this blog

Solution for the fault: "System.Diagnostics.Process.Start is not working on IIS, but working on ASP.NET Development Server"

Solution for "Can not obtain Oracle client information from registry"