c# - 在 asp.net 中缓存(输出)?

标签 c# asp.net caching

在我的应用程序中,我尝试实现缓存(输出),但它无法正常工作, 也就是说,每次从缓存获取时,这都是我的代码。

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

代码:

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();

最佳答案

OutputCachePage.Cache 没有任何关系。 OutputCache 缓存页面生成的 html 并将其返回到浏览器,而无需再次运行代码(根据当前配置,持续 10 秒)。 Page.Cache提供了一种存储应用程序范围数据的机制。一旦将某些内容添加到该缓存中,它就会一直存在,直到您下次重新启动网站为止(除非明确删除)。

关于c# - 在 asp.net 中缓存(输出)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2424340/

相关文章:

c# - 列出带有实例的可用 SQL Server 不包括 SQL Server Express

java - 将序列化和压缩的对象保留在内存中

c# - 在 C# 中验证 Amazon SNS 签名

c# - C#解析网页有用内容

c# - 如何从 Silverlight 以编程方式调用 Web 服务?

asp.net - 无法找到此查询在 asp.net 中无法正常工作的原因

c# - 如果使用验证器在 asp.net 中验证失败,则更改文本框的 CssClass

c# - 如何将参数传递给ajax调用

php - Twitter - twemproxy - memcached - 重试未按预期工作

HTTP:很少更改的图像的最佳 header 是什么