Web服务中的静态缓存

时间:2009-06-08 14:38:29

标签: web-services caching

这是在Web服务中初始化静态缓存对象的正确方法吗?

public class someclass{
 private static Cache cache;
 static someclass()
    {
        cache = HttpContext.Current.Cache;
    }
}

更多信息:

Seems like I receive more then one cache object from webservice. It creates a new request that only lasts for the duration of that call. If I move to a different machine, it creates a new request (and I think a webservice ) object that returns new cache. (because I can see two different caches being returned in the sniffer) By forcing it to be static I was hoping to have only one. However no avail. doesn't work.

2 个答案:

答案 0 :(得分:0)

这对我来说很好 - 特别是如果你要打包Current.Context并公开缓存值的属性,如下所示:

public static class CacheManager
{
    public static Boolean Foo
    {
        get { return (Boolean)HttpContext.Current.Cache["Foo"] }
        set { HttpContext.Current.Cache["Foo"] = value; }
    }

    // etc...
}

您实际上不需要创建对当前缓存的私有引用,除非您只是为了节省输入而保存。另请注意,我也创建了课程static

答案 1 :(得分:0)

为什么不直接使用HTTPContext.Current.Cache

来访问它