Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mixing cache and sessions within a session object

Status
Not open for further replies.

blove57

Programmer
Nov 27, 2007
6
0
0
US
I'm having trouble getting my app to compile. What I'm trying to do is I have a class that has a bunch of cache and session objects. But then I'm putting that whole class within the user's session for easy referencing. This is what I have:

public partial class test_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Globals.Site.SessionTest = "sessionTest2";
Globals.Site.CacheTest = "cacheTest2";

Response.Write("sessionTest = " + Globals.Site.SessionTest + "<br>");
Response.Write("cacheTest = " + Globals.Site.CacheTest);
}
}

public class SiteSettings
{
private string _sessionTest = (string)HttpContext.Current.Session["stest"];
public string SessionTest
{
get
{
return _sessionTest;
}
set
{
HttpContext.Current.Session["stest"] = value;
}
}

private string _cacheTest = (string)HttpContext.Current.Cache["ctest"];
public string CacheTest
{
get
{
return _cacheTest;
}
set
{
HttpContext.Current.Cache["ctest"] = value;
}
}
}

public static class Globals
{
public static SiteSettings Site
{
get
{
if (HttpContext.Current.Session["siteID"] == null)
HttpContext.Current.Session["siteID"] = new SiteSettings();
return (SiteSettings)HttpContext.Current.Session["siteID"];
}
}
}

The compile error I get is:
Unable to cast object of type 'SiteSettings' to type 'SiteSettings'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'SiteSettings' to type 'SiteSettings'.

Source Error:

Line 66: if (HttpContext.Current.Session["siteID"] == null)
Line 67: HttpContext.Current.Session["siteID"] = new SiteSettings();
Line 68: return (SiteSettings)HttpContext.Current.Session["siteID"];
Line 69: }
Line 70: }
Source File: C:\ Line: 68

Am I allowed to put a mixed bag of cache and session items into a session slot of a user?? Not sure if I'm going about this the right way so I would greatly appreciate advice on how to handle this. What my intention is the SiteSetting class will hold some values that are meant to share (like images directory or store email) and other private user data (like cart total). Am I defeating the purpose by putting cache values within a sessioned object class?
 
right idea, wrong implementation just instantiate an object and then set it to the cache. like this
Code:
public class Foo
{
   private string bar;
   public string Bar 
   { 
      get { return bar; }
      set { bar = value; }
   }
}

public class FooCacheManager
{
   public static Foo GetFoo()
   {
      return (Foo)HttpContext.Current.Cache["foo"];
   }

   public static void Save(Foo f)
   {
      HttpContext.Current.Cache["foo"] = f;
   }
}
the same can be done for session instead of cache.
then in a page event:
Code:
Foo fooy = new Foo();
fooy.Bar = "fubar";

FooCacheManager.Save(fooy);
response.write(FooCacheManager.GetFoo().Bar);

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top