views:

34

answers:

3

We are writing a portal and like every portal we store html data in Db fro Modules. So I thought that I can cache each module in files. I use OnLoad event to check if there is a cache file for this Module, use that and else create cache file:

if (!IsPostBack)
  {
   string Path = AppDomain.CurrentDomain.BaseDirectory + "\\Cache\\Modules\\" + ModuleId + ".dat";
   if (File.Exists(Path))
   {
    Controls.Clear();
    Literal ltCache = new Literal();
    ltCache.Text = File.ReadAllText(Path);
    Controls.Add(ltCache);
   }
   else
   {
    base.OnLoad(e);
    StringBuilder SB = new StringBuilder();
    StringWriter SW = new StringWriter(SB);
    HtmlTextWriter htmlTW = new HtmlTextWriter(SW);
    //this.Visible = true;
    this.RenderControl(htmlTW);
    File.WriteAllText(Path, SB.ToString());
    //this.Visible = false;
   }
  }

but know I doubt that it's a good idea. what do you think? retrieving data from file can slow server more than retrieving data from DB?

A: 

well the text files won't be large so it would be better to store them in a database

Yassir
+3  A: 

Since you are using the .NET Framework, you should probably use one of the many .NET caching features.

You may want to check the following articles for some tips on the topic:

Daniel Vassallo
I want to cache data on server so if for example 500 users was on each page, application doesn't get data from DB 500 times; so OutPut Caching and Fragment caching.also data caching because keep data on Memory may cause problems on server. so what else? ;)
mahdi
+1  A: 

You should rather cache it in memory if you want a reasonable chance for it to be an improvment over the database.

The database is disk based with a memory cache, so if you cache it on disk that will only be faster if your files happens to be in the disk cache in memory. As the database cache is a lot larger than the disk cache, it's probable that a disk based cache would actually make it slower.

Guffa