Best way to save data in .NET Web Service

I have a web service that requests data from this json file, but I do not want the web service to access the file every time. I think that maybe I can store the data somewhere else (maybe in memory), so the web service can just get the data from there next time, trying to request the same data. I understand what needs to be done, but I'm just not sure how to do it. How do we store data in a web service?

Update: Both suggestions, caching and using static variables look good. Maybe I just need to use both options so that I can look at them first, and if it is not used there, use the second one, if it is not there either, then I will look at the json file.

+4
source share
4 answers

Continuing the idea of Ice ^^ Heat , you may need to think about where you will cache - or cache the contents of the json file in the application cache like this:

Context.Cache.Insert("foo", _ Foo, _ Nothing, _ DateAdd(DateInterval.Minute, 30, Now()), _ System.Web.Caching.Cache.NoSlidingExpiration) 

And then generate the results you need with every hit. Alternatively, you can cache the output of the webservice in the function definition:

 <WebMethod(CacheDuration:=60)> _ Public Function HelloWorld() As String Return "Hello World" End Function 

Information collected from the Web Services Caching Strategy .

+6
source

How to use a global or static collection object? Is that a good idea?

+3
source

ASP.NET caching also works with web services, so you can implement regular caching as described here: http://msdn.microsoft.com/en-us/library/aa478965.aspx

+2
source

To echo klughing , if your JSON data doesn't change often, I think the easiest way to cache it is to use a static set of some kind - perhaps a DataTable.

First, parse the JSON data in the System.Data.DataTable file and make it static in your web service class. Then refer to the static object. Data must be cached until IIS processes your application pool.

 public class WebServiceClass { private static DataTable _myData = null; public static DataTable MyData { get { if (_myData == null) { _myData = ParseJsonDataReturnDT(); } return _myData; } } [WebMethod] public string GetData() { //... do some stuff with MyData and return a string ... return MyData.Rows[0]["MyColumn"].ToString(); } } 
+2
source

Source: https://habr.com/ru/post/1276339/


All Articles