Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / August 2007

Tip: Looking for answers? Try searching our database.

Caching business objects?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Showjumper - 22 Aug 2007 14:09 GMT
Hi
I have custom bustom objects whose properties i populate using a datareader.
Is it possible to cache the business object afteri have populated it? If so,
how? Everything i have tried does not work.

Ashok
Eliyahu Goldin - 22 Aug 2007 14:34 GMT
Did you try putting it into a session variable?

Signature

Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net

> Hi
> I have custom bustom objects whose properties i populate using a
> datareader. Is it possible to cache the business object afteri have
> populated it? If so, how? Everything i have tried does not work.
>
> Ashok
sloan - 22 Aug 2007 15:08 GMT
When I know I need to cache these objects, I usually go ahead an put on the
[Serializable]
attribute.

This would affect you if you ever went to Session storing like sql server
session state.

You can also look at this fancy object I wrote, for a "strong typed" version
of storing items in Session.

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!151.entry

If you're on 1.1, go back to the main blog page, there is a 1.1 version of
it as well.

> Hi
> I have custom bustom objects whose properties i populate using a
> datareader. Is it possible to cache the business object afteri have
> populated it? If so, how? Everything i have tried does not work.
>
> Ashok
Showjumper - 23 Aug 2007 01:15 GMT
No i did not try session variables. I was attempting to use cache.insert
> Hi
> I have custom bustom objects whose properties i populate using a
> datareader. Is it possible to cache the business object afteri have
> populated it? If so, how? Everything i have tried does not work.
>
> Ashok
Paul - 23 Aug 2007 12:59 GMT
Here is what we use ( in a static class )
Be aware that Cache is global and NOT session based.

       #region checkCache
       /// <summary>
       /// Will test for the requested value in the Session Cache.
       /// </summary>
       /// <typeparam name="T">Type of cached object</typeparam>
       /// <param name="sKey">Key to search on</param>
       /// <returns>T</returns>
       public static T checkCache<T>(string sKey)
       {
           T returnValue = default(T);

           if (HttpContext.Current != null)
           {
               object oValue = HttpContext.Current.Cache.Get(sKey);

               if (oValue is T)
               {
                   returnValue = (T)oValue;
               }
           }

           return returnValue;
       }
       #endregion

       #region cacheItem
       /// <summary>
       /// Cache items to the session chache for a set number of
minutes.
       /// </summary>
       /// <param name="sKey"></param>
       /// <param name="oItem"></param>
       /// <param name="cacheMinutes"></param>
       public static void cacheItem(string sKey, object oItem, int
cacheMinutes)
       {
           if (HttpContext.Current != null && oItem != null)
           {
               Cache cache = HttpContext.Current.Cache;

               object oExistingItem = cache.Get(sKey);

               // If the item is already cached
               if (oExistingItem != null)
               {
                   cache.Remove(sKey);
               }

               cache.Add(sKey, oItem, null,
DateTime.Now.AddMinutes(cacheMinutes), Cache.NoSlidingExpiration,
CacheItemPriority.Normal, null);

           }
       }
       #endregion

       #region expireCacheItem
       /// <summary>
       /// Removes an item from the Cache.
       /// </summary>
       /// <param name="sKey"></param>
       public static void expireCacheItem(string sKey)
       {
           if (HttpContext.Current != null)
           {
               HttpContext.Current.Cache.Remove(sKey);
           }
       }
       #endregion
Paul - 23 Aug 2007 13:00 GMT
An example of calling that code using the Session ID to make is
session specific in this case.

               string sCacheKey = "CMSContext{0}{1}";
               sCacheKey = string.Format(sCacheKey,
loggedOnUser.GUID, Session.SessionID);

               CMSContext currentContext =
util.checkCache<CMSContext>(sCacheKey);

               if (currentContext == null)
               {
                   currentContext = new CMSContext(Session,
loggedOnUser);
               }

               // Unlike most cache Items we refresh the Cache of
this Item every time it is accessed
               util.cacheItem(sCacheKey, currentContext, 20);

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.