Sunday, September 13, 2009

Compressing ASP.NET Session and Cache State

A lot of ASP.NET web applications out there rely on Session and Cache. Session is used for storing user and session specific information (including logged in credentials, user's products, etc). Cache normally include application wide specific data. As mentioned in my previous article all this requires ViewState.

If you do require Session and Cache to a large degree in your web applications (and its a valid implementation) then I suggest looking at another Scott Hanselman article. He shows you how to implement ZIP compression on your Session and Cache data. He provided an extension (.NET 2 and higher) to compress and decompress data stored in these containers.

How to use it? Here is a snippet from his article on how easy it is:

1.Session["foo"] = "Session: this is a test of the emergency broadcast system.";
2.Zip.Session["foo"] = "ZipSession this is a test of the emergency broadcast system.";
3.string zipsession = Zip.Session["foo"];
4.Cache["foo"] = "Cache: this is a test of the emergency broadcast system.";
5.Zip.Cache["foo"] = "ZipCache: this is a test of the emergency broadcast system.";
6.string zipfoo = Zip.Cache["foo"];


Just by doing this you can store 5 to 10 times the amount of data for the same amount of ViewState. Another huge saving.

No comments:

Post a Comment