日期:2014-05-16  浏览次数:20409 次

asp.net缓存问题

        string key = "test";
        test t1 = new test();
        t1.id = DateTime.Now.Second;

        test t2 = new test();
        t2.id = t1.id;

        string str = Request.PhysicalApplicationPath + @"html/Blog_Home.html";
        //创建缓存依赖项
        test tt = HttpContext.Current.Cache[key] as test;
        if (tt == null)
        {
            CacheDependency dependency = new CacheDependency(str);
            HttpContext.Current.Cache.Insert(key, t1, dependency, Cache.NoAbsoluteExpiration, new TimeSpan(0, 30, 0));
        }
        tt = HttpContext.Current.Cache[key] as test;
        Response.Write(tt.id + "</br>");
        t1.id = DateTime.Now.Second+10;//看这里
        tt = HttpContext.Current.Cache[key] as test;
        Response.Write(tt.id + "</br>");



问题是这样的:我缓存了一个对象t1,但当我修改该对象t1时,缓存里的对象也也跟着改变,如果在Cache.insert方法里把t1改为t2就没问题了,因为后面的语句都没有修改t2这个对象,我想问大家,你们平时用缓存都是这样用的吗?我是想把对象放进缓存里后,不管怎样改变这个对象都不会影响这个缓存项
------解决方案--------------------
两处都引用同一个对象,又不是复制,怎么可能一处修改了t1而另一处没改变呢?