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 i开发者_运维技巧n Application or Session - I need to purge those values after some time interval) from webservice?You can always access the Cache through the static HttpRuntime.Cache property, even if you don't have an HttpContext at the moment.
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.
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.
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.
精彩评论