[AccessD] Cache coherency monitor

jwcolby jwcolby at colbyconsulting.com
Wed Dec 22 16:52:56 CST 2010


I have developed a class to allow my various cache supervisors to monitor changes to cache data.

'---------------------------------------------------------------------------------------
' Module    : clsCacheState
' Author    : jcolby
' Date      : 12/22/2010
' Purpose   : Provide an object that tracks cache state
'
'It is occasionally necessary to cache infrequently changing data in a table so that it can be
'used in a program at full speed without having to go to the table every time you want a value.
'
'For example the security system uses Users, Groups and UserGroups.  These items don't change
'often, sometimes not for weeks at a time.  It is useful to be able to read the users into a
'user cache, groups into a groups cache etc.
'
'Even so, things can change so it is useful to have a way to signal the client application such
'that if something changes, it can be sensed and the cache refreshed.
'
'In order to monitor cache coherency we hold a recordset open.  We select a specific record
'and then examine a specific field for a date / time.
'
'Holding that record open allows any modification to that record to be sensed instantly
'
'This class allows us to use the same sensing mechanism across different cache by using instances
'of this class in the cache supervisor class
'---------------------------------------------------------------------------------------
Const cstrModule = "clsCacheState"
Option Compare Database
Option Explicit

Private mDteRefreshed As Date       'Date the cache was refreshed
Private mstrSQL As String           'The SQL statement required to obtain a date time from a cached 
table
Private mstrSQLUpdate As String     'The SQL statement used to update at a given instant

Private mdb As DAO.Database
Private mstrSVTblName As String      'The name of the SV table being monitored
Private mstrSVValFld As String      'The name of the field in the table to monitor
Private mstrSVNameFld As String     'The name of the SV Name field (used to find the correct record)
Private mstrSVName As String        'The SV name to search for (which record to monitor)

Private mRst As DAO.Recordset       'The recordset to hold open to monitor the cache coherency date

Private Sub Class_Terminate()
On Error Resume Next
     mRst.Close
     Set mRst = Nothing
End Sub
'---------------------------------------------------------------------------------------
' Procedure : mInit
' Author    : jcolby
' Date      : 12/22/2010
' Purpose   : Opens the recordset to a specific record and holds the rst open
'
'db is the database that the SysVar table is in
'strSVTblName is the name of the SysVar table
'strSVNameFld is the name of the field to use in searching for a specific SV record
'strSVValFld is the name of the field that will contain the SysVar value to be monitored
'strSVName is the "name" of the sysvar to search for in the SysVar table
'---------------------------------------------------------------------------------------
'
Public Function mInit(db As DAO.Database, strSVTblName As String, strSVNameFld As String, 
strSVValFld As String, strSVName As String)
On Error GoTo Err_mInit

     mstrSVTblName = strSVTblName
     mstrSVValFld = strSVValFld
     mstrSVNameFld = strSVNameFld
     mstrSVName = strSVName
     mstrSQL = "SELECT " & strSVValFld & " " & _
                 "FROM " & strSVTblName & " " & _
                 "WHERE " & strSVNameFld & " = '" & strSVName & "'"
     Set mdb = db    'Save for later
     Set mRst = mdb.OpenRecordset(mstrSQL)

Exit_mInit:
     On Error Resume Next
     Exit Function
Err_mInit:
     Select Case Err
     Case 0      '.insert Errors you wish to ignore here
         Resume Next
     Case Else   '.All other errors will trap
         Beep
         PLSLogErr Err.Number, Err.Description, Erl, cstrModule, "mInit"
         Resume Exit_mInit
     End Select
     Resume 0    '.FOR TROUBLESHOOTING
End Function
Property Get pSQL() As String
     pSQL = mstrSQL
End Property
Property Get pModified() As Date
     pModified = mRst(mstrSVValFld)
End Property

'Private mstrSVTblName As String      'The name of the SV table being monitored
'Private mstrSVValFld As String      'The name of the field in the table to monitor
'Private mstrSVNameFld As String     'The name of the SV Name field (used to find the correct record)
'Private mstrSVValFld As String      'The name of rhe SV Value field to monitor
'Private mstrSVName As String        'The SV name to search for (which record to monitor)
'---------------------------------------------------------------------------------------
' Procedure : pModified
' Author    : jcolby
' Date      : 12/22/2010
' Purpose   : used to modify the date value in the SV table / field / record being monitored
'---------------------------------------------------------------------------------------
'
Property Let pModified(ldteModified As Date)
On Error GoTo Err_pModified

     mstrSQLUpdate = "UPDATE " & mstrSVTblName & " SET " & mstrSVTblName & "." & mstrSVValFld & " = 
'" & CStr(ldteModified) & "' " & _
                 "WHERE ((" & mstrSVTblName & "." & mstrSVNameFld & " ='" & mstrSVName & "'));"
     mdb.Execute mstrSQLUpdate

Exit_pModified:
     On Error Resume Next
     Exit Property
Err_pModified:
     Select Case Err
     Case 0      '.insert Errors you wish to ignore here
         Resume Next
     Case Else   '.All other errors will trap
         Beep
         PLSLogErr Err.Number, Err.Description, Erl, cstrModule, "pModified"
         Resume Exit_pModified
     End Select
     Resume 0    '.FOR TROUBLESHOOTING
End Property
Property Get pRefreshed() As Date
     pRefreshed = mDteRefreshed
End Property
'
'Set the refreshed date when the cache is refreshed
Property Let pRefreshed(ldteRefreshed As Date)
     mDteRefreshed = ldteRefreshed
End Property
'
'Compares the datetime refreshed to the dateTime modified
'Returns true if a refresh is needed
Property Get pRefreshNeeded() As Boolean
     pRefreshNeeded = (pRefreshed < pModified)
End Property

-- 
John W. Colby
www.ColbyConsulting.com



More information about the AccessD mailing list