2

How can I use server side caching on a C# WCF Rest service?

For example, I generate a lot of data into one object (not through database) and I do not want to do that every call a (random) user makes. How can I cache the object.

Verifying question: Is it right that a HttpContext cache object is only between a specific client and the host?

Mario
  • 513
  • 5
  • 12

2 Answers2

2

Is it right that a HttpContext cache object is only between a specific client and the host?

No, it is a shared object, as per msdn

There is one instance of the Cache class per application domain. As a result, the Cache object that is returned by the Cache property is the Cache object for all requests in the application domain.

Depending on the load, you may also use a database for chaching (depending what you call caching). There are also in-memory databases specifically optimised for distributed caching, see memchached, redis and Memcache vs. Redis?

Community
  • 1
  • 1
oleksii
  • 33,544
  • 11
  • 83
  • 149
0

The HttpContext.Cache is local to the Application Domain, and so is shared by all code that runs in that Application Domain. It is certainly fast and flexible enough for most applications.

How you would use it, depends of course on your needs. You may use a serialized version of input parameters as the key, for instance, like in this example:

public MyObject GetMyObject(int size, string cultureId, string extra)
{
    // Input validation first
    ...

    // Determine cache key
    string cacheKey = size.ToString() + cultureId.ToString() + extra.ToString();

    // rest of your code here
}
Roy Dictus
  • 30,703
  • 5
  • 56
  • 69