标题:一周都没搞明白的Cache,只能请教各位DX了
只看楼主
whoisliang
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-10-31
结帖率:0
已结贴  问题点数:20 回复次数:2 
一周都没搞明白的Cache,只能请教各位DX了
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Caching" %>
<script runat="server">
   static CacheItemRemovedCallback oncallback=null;
   static CacheDependency dep =null;
    protected static void callback(string s, object o, CacheItemRemovedReason r)
    {
        if (r == CacheItemRemovedReason.DependencyChanged)
        {
            if (HttpRuntime.Cache[s] == null)
            {
                object b = HttpRuntime.Cache["abc"];
                HttpRuntime.Cache.Insert(s, "DependencyChanged", null, DateTime.Now.AddSeconds(55), Cache.NoSlidingExpiration, CacheItemPriority.Normal, oncallback);
                b = HttpRuntime.Cache["abc"];
                return;
            }
        }
        else
        {
            if (r == CacheItemRemovedReason.Expired)
            {
                HttpRuntime.Cache.Insert(s, "Expired",dep , DateTime.Now.AddSeconds(15), Cache.NoSlidingExpiration, CacheItemPriority.Normal, oncallback);
            }
        }  
    }
    void Application_Start(object sender, EventArgs e)
    {
        // 在应用程序启动时运行的代码
        oncallback += new CacheItemRemovedCallback(callback);
        oncallback1 += new CacheItemRemovedCallback(callback1);
        dep = new CacheDependency(Server.MapPath("~/") + "abc.txt");      
        HttpRuntime.Cache.Insert("abc", "123", dep, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, oncallback);
    }
</script>

说明:
1)根目录下有一abc.txt;
2)另有一default.aspx,后台代码很简单:
    if(Cache["abc"]!=null
        Response.Write(Cache["abc"].ToString());
3)我的程序原本目的是:
    a)启动后让CACHE依赖abc.txt,当我人为改了abc.txt内容之后,就会进入if (r == CacheItemRemovedReason.DependencyChanged)中执行;
    b)这样就会重新安装一个绝对定位时间55秒后过期的Cache;
    c)当再次过期到来时,就会进入if (r == CacheItemRemovedReason.Expired)
中执行,也就是说重新安装一个依赖abc.txt的Cache.

实验发现结果完全和我想像的不一样???????????? object b总是null????

如果:
    protected static void callback(string s, object o, CacheItemRemovedReason r)
    {
        if (r == CacheItemRemovedReason.DependencyChanged)
        {
            if (HttpRuntime.Cache[s] == null)
            {
                object b = HttpRuntime.Cache["abc"];
                HttpRuntime.Cache.Insert(s, "DependencyChanged", dep, DateTime.Now.AddSeconds(55), Cache.NoSlidingExpiration, CacheItemPriority.Normal, oncallback);
                b = HttpRuntime.Cache["abc"];
                return;
              }
        }
    }
则会出现椎栈溢出异常,真是搞不明白????????
请教各位大牛,我在此叩首了!只有20分,全送了.
搜索更多相关主题的帖子: void server null 
2012-03-27 23:43
wxm198427
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:3
帖 子:561
专家分:25
注 册:2008-6-30
得分:20 
回复 楼主 whoisliang
大哥,你做这个有什么用?你做这个的想法是怎么样的?
2012-03-29 16:27
whoisliang
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-10-31
得分:0 
回复 2楼 wxm198427
学习呀.怎么就没有高手给我解惑呢?
2012-03-30 20:47



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-364528-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.308791 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved