Direi che, se il tuo inserimento/aggiornamento/cancellazione modifica il contenuto di uno di questi elenchi, dovresti richiedere l'elenco. Ho alcuni dati della cache nella mia app, e io uso una raccolta della struttura sottostante per mantenerli. In questo modo, è facile svuotare l'intera cache, e quando chiedo un particolare datatable controllo per vedere se ne esiste uno nella cache che non è scaduto.
Protected Structure CachedDT
#Region "Local Variables"
Public TheDT As DataTable
Public TheExpirationTime As DateTime
Public TheUniqueIdentifier As String
#End Region 'Local Variables
End Structure
Protected cCachedDTs As Dictionary(Of String, CachedDT) = New Dictionary(Of String, CachedDT)
Questi vivono nella mia classe base per oggetti che interrogano i database. Un esempio di utilizzo delle datatables cache è:
<System.Diagnostics.DebuggerStepThrough> _
Public Overrides Function GetPermissionsSystem(ByVal SystemUserName As String) As DataTable
Try
Dim oCmd As New SqlCommand
Dim aDpt As New SqlDataAdapter
Dim aDst As New DataSet
Dim theCached As CachedDT
Dim theCacheName As String = "GetPermissionsSystem|" & SystemUserName
If cCachedDTs.ContainsKey(theCacheName) Then
theCached = cCachedDTs.Item(theCacheName)
If theCached.TheExpirationTime < DateTime.Now Then
cCachedDTs.Remove(theCacheName)
Else
Return theCached.TheDT
End If
End If
With oCmd
.Connection = MyBase.Conn
.CommandType = CommandType.StoredProcedure
.CommandTimeout = MyBase.TimeoutShort
.CommandText = Invoicing.GetPermissionsSystem
.Parameters.Add(GP("@SystemUserName", SystemUserName))
End With
aDpt.SelectCommand = oCmd
aDpt.Fill(aDst)
theCached = New CachedDT
With theCached
.TheUniqueIdentifier = theCacheName
.TheExpirationTime = DateTime.Now.AddSeconds(10)
.TheDT = aDst.Tables(0)
End With
cCachedDTs.Add(theCached.TheUniqueIdentifier, theCached)
Return aDst.Tables(0)
Catch sqlex As SqlException
MyBase.HandelEX(sqlex)
Catch ex As Exception
MyBase.HandleEX(ex)
Finally
MyBase.CloseConn()
End Try
End Function
Nell'esempio precedente, la funzione controlla la cache per vedere se esiste un oggetto appropriato. Se lo fa, viene restituito piuttosto che colpire nuovamente il database. Alla fine, l'oggetto fresco viene aggiunto alla cache.
Tutto quello che dovresti fare sarebbe fornire dei mezzi per rimuovere un particolare elenco dalla cache. Quindi, quando si esegue un inserimento/aggiornamento/eliminazione, assicurarsi di cancellare l'elemento appropriato.
fonte
2010-11-12 16:11:52
Buone domande di follow-up. –
"I dati possono essere obsoleti? Se sì, per quanto tempo?": Forse 1 ora, forse 1 anno. Il problema principale deriva da un dominio aziendale volatile. – boj