2eNetWorX :: Development Community with Tutorial, Script, Code, Programming sections for Visual Basic, Active Server Pages and .Net Framework Developers.  

Expire Cache :: Article - 2eNetWorX


This article explains how to build a Recordset caching system for your ASP web site to greatly improve the performance.

 
  2eNetWorX :: ASP, VB, Asp.Net, Vb.Net, C#, dotNet, .Net OpenSource Projects
MyDev What's New | Projects | Tutorials & Articles | Services | Stats | Contact Us
Guest-19463    /dev :: home :: articles :: Caching Recorsets  197 online visitors
Popular Projects

  • TableEditor
  • StatCounteX
  • 2eNetWorX/Dev
  • MiniSurvey
  • OpenForum
  • PowerTool
  • ShopEZ
  • Trans-It
  • HiLiteR
  • (View All Projects)


    Top 10

    » Contributions


    Tutorials & Articles

    » Recordset Cache
    » OpenSource
    » Statisticus
    » OpenCYA!


    Code Samples

    » Undocumented...
    » Ask For Login
    » DSN'less Connection
    » Who's Online?
    » (All Samples)


    Link Back



    Site Banner


  • Improving Database Performance by Caching serialized Recordsets

    by Hakan Eskici

    Part 1 - Key Features and Design
    Part 2 - Cache Storage and Implementation
    Part 3 - GetRecordset Function
    Part 4 - Cache Expiration and Removal
    Part 5 - Using the Cache Engine in your Projects
    Part 6 - Case Study and Final Words


    The other most important feature of a cache engine is the ability to expire (remove) cached entries from the storage. In our implementation, we will provide a method that will be manually called. We could also build an automatic expiration feature, but that would make things even more complex. So let's keep it simple:

    Public Sub Remove(SQL)
    
    	'Lock the application
    	Application.Lock
    	
    	'Check whether the data is in cache
    	If dDataCache.Exists(SQL) Then 
    		'It's in cache, so remove it
    		dDataCache.Remove SQL
    	End If
    	
    	'Unlock the application
    	Application.UnLock
    	
    End Sub
    
    

    The trick here is to know when to expire (remove) the cached entries. The best approach would be calling the Remove() method when we are sure that the underlying database table has changed. If that table gets updated very often (such as in a few minutes) it might be useful to delay calling Remove() for a couple of updates.

    The bad thing about the Remove() method is that we need to know the exact SQL statement of the cached data. In most cases, this is an annoying task. Wouldn't it be nice to have a method that will expire all entries that belong to a specific database table?

    So we could call it like:

    MyCache.ExpireTable "Customers"
    

    Then it would expire all entries such as:

    SELECT * FROM Customers
    SELECT FirstName FROM Customers WHERE CustomerID = 531
    SELECT SUM(Balance) FROM Customers
    SELECT TOP 5 FROM Customers ORDER BY Balance DESC
    ... etc
    

    There we go:

    Public Sub ExpireTable(TableName)
    
    'Get keys as an array
    aKeys = dDataCache.Keys
    
    'How many entries?
    iCount = dDataCache.Count
    
    'Enumerate cache
    For i = 0 To iCount - 1
    
    	'Get SQL
    	sSQL = aKeys(i)
    	
    	sItemTable = ""
    	
    	'Find the underlying table
    	aSQL = Split(sSQL, " ")
    	For iSQL = 0 To UBound(aSQL) - 1
    		'Look for FROM keyword
    		If LCase(aSQL(iSQL)) = "from" Then
    			'Found it
    			sItemTable = aSQL(iSQL + 1)
    			Exit For
    		End if
    	Next
    	
    	If LCase(TableName) = LCase(sItemTable) Then
    		'Remove the entry
    		dDataCache.Remove sSQL
    	End If
    
    Next
    	
    End Sub
    
    

    A little bit tricky, but that's all we can do for now.

    Finally, we will build a method that removes all entries from the cache.

    That one is fairly easy, so there are no code comments:

    Public Sub RemoveAll
    
    	Application.Lock
    	dDataCache.RemoveAll
    	Application.Unlock
    
    End Sub
    


    < Previous Part: GetRecordset Function
    > Next Part: Using the Cache Engine in your Projects



    Who's Online

     Bot: Msn
     Bot: Google
     (82) guests
     (113) repeated

    How is this done?


    Popular Web Sites

      echoPANEL, Free .NET
      Webserver Control Panel

      FogBugz Hosting Plans
      SubVersion Hosting Plans
      SourceGear Vault
      Hosting Plans

    Admin's Toolbox

    Web Database Management
    Web Site Statistics
    Survey Manager
    Integrated forum for webpages
    OpenSource WebSite Framework


      privacy policy , license , disclaimer , © 2000-2004, 2eNetWorX.   [page generated in 63 ms.]