Reputation: 6756
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?
Upvotes: 1
Views: 1276
Reputation: 11584
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).
Upvotes: 3
Reputation: 61518
Does your List(Of x) requires any processing or is it just raw data pulled from a database?
If it's just raw data pulled from a database it might be a good idea to cache it at the data access layer.
But if it required processing, then it should be done in the business logic layer
Since you're referring to presentational code and .DLL, did you happen to mean an n-tier architecture?
Upvotes: 2
Reputation: 183
You might consider doing it without caching and seeing if you have a performance/network issue. This might be premature optimazation?
Upvotes: 0