We have data that’s updated nightly in a database residing in the same instance as my app’s database. So to save on database calls, I want to cache this static-for-the-day data into a List(Of MyObject). From a theory point of view, should this cached List(Of ) be cached in the presentation layer code, via a global variable? Should it be in a global variable in the .DLL?
I’m thinking in the .DLL, because I created a service layer, which is exposed publicly to the GUI and makes calls to the data access layer inside the .DLL:
Public Shared Function Search(ByVal criteria As Core.Other.Customer) As List(Of Core.Other.Customer) ' TODO: Check the customer cache to see if it has been populated yet. If not, populate it. If 1 = 1 Then ' TODO: Variable 'list' needs to be a global object in the DLL. ' For SO readers: Dal class declared Friend. Dim list As List(Of Core.Other.Customer) = Dal.Search.Customers.GetCache() End If Dim results As New List(Of Core.Other.Customer) ' TODO: Find the relevant customers in the cache and add them to variable 'results'. Return results End Function
Am I going about this the best way that I can?
I would tend to cache this in your service layer. I like to keep my data access simple (typically using an OR/M like NHibernate) so I don’t want to do anything goofy there that might change my expectation of how the data access layer works (as a developer, I would expect all calls to the DAL to actually hit the DB, and not a cache, unless it was the OR’M’s cache, and that’s an implementation detail I don’t care about).
The service seems like the appropriate place (and when I cache data, that is where I do it in my apps, if that helps at all).