|
Get Recordset from Cache :: Article - 2eNetWorX
|
|||
| 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 |
» Contributions
» Recordset Cache » OpenSource » Statisticus » OpenCYA!
» Undocumented... » Ask For Login » DSN'less Connection » Who's Online? » (All Samples)
|
Improving Database Performance by Caching serialized Recordsetsby Hakan Eskici Part 1 - Key Features and DesignPart 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.
|
Bot: Google
echoPANEL, Free .NET
FogBugz Hosting Plans
|
| privacy policy , license , disclaimer , © 2000-2004, 2eNetWorX. [page generated in 94 ms.] |