locked
keeping information in memory that any user can access easily? RRS feed

  • Question

  • User1322403867 posted

    Hi,

    I want to read some information from an xml file and store it ready in memory so that it can go out with every page response without being reloaded from the xml on every request.

     

    What is the best way of doing this?

    It looks to me like there are 2 options. Either create a class with static fields that the information is stored in. Or create a class with public fields that the info is stored in and create an instance of that class at application level.

    Is that correct, are both of those options viable? or will I not be able to access the fields in the second method without creating an instance of the class in the code that responds to the page request(basically if you create an instance of a class at application level can you use that instance from within code anywhere in your website as long as it is public)?

    Do I have any other options?

    What are the pros and cons of each technique?

    Thanks,

    Duncan

    Saturday, March 20, 2010 12:21 PM

Answers

  • User541108374 posted

    Hi,

    why don't you load it into the Cache object and when you need it retrieve it from there? Load it in Cache at startup or the first time you need it.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 20, 2010 1:16 PM
  • User187056398 posted

    I think you need to use the Cache object.  Here's the simplest example:

        private String GetFileData()
        {
            const String Key = "My_File";
    
            // see if the file  is in the cache.
            String Data = Page.Cache[Key] as String;
    
            if (Data != null)
                return Data;
    
            Data = File.ReadAllText(@"C:\Test.txt");
    
            // put it in the cache so we don't have to read the disk again           
            Page.Cache.Insert(Key, Data);
    
            return Data;
        }
    


     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 20, 2010 1:19 PM
  • User-525215917 posted

    Two options:

    • Class with static members or new static members in global application class. It is not very good solution although everything is strongly typed etc. You have to manage concurrency issues by yourself.
    • Class for your data that is kept in cache. If you don't need changes to your data then you can keep it in Cache. Just create class that fits your needs, instantiate it and write it to cache.

      // Write to cache
      public void CreateInstance()
      {
          var cls = new MyClass();
          cls.LoadDataFromFile();
          Cache["MyData"] = cls;    
          return cls;
      }
      
      
      // Read from cache
      var cls = Cache["MyData"] as MyClass;
      if(cls == null)
          cls = CreateInstance();
      
      // Use cls here

    Whatever you do I strongly recommend to test your code for concurrency issues so you can be sure that everything works fine when multiple users are visiting your page at same time.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 21, 2010 5:01 AM
  • User-37275327 posted

    It is better to use cache object. storing in cache shares everypage. But too big object can reduce the performance of the web application.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 23, 2010 8:38 AM
  • User-37275327 posted

    Hope this would help

    http://www.codeproject.com/KB/web-cache/singleLineCacheManager.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 23, 2010 9:24 PM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 23, 2010 9:31 PM

All replies

  • User541108374 posted

    Hi,

    why don't you load it into the Cache object and when you need it retrieve it from there? Load it in Cache at startup or the first time you need it.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 20, 2010 1:16 PM
  • User187056398 posted

    I think you need to use the Cache object.  Here's the simplest example:

        private String GetFileData()
        {
            const String Key = "My_File";
    
            // see if the file  is in the cache.
            String Data = Page.Cache[Key] as String;
    
            if (Data != null)
                return Data;
    
            Data = File.ReadAllText(@"C:\Test.txt");
    
            // put it in the cache so we don't have to read the disk again           
            Page.Cache.Insert(Key, Data);
    
            return Data;
        }
    


     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 20, 2010 1:19 PM
  • User-525215917 posted

    Two options:

    • Class with static members or new static members in global application class. It is not very good solution although everything is strongly typed etc. You have to manage concurrency issues by yourself.
    • Class for your data that is kept in cache. If you don't need changes to your data then you can keep it in Cache. Just create class that fits your needs, instantiate it and write it to cache.

      // Write to cache
      public void CreateInstance()
      {
          var cls = new MyClass();
          cls.LoadDataFromFile();
          Cache["MyData"] = cls;    
          return cls;
      }
      
      
      // Read from cache
      var cls = Cache["MyData"] as MyClass;
      if(cls == null)
          cls = CreateInstance();
      
      // Use cls here

    Whatever you do I strongly recommend to test your code for concurrency issues so you can be sure that everything works fine when multiple users are visiting your page at same time.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 21, 2010 5:01 AM
  • User-37275327 posted

    It is better to use cache object. storing in cache shares everypage. But too big object can reduce the performance of the web application.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 23, 2010 8:38 AM
  • User1322403867 posted

    Thank you all for your replies!

    I have heard of storing things in cache but I thought I had to make the cache myself I didn't realise there was a dedicated cashe object.

    Is there some documentation explaining cache? i have had a look in the msdn library but I can't find it. If you know where to find the documentation and could give me a link that would be very useful.

     

    Thanks, 

    Tuesday, March 23, 2010 6:08 PM
  • User-37275327 posted

    Hope this would help

    http://www.codeproject.com/KB/web-cache/singleLineCacheManager.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 23, 2010 9:24 PM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 23, 2010 9:31 PM
  • User541108374 posted

    Hi,

    some interesting video on how to store information in the Cache object can be found here: [How Do I]: Use the ASP.NET Cache Object to Cache Application Information.

    Grz, Kris. 

    Wednesday, March 24, 2010 9:01 AM