惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
博客园_首页
H
Help Net Security
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
B
Blog
I
InfoQ
SecWiki News
SecWiki News
T
Tailwind CSS Blog
Spread Privacy
Spread Privacy
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
P
Palo Alto Networks Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
M
MIT News - Artificial intelligence
S
Schneier on Security
T
Threat Research - Cisco Blogs
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The Cloudflare Blog
Recent Announcements
Recent Announcements
Security Latest
Security Latest
G
GRAHAM CLULEY
IT之家
IT之家
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
腾讯CDC
Google DeepMind News
Google DeepMind News
V
V2EX
S
Securelist
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
S
SegmentFault 最新的问题
博客园 - 叶小钗
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Project Zero
Project Zero
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure

博客园 - o0myself0o

利用线程池实现多客户端和单服务器端Socket通讯(二):异步编程模型实现 生产者消费者模式,代码中碰到的疑问(已解决) 利用线程池实现多客户端和单服务器端Socket通讯(一):同步方式 Entity Framework 4.0 ObjectContext下的各种方法实践 题目:若干个不重复数,打乱顺序输出 wtf js(四) - o0myself0o - 博客园 wtf js(三) number的类型不是number wtf js(二) 算法:给定两个已从小到大排好序的整型数组arrA和arrB,将两个数组合并成arrC,使得arrC也要按从小到大的顺序排好序 应用中的单例模式 面试题:给你三个bool类型变量a, b, c,判断至少有两个为true javascript面向对象编程(一) - o0myself0o - 博客园 wtf js(一) - o0myself0o - 博客园 社区网站功能实现系列(三):社区页面无刷新回发的一种实现方式 社区网站功能实现系列(二):社区内容分享到别的SNS 社区网站功能实现系列(一):多国语言的实现 反射获取Class中Property的值 A*寻路初探 使用 jQuery 简化 Ajax 开发
闲谈ASP.NET 2.0缓存技术
o0myself0o · 2008-06-25 · via 博客园 - o0myself0o

    这一个学期一直都生活在忙忙碌碌、犹犹豫豫之中,也一直没有什么心情写一些技术性的东西。这两天刚从考研的囫囵里跳出来,顿感轻松了许多。人也活力了很多。遂决定时不时的写点东西。今天刚好把袁阔成的评三国下下来了,一边听着三国,一边写。今天想闲谈一下ASP.NET 2.0缓存技术。
    1.在.aspx页面加<%@ OutputCache Duration="60" VaryByParam="none" %>  Duration为缓存时间,VaryByParam为QueryString,none表示地址没有参数。
    ......
    <%@ OutputCache Duration="60" VaryByParam="none" %>
    ......
    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        lblTime.Text = DateTime.Now.ToString();
    }
    </script>
    ......
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="lblTime" Text="" runat="server"></asp:Label>
    </div>
    </form>
    2.同1,只是VaryByParam值不为none,可以任设,比如state。例如:链接<a href="default.aspx?state=...">链接</a>,state的值做为一个GridView数据源的参数,为了能看出效果,最好加一个lable放时间,点链接之后,选出了一部分数据,这就是对一个页面不同的参数的分别缓存效果。
    ......
    <%@ OutputCache Duration="60" VaryByParam="state" %>
    ......
    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.QueryString["state"]==null)
            lblTime.Text = DateTime.Now.ToString();
        else if(Request.QueryString["state"].ToString()=="1")
            lblTime1.Text = DateTime.Now.ToString();
        else if(Request.QueryString["state"].ToString()=="2")
            lblTime2.Text = DateTime.Now.ToString();
    }
    </script>
    ......
    <form id="form1" runat="server">
    <div>
        <a href="OutputCacheWithParam.aspx">刷新</a><br />
        <a href="OutputCacheWithParam.aspx?state=1">刷新1</a><br />
        <a href="OutputCacheWithParam.aspx?state=2">刷新2</a><br />
        <asp:Label ID="lblTime" ForeColor="red" Text="" runat="server"></asp:Label><br />
        <asp:Label ID="lblTime1" ForeColor="green" Text="" runat="server"></asp:Label><br />
        <asp:Label ID="lblTime2" ForeColor="blue" Text="" runat="server"></asp:Label><br />
    </div>
    </form>
    3.上面两种方法的缺点就是页面的数据不能及时更新。有的时候要给用户显示一些很重要的数据,这样的方式就不能满足了,下面的部分缓存可以解决这以问题。
    ......
    <%@ OutputCache Duration="60" VaryByParam="none" %>
    ......
    <script runat="server">
    static string GetCurentTime(HttpContext context)
    {
        return DateTime.Now.ToString();
    }
    </script>
    ......
    <form id="form1" runat="server">
    <div>
    缓存时间:
    <asp:Label ID="lblTime" ForeColor="red" Text="" runat="server"><%=DateTime.Now.ToString() %></asp:Label><br /><br />
    现在时间:
    <asp:Substitution ID="Substitution1" runat="server" MethodName="GetCurentTime" />
    </div>
    </form>
    
    这里有一个方法静态GetCurentTime,返回值作为动态数据传给Substitution,这样,第一个时间在缓存期内不改变,第二个时间不受缓存影响。
    4.用API
    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));//相当于duration
        Response.Cache.SetCacheability(HttpCacheability.Public);//相当于VaryByParam
        lblTime.Text = DateTime.Now.ToString();
    }
    </script>
    ......
    5.利用Page.Cache缓存数据
    ......
    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        DataView source = (DataView)Cache["UsersCache"];

        if (source == null)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["UsersConnectionString"].ConnectionString);
            SqlDataAdapter adp = new SqlDataAdapter("select * from Users", conn);
            DataSet ds = new DataSet();
            adp.Fill(ds, "UsersTable");

            source = new DataView(ds.Tables["UsersTable"]);
            Cache["UsersCache"] = source;
        }
        this.GridView1.DataSource = source;
        this.GridView1.DataBind();
    }
    </script>
    ......
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div>
    </form>

    6.实现用户控件缓存,但页面其他地方不缓冲,也是实现部分缓存
    --WebUserControl.ascx部分代码
    ......
    <%@ OutputCache Duration="60" VaryByParam="none" %>
    <%--这里可以是很大的数据库数据,所以缓存非常有必要--%>
    这是用户控件中的内容:<br />
    <asp:Label runat="server" ID="lblTime1" ForeColor="red"><%=DateTime.Now.ToString() %></asp:Label>

    --PageFragmentCaching.aspx部分代码
    ......
    <%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>
    .......
    <form id="form1" runat="server">
    <div>
        <uc1:WebUserControl ID="WebUserControl1" runat="server" />
        <hr />
        这里是主文件的内容:<br />
        <asp:Label runat="server" ID="lblTime" ForeColor="green"><%=DateTime.Now.ToString() %></asp:Label>
    </div>
    </form>

    今天就写到这里了。唉,不写还好,一些发现了太多的问题,文笔不好不说,写出来的代码估计都是有问题的。看来以后不管多紧张,还是要坚持写博啊。