Search Asp.Net Here

Make Cache Dependency : How to make Cache Dependent on a File (XML File)



How to Make Cache Dependency (Chache Dependent on a File)

Here is an Example of how to use Data Caching in Asp.Net and Make Cache Dependency
Data Caching is a type of caching provided by Asp.net . Data Caching has three types.

File Dependency – Cache Depends on a file. If content in file changed than cache destroyed
Key Dependency-Cache depends on another cache .
Time Dependency-Cache depends on Time

Here I am giving an example of how to use File Dependency or how to make your cache depend on a file

In my example I am using a xml file on which cache is dependent. I will make changes to this file using Grid View . when you add a record in xml file using this Grid View, the cache will destroy .

I placed a Button on top of the page to refresh page (To check where data is coming from)

Click on this button to check where data is coming from. It will show message on Grid View 2 control

Write the following code in your .aspx page

<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="Check From where Data is coming" />
  
        <br />
        <br />
        <asp:GridView ID="GridView2" runat="server">
        </asp:GridView>
  
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            ShowFooter="True"
            OnRowCommand="GridView1_RowCommand"
            Caption="Make change in file using this form and click on button on Top">
            <FooterStyle BackColor="#CCCCCC" />
            <Columns>
                <asp:TemplateField HeaderText="Eno">
                    <FooterTemplate>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("eno") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <FooterTemplate>
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Eval("ename") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Address">
                    <FooterTemplate>
                        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Eval("eadd") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Salary">
                    <FooterTemplate>
                        <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%# Eval("esal") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Save">
                    <FooterTemplate>
                        <asp:LinkButton ID="LinkButton5" runat="server" CommandName="save">Save</asp:LinkButton>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
            <EmptyDataTemplate>
                No Data Available
            </EmptyDataTemplate>
        </asp:GridView>

Now add the following code inside your page_load event

if (Page.IsPostBack == false)
        {
            bindgrid();//binding grid view 1 with xml file
        }
        String st = Server.MapPath("emp.xml");//file path
        DataSet ds = (DataSet)Cache["emp"];
        if (ds == null)
        {
            ds = get_data_into_cache();
            GridView2.Caption = "Data From Database(File)";
        }
        else
        {
            GridView2.Caption = "Data From Cache";
        }
        GridView2.DataSource = ds;
        GridView2.DataBind();

Now add the following code below (after closing of) your Page_load Event

//Method that will bind grid view 1 with xml file
private void bindgrid()
    {
        DataSet ds = new DataSet();
        String st = Server.MapPath("emp.xml");
        ds.ReadXml(st);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

//method that will insert data into cache
    private DataSet get_data_into_cache()
    {
        DataSet ds = new DataSet();
        String st = Server.MapPath("emp.xml");
        ds.ReadXml(st);
        CacheDependency dep = new CacheDependency(st);//setting dependency on file
        Cache.Insert("emp", ds, dep);//Inserting data into cache
        return ds;
    }
//changing xml file (saving a record)
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "save")
        {
            String st = Server.MapPath("emp.xml");
            DataSet ds = new DataSet();
            ds.ReadXml(st);
            DataRow r = ds.Tables[0].NewRow();
            r[0] = Convert.ToInt32(((TextBox)(GridView1.FooterRow.FindControl("TextBox1"))).Text);
            r[1] = ((TextBox)(GridView1.FooterRow.FindControl("TextBox3"))).Text;
            r[2] = ((TextBox)(GridView1.FooterRow.FindControl("TextBox5"))).Text;
            r[3] = Convert.ToInt32(((TextBox)(GridView1.FooterRow.FindControl("TextBox7"))).Text);
            ds.Tables[0].Rows.Add(r);
            ds.WriteXml(st);
            bindgrid();
        }
    }

//refreshing page
    protected void Button1_Click(object sender, EventArgs e)
    {
    }

Download Source code





Comments :

0 comments to “Make Cache Dependency : How to make Cache Dependent on a File (XML File)”

Post a Comment