views:

76

answers:

2

Is this the right way to initialize a static cache object in a web service?

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

More Info:

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.
A: 

This looks good to me - especially if you are going to wrap the Current.Context and expose properties for cache values like this:

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

    // etc...
}

You don't really need to create a private reference to the current cache unless you are only doing so to save on typing. Also notice that I made the class static as well.

Andrew Hare
A: 

Why not just access it directly using HTTPContext.Current.Cache?

RichardOD
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. By forcing it to be static I was hoping to have only one. However no avail.
ra170
Then there is something wrong with your machine. Are you specifying timeout values for the items in cache? Does your machine have only a small amount of RAM?
RichardOD