saXen saXen - 1 year ago 61
C# Question

Prevent reading data from file for each pageview

I run a C# .net site where I store four variables in "file.txt". Reason for storing in a .txt fil is the need of easy access to do changes.

The variables are used for every page the user get presented for the complete session. Reading the variables from the file for every pageview seems like unnecessary hard work.

Size of the .txt file is about 500 byte.

  • I know the variables may be stored as session variables, but I've been reccomended not to have to mutch data stored in session.

  • Another though was to cache the complete .txt file

What would be the best way to store those variables?

Answer Source

I ended up caching the four strings instead of the file. Then I also prevent reading the filecontent for each pageload.

string cachedString;
cachedString = (string)Cache["CacheItem"];
if (cachedString == null)
  cachedString = "Hello, World.";
  Cache.Insert("CacheItem", cachedString);

Based on the code at msdn it turned out a rather basic solution. Just modified it to allow several strings. :)

Thanks for all inputs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download