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

Get Recordset from 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-19372    /dev :: home :: articles :: Caching Recorsets  298 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


    To refresh our minds, we remember that GetRecordset() function accepts an SQL string as the input, and returns a Recordset object filled with data for the given SQL statement. The function itself will decide whether to get the data directly from the database or from the cache.

    If the requested data is not available in the cache, it must retrieve it from the database, then store it in the cache for later use.
    Public Function GetRecordset(SQL)
    
    Dim conn, rsTemp, sBuffer
    
    'Create a temporary recordset
    Set rsTemp = Server.CreateObject("AdoDB.Recordset")
    
    'Check whether the requested SQL is in cache
    If dDataCache.Exists(SQL) then
    	'Already in cache
    	
    	'Get stream ready
    	stCache.Open
    	
    	'Retrieve data from the cache
    	sBuffer = dDataCache.Item(SQL)
    	
    	'Fill the stream
    	stCache.Write sBuffer
    	stCache.Position = 0
    	
    	'Deserialize stream into Recorset
    	rsTemp.Open stCache
    	
    	'Now rsTemp is ready to use in full power
    	
    	stCache.Close
    	
    Else
    	'Not in cache
    	
    	'Get the data from the database
    	Set connTemp = Server.CreateObject("ADODB.Connection")
    	connTemp.Open ConnectionString
    	rsTemp.CursorLocation = 2 'adUseServer
    	rsTemp.Open SQL, connTemp, 3, 1, 1
    	'3, 1, 1 = adOpenStatic, adLockReadOnly, adCmdText
    	
    	'Save recordset in the stream in ADTG format
    	rsTemp.Save stCache, 0 'adPersistADTG
    	
    	'Optionally, XML serialization can be used:
    	'rsTemp.Save stTemp, 1 'adPersistXML
    	'However, this would require some modifications
    
    	'Serialize the stream as text
    	sBuffer = stCache.Read(-1) 'adReadAll
    	
    	'Lock the application object for synch issues
    	Application.Lock
    
    	'Add the serialized data to the cache
    	'We use SQL statement as the unique item key
    	dDataCache.Add SQL, sBuffer
    
    	Application.Unlock
    	stCache.Close
    
    End If
    
    'Return the recordset, it doesn't matter
    'whether it is from cache or from database
    Set GetRecordset = rsTemp
    
    'Dispose the temporary object
    Set rsTemp = nothing
    
    End Function
    
    

    Ok, the most important part is done. Although it may look a little bit advanced and complex, basically it performs a simple task of returning a Recordset object either from the database or from the cache.

    < Previous Part: Cache Storage and Implementation
    > Next Part: Cache Expiration and Removal



    Who's Online

     Bot: Google
     Bot: Msn
     (235) guests
     (61) 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 94 ms.]