Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ADO : Fastest way of Database Access : Caching Records in Memory
 
Function DisplayCachedRecords(Secs)
	Dim retVal, datVal, temp1
		retVal = Application("cache_demo")
		datVal = Application("cache_demo_date")

		If datVal = "" Then
			datVal = DateAdd("s",Secs,Now)
		End If

		temp1 = DateDiff("s", Now, datVal)
			
	If temp1 > 0 And retVal <> "" Then
		DisplayCachedRecords = retVal

		' Debugging Code :
		Response.Write "<b><font color=""green"">Displaying from Cache"
		Response.Write " ... (" & temp1 & " secs remaining)</font></b>"
		Response.Write "<br><br>"
	Else

		Dim temp2
		' Change DisplayRecords() to the function whose 
		' value you want to cache
		temp2 = DisplayRecords()

		Application.Lock
			Application("cache_demo") = temp2
			Application("cache_demo_date") = DateAdd("s",Secs,Now)
		Application.UnLock

		DisplayCachedRecords = temp2

		' Debugging Code :
		Response.Write "<b><font color=""red"">Refreshing Cache ..."
		Response.Write "</font></b><br><br>"

	End If
End Function

Explanation
DisplayCachedRecords() takes a single argument of number of seconds after which it will refresh the content from database. To cache data in memory it makes use of two Application level variables; cache_demo and cache_demo_date.

In the beginning of the method it retrieves cache_demo and cache_demo_date Application variable's values. cache_demo contains the in-memory list of players while cache_demo_date contains the complete date till which to keep the content cached in memory, after which it will be refreshed.

	Dim retVal, datVal, temp1
		retVal = Application("cache_demo")
		datVal = Application("cache_demo_date")

Just in case if this is the first time this method has been called after an application startup ( after computer reboot for example ), we add a new value in the datVal variable making use of DateAdd() VBScript function which adds given number of seconds in the current data.

		If datVal = "" Then
			datVal = DateAdd("s",Secs,Now)
		End If

Now we check for the difference in number of seconds between current date ( Now() ) and the date in the datVal variable.

		temp1 = DateDiff("s", Now, datVal)

If the difference is more than 0 then it means that the expiry time has not reached, so show the content from the cache.

	If temp1 > 0 And retVal <> "" Then
		DisplayCachedRecords = retVal

		' Debugging Code :
		Response.Write "<b><font color=""green"">Displaying from Cache"
		Response.Write " ... (" & temp1 & " secs remaining)</font></b>"
		Response.Write "<br><br>"

If cache time has expired then we call the DisplayRecords() method again and retrieve the value into the cache_demo Application variable and set the new expiry date to current date plus given number of seconds to keep the content in memory. And then display the content to the user.

	Else

		Dim temp2
		' Change DisplayRecords() to the function whose 
		' value you want to cache
		temp2 = DisplayRecords()

		Application.Lock
			Application("cache_demo") = temp2
			Application("cache_demo_date") = DateAdd("s",Secs,Now)
		Application.UnLock

		DisplayCachedRecords = temp2

		' Debugging Code :
		Response.Write "<b><font color=""red"">Refreshing Cache ..."
		Response.Write "</font></b><br><br>"

	End If

Now wasn't it simple? DisplayCachedRecords() used two Application variables, two If ... Else statements and two VBScript functions DateAdd() and DateDiff() to do the job for us.

This is it for DisplayCachedRecords() function. Now lets see how to call this function from the ASP page. Open Cache.asp page and add the following code below the two functions we already saved in it :

<html>
<head>
	<title>Caching Records from Database Demo</title>
	<style>
	body, p, td { font-family:Sans-Serif; font-size:8pt; }
	td { padding-left: 5; }
	</style>
</head>
<body>
<%
	Dim t1, t2
		t1 = Timer

	Response.Write DisplayCachedRecords(60)

		t2 = Timer
%>

<p align="center">
Time Elapsed : <%= Left((CDbl((t2 - t1) * 1000.0)), 5) %> ms
</p>

