2

in my application i trying to implement the cache (ouput) but it is not working fine, that is it is every time getting from cache only this is my code.

<%@ OutputCache VaryByParam ="none" Location="Client" Duration="10" %>.

Code:

protected void btn_Click(object sender, EventArgs e)
    {
        DataView dtv;
        dtv = (DataView)Cache["mycache"];
        if(dtv ==null )
        {
            string sqry="select * from scrap";
            da=new SqlDataAdapter (sqry,con);
            ds=new DataSet();
            da.Fill (ds);
            dtv=new DataView (ds.Tables[0]);
            Cache["mycache"]=dtv ;
            Response.Write ("<script> alert ('from code')</script>");
        }
        else 
        {
             Response.Write ("<script> alert ('from cache')</script>");
        }
        grd1 .DataSource =dtv;
        grd1 .DataBind();
4
  • every time it is coming from cache only. if u get the solution can u suggest the code Commented Mar 11, 2010 at 11:05
  • I'm not sure I understand. Do you mean the page is coming from the Output cache each time or the dtv DataView is being fetched from Cache["mycache"] each time. Commented Mar 11, 2010 at 11:19
  • it is coming from dataview dtv , every time dtv is not null Commented Mar 11, 2010 at 11:30
  • that's because you've not set any expiration time.. see below Commented Mar 11, 2010 at 11:46

2 Answers 2

2

The OutputCache and the Page.Cache are in no way related. The OutputCache caches the html that the page generates and returns that to the browser without running your code again (for 10 seconds as by your current configuration). The Page.Cache provides a mechanism for storing application wide data. Once something is added to that cache it will be there until the next time you restart your website (unless explicitly removed).

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, @klausbyskov is right. Try using the Cache.Insert() method overload with the expiration argument for setting timeout on the data cache elements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.