<p>I have a web service that queries data from this json file, but I don't want the web service to have to access the file every time. I'm thinking that maybe I can store the data somewhere else (maybe in memory) so the web service can just get the data from there the next time it's trying to query the same data. I kinda understand what needs to be done but I'm just not sure how to actually do it. How do we persist data in a web service? </p>

<p><strong>Update:</strong>
Both suggestions, caching and using static variables, look good. Maybe I should just use both so I can look at one first, and if it's not in there, use the second one, if it's not in there either, then I'll look at the json file.</p>

[{'answer_id': 11779, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>ASP.NET caching works just as well with Web services so you can implement regular caching as explained here: <a href="http://msdn.microsoft.com/en-us/library/aa478965.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa478965.aspx</a></p>\n'}, {'answer_id': 11789, 'author': 'Hertanto Lie', 'author_id': 1381, 'author_profile': 'https://Stackoverflow.com/users/1381', 'pm_score': 2, 'selected': False, 'text': '<p>What about using a global or static collection object? Is that a good idea?</p>\n'}, {'answer_id': 11826, 'author': 'Mark Glorie', 'author_id': 952, 'author_profile': 'https://Stackoverflow.com/users/952', 'pm_score': 4, 'selected': True, 'text': '<p>Extending on <a href="https://stackoverflow.com/questions/11761/persisting-data-in-net-web-service-memory#11779">Ice^^Heat</a>\'s idea, you might want to think about where you would cache - either cache the contents of the json file in the Application cache like so:  </p>\n\n<pre><code>Context.Cache.Insert("foo", _\n                 Foo, _\n                 Nothing, _\n                 DateAdd(DateInterval.Minute, 30, Now()), _\n                 System.Web.Caching.Cache.NoSlidingExpiration)\n</code></pre>\n\n<p>And then generate the results you need from that on every hit. Alternatively you can cache the webservice output on the function definition:  </p>\n\n<pre><code>&lt;WebMethod(CacheDuration:=60)&gt; _\nPublic Function HelloWorld() As String\n    Return "Hello World"\nEnd Function\n</code></pre>\n\n<p>Info gathered from <a href="http://msdn.microsoft.com/en-us/library/aa480499.aspx" rel="nofollow noreferrer">XML Web Service Caching Strategies</a>.</p>\n'}, {'answer_id': 11904, 'author': 'Seibar', 'author_id': 357, 'author_profile': 'https://Stackoverflow.com/users/357', 'pm_score': 2, 'selected': False, 'text': '<p>To echo <a href="https://stackoverflow.com/questions/11761/persisting-data-in-net-web-service-memory#11789">klughing</a>, if your JSON data isn\'t expected to change often, I think the simplest way to cache it is to use a static collection of some kind - perhaps a DataTable.</p>\n\n<p>First, parse your JSON data into a System.Data.DataTable, and make it static in your Web service class. Then, access the static object. The data should stay cached until IIS recycles your application pool.</p>\n\n<pre><code>public class WebServiceClass\n{\n    private static DataTable _myData = null;\n    public static DataTable MyData\n    {\n        get\n        {\n            if (_myData == null)\n            {\n                _myData = ParseJsonDataReturnDT();\n            }\n            return _myData;\n        }\n    }\n\n    [WebMethod]\n    public string GetData()\n    {\n        //... do some stuff with MyData and return a string ...\n        return MyData.Rows[0]["MyColumn"].ToString();\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/11761', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1381/']