Scott Hanselman recently posted a blog article describing how to compress strings stored in the session / cache. This looks fairly promising, but the majority of data that I am storing in the session / cache are not strings but custom classes. How would you go about compressing these?
My initial thought would be to utilize the BinaryFormatter to serialize the object first (just like the ASP.NET framework would do normally when storing custom class objects into the session / cache), then compress the resulting byte array. However, this has the adverse side effect that the retrieved data from the session / cache would be readonly (since decompressing and deserializing would create a new in-memory object).
In other words, if my code currently looks like the following, is there a way to compress its storage into the session?
MyClass foo = new MyClass();
Session["foo"] = foo;
MyClass retrievedFoo1 = (MyClass) Session["foo"];
retrievedFoo1.Property1 = "property 1";
// retrievedFoo2.Property1 should equal "property 1"!
MyClass retrievedFoo2 = (MyClass) Session["foo"];