</body>
</html>

Explanation
The ASP code above calls the DisplayCachedRecords() function :

	Response.Write DisplayCachedRecords(60)

The only argument ( 60 ) is the number of seconds to keep the data in memory and once this time period is reached, the content is refreshed.

Above code also makes use of Timer code to determine the number of milliseconds consumed by the DisplayCachedRecords() function.

Summary
We built two functions using simple VBScript. One of which accesses the database every time it is called to retrieve a list of 17 players with their first and last names. This function was called DisplayRecords(). Then the other main function called the first function to get a list of players and saved it in an Application variable in memory. It also stored the expiry date in a separate variable in memory. This function was called DisplayCachedRecords(). Every time this function was called it will display a list of players using in-memory cache. If cache time period has expired it will refresh the data in memory by calling DisplayRecords() again.

Tips

  • To run the two functions in production environment you should remove all the Response.Write statements under the ' Debugging Code :' comments within DisplayRecords() and DisplayCachedRecords() functions. Writing output from within the function is not required in production environment. We added Response.Write lines only to help us understand how the functions were working during development.
  • Also remove all the Timer code from Cache.asp page as Timer code takes unnecessary time.
  • To create your own custom cache functions, you should create two functions for every data access function. You can use the DisplayRecords() and DisplayCachedRecords() as templates. Change their names first. Then change the DisplayCachedRecords() functions to use other variables than cache_demo and cache_demo_date. For example you can use Application variables like cache_1 and cache_1_date. Then edit the DisplayRecords() function to generate your own list of data from your own database.

Well this is it. I hope you found the expiry date limited cache technique useful.

Function DisplayCachedRecords(Secs)
	Dim retVal, datVal, temp1
		retVal = Application("cache_demo")
		datVal = Application("cache_demo_date")

		If datVal = "" Then
			datVal = DateAdd("s",Secs,Now)
		End If

		temp1 = DateDiff("s", Now, datVal)
			
	If temp1 > 0 And retVal <> "" Then
		DisplayCachedRecords = retVal

		' Debugging Code :
		Response.Write "<b><font color=""green"">Displaying from Cache"
		Response.Write " ... (" & temp1 & " secs remaining)</font></b>"
		Response.Write "<br><br>"
	Else

		Dim temp2
		' Change DisplayRecords() to the function whose 
		' value you want to cache
		temp2 = DisplayRecords()

		Application.Lock
			Application("cache_demo") = temp2
			Application("cache_demo_date") = DateAdd("s",Secs,Now)
		Application.UnLock

		DisplayCachedRecords = temp2

		' Debugging Code :
		Response.Write "<b><font color=""red"">Refreshing Cache ..."
		Response.Write "</font></b><br><br>"

	End If
End Function

Explanation
DisplayCachedRecords() takes a single argument of number of seconds after which it will refresh the content from database. To cache data in memory it makes use of two Application level variables; cache_demo and cache_demo_date.

In the beginning of the method it retrieves cache_demo and cache_demo_date Application variable's values. cache_demo contains the in-memory list of players while cache_demo_date contains the complete date till which to keep the content cached in memory, after which it will be refreshed.

	Dim retVal, datVal, temp1
		retVal = Application("cache_demo")
		datVal = Application("cache_demo_date")

Just in case if this is the first time this method has been called after an application startup ( after computer reboot for example ), we add a new value in the datVal variable making use of DateAdd() VBScript function which adds given number of seconds in the current data.

		If datVal = "" Then
			datVal = DateAdd("s",Secs,Now)
		End If

Now we check for the difference in number of seconds between current date ( Now() ) and the date in the datVal variable.

		temp1 = DateDiff("s", Now, datVal)

If the difference is more than 0 then it means that the expiry time has not reached, so show the content from the cache.

	If temp1 > 0 And retVal <> "" Then
		DisplayCachedRecords = retVal

		' Debugging Code :
		Response.Write "<b><font color=""green"">Displaying from Cache"
		Response.Write " ... (" & temp1 & " secs remaining)</font></b>"
		Response.Write "<br><br>"

