Treba da napišem funkciju koja vraća listu jezika određenog usera.
Lista jezika se čuva u dictionary-ju a key je ClientID usera.
Cilj je da se ne čita iz baze ako statički dictionary ima vrednost za odgovarajući ClientID.
Dakle čitanje iz baze ide samo ako mora.
GetLanguages(userData) je funkcija koja čita iz baze.
Uvek treba biti pažljiv kod sinhronizacije thread-ova kada se radi sa statičkim vrednostima na web stranici.
A testiranje je jako problematično.
Nekako mi je logičnije da SyncLock bude unutar If - a ali to ima svojih problema.
Da li je ovo dobro rešenje?
Code:
Private Shared dictionary As New Dictionary(Of Integer, List(Of Language))
Public Shared Function GetListOfLanguages(ByVal userData As IUser) As List(Of Language)
If Not userData Is Nothing Then
SyncLock dictionary
If Not dictionary.ContainsKey(userData.ClientID) Then
dictionary.Add(userData.ClientID, GetLanguages(userData))
End If
End SyncLock
Return dictionary.Item(userData.ClientID)
End If
Return Nothing
End Function
Private Shared dictionary As New Dictionary(Of Integer, List(Of Language))
Public Shared Function GetListOfLanguages(ByVal userData As IUser) As List(Of Language)
If Not userData Is Nothing Then
SyncLock dictionary
If Not dictionary.ContainsKey(userData.ClientID) Then
dictionary.Add(userData.ClientID, GetLanguages(userData))
End If
End SyncLock
Return dictionary.Item(userData.ClientID)
End If
Return Nothing
End Function