Marek Kwiendacz
Marek Kwiendacz

Reputation: 9834

Is it possible to use Cache in webservice?

In my webapplication I have some precalculated values that are stored in Page.Cache. At his moment I have to move this functionality to webservice.

So I need to introduce similiar functionality in webservice - calculate values on first call to web method and store it in Cache.
Is it possible to put some values in Cache (not in Application or Session - I need to purge those values after some time interval) from webservice?

Upvotes: 6

Views: 15248

Answers (4)

Amit Rai Sharma
Amit Rai Sharma

Reputation: 4225

Refer XML Web Service Caching Strategies

It seems to be a bit old but good article. I hope you will be able to find what you are looking for.

Upvotes: 1

VinayC
VinayC

Reputation: 49195

In short - yes, you can use caching in web services. You have to choose the correct cache implementation based on service implementation. For example, if you are using ASP.NET web services then you can probably use ASP.NET Cache - available via HttpContext.Current.Cache.

If you are using WCF web services then you may need to use other libraries (BTW, ASP.NET web services are obsolete, so I will suggest WCF web services anyway). If WCF services are marked as ASP.NET compatible then ASP.NET infrastructure and its cache will be available. But you have choice to host WCF services w/o ASP.NET integration and in such case, you probably need to look at different caching API - for example, System.Runtime.Caching if you are using .NET 4 (see a quick tutorial for using in-memory cache) otherwise you may try Caching Application Block.

Upvotes: 6

Hans Kesting
Hans Kesting

Reputation: 39283

You can always access the Cache through the static HttpRuntime.Cache property, even if you don't have an HttpContext at the moment.

Upvotes: 8

MatteKarla
MatteKarla

Reputation: 2737

You should be able to use:

HttpContext.Current.Cache.Insert(strName, objItem, null,
     DateTime.Now.AddMinutes(intMinutes), Cache.NoSlidingExpiration)

to cache item for a specified time interval.

Upvotes: 2

Related Questions