Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 18th, 2010, 05:32 AM
Authorized User
 
Join Date: Jul 2010
Posts: 23
Thanks: 7
Thanked 2 Times in 1 Post
Default Saving integer in cache

Hi, just finished chapter 16 in Imars awesome book and i want to implement caching to my own little project.

I am counting the number of pictures assosiated with a tag, and i want to save the number in my cache. But i am unsure of how to check if the item is in the cache already.

I wanted to use TryCast(and then check for Nothing) but it did not work with Integers :s

Code:
            Dim count As Int32 = Nothing
            count = Cache(TagN + "Count")
            If count = Nothing Then
                count = (From ccc In myEnt.TagLinks
                        Where ccc.Tag.TagName = TagN
                        Select ccc).Count

                Cache.Insert(TagN + "Count", count, Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10))

            End If
How should i do this? Are there other things in this code snippet i should change?
 
Old September 19th, 2010, 07:57 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

TryCast only works with reference types as it returns Nothing when the conversion fails. Try this code instead:
Code:
 
Dim count As Int32
Dim cachedCount As Object = Cache("Count")
If cachedCount Is Nothing Then
  count = 23
  Cache.Insert("Count", count, Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10))
Else
  count = CType(cachedCount, Int32)
End If
This code checks if the cached item is Nothing (using Is, not =). If it is, it assigns some value and stores the item in the cache. If the item does exist, it's casted to an Integer using CType.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
andreas5 (September 19th, 2010)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Data Cache not saving rturner003 ASP.NET 2.0 Professional 1 August 8th, 2007 07:29 PM
testing integer rjonk XSLT 2 July 27th, 2006 03:11 PM
EXCEL question saving a file saving the the first macupryk VS.NET 2002/2003 0 January 6th, 2005 05:33 PM
How To avoid the SAving of toolbars while saving Hari_Word Excel VBA 6 July 26th, 2004 12:13 AM
Testing for integer MattLeek Excel VBA 8 March 7th, 2004 09:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.