If cache time has expired then we call the DisplayRecords() method again and retrieve the value into the cache_demo Application variable and set the new expiry date to current date plus given number of seconds to keep the content in memory. And then display the content to the user.

	Else

		Dim temp2
		' Change DisplayRecords() to the function whose 
		' value you want to cache
		temp2 = DisplayRecords()

		Application.Lock
			Application("cache_demo") = temp2
			Application("cache_demo_date") = DateAdd("s",Secs,Now)
		Application.UnLock

		DisplayCachedRecords = temp2

		' Debugging Code :
		Response.Write "<b><font color=""red"">Refreshing Cache ..."
		Response.Write "</font></b><br><br>"

	End If

Now wasn't it simple? DisplayCachedRecords() used two Application variables, two If ... Else statements and two VBScript functions DateAdd() and DateDiff() to do the job for us.

This is it for DisplayCachedRecords() function. Now lets see how to call this function from the ASP page. Open Cache.asp page and add the following code below the two functions we already saved in it :

<html>
<head>
	<title>Caching Records from Database Demo</title>
	<style>
	body, p, td { font-family:Sans-Serif; font-size:8pt; }
	td { padding-left: 5; }
	</style>
</head>
<body>
<%
	Dim t1, t2
		t1 = Timer

	Response.Write DisplayCachedRecords(60)

		t2 = Timer
%>

<p align="center">
Time Elapsed : <%= Left((CDbl((t2 - t1) * 1000.0)), 5) %> ms
</p>

</body>
</html>

Explanation
The ASP code above calls the DisplayCachedRecords() function :

	Response.Write DisplayCachedRecords(60)

The only argument ( 60 ) is the number of seconds to keep the data in memory and once this time period is reached, the content is refreshed.

Above code also makes use of Timer code to determine the number of milliseconds consumed by the DisplayCachedRecords() function.

Summary
We built two functions using simple VBScript. One of which accesses the database every time it is called to retrieve a list of 17 players with their first and last names. This function was called DisplayRecords(). Then the other main function called the first function to get a list of players and saved it in an Application variable in memory. It also stored the expiry date in a separate variable in memory. This function was called DisplayCachedRecords(). Every time this function was called it will display a list of players using in-memory cache. If cache time period has expired it will refresh the data in memory by calling DisplayRecords() again.

Tips

  • To run the two functions in production environment you should remove all the Response.Write statements under the ' Debugging Code :' comments within DisplayRecords() and DisplayCachedRecords() functions. Writing output from within the function is not required in production environment. We added Response.Write lines only to help us understand how the functions were working during development.
  • Also remove all the Timer code from Cache.asp page as Timer code takes unnecessary time.
  • To create your own custom cache functions, you should create two functions for every data access function. You can use the DisplayRecords() and DisplayCachedRecords() as templates. Change their names first. Then change the DisplayCachedRecords() functions to use other variables than cache_demo and cache_demo_date. For example you can use Application variables like cache_1 and cache_1_date. Then edit the DisplayRecords() function to generate your own list of data from your own database.

Well this is it. I hope you found the expiry date limited cache technique useful.


Previous ( 1 Gone )( No Further Pages )

See all comments and questions (post-ad) posted for this tutorial.


Related Articles
  1. How to display records from top 4 database systems using plain ASP?
  2. Uploading Files into an Access Database using plain ASP
  3. Displaying Images from an Access Database using plain ASP
  4. Adding records to the database with ASP
  5. DSN vs DSN less Database Connections
  6. Searching and sorting records in a recordset from the database
  7. Speedup Database Access using GetRows
  8. Inserting Form content into Database with ASP
  9. Using ASP pages to page through Recordsets
  10. Generating Random Records from the Database

Comments/Questions ( Threads: 4, Comments: 11 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Display Record in table
  2. Which database ? ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  3. Refresh List box using javascript ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  4. Speed up data retrieval time ( 5 Replies ) This thread contains 3 replies by the Author of this Article. This thread contains 3 replies by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.