views:

29

answers:

1

I have the following Repository:

Public Class PageRepository
    Private Shared _pages As BLL.PageCollection

    Shared Function AllPages() As BLL.PageCollection
        If _pages Is Nothing Then _pages = new BLL.PageCollection(LOADALL)
        Return _pages
    End Function
End Class

I do all selects using LINQ on the PageRepository.AllPages, and I also Add new entities through the collection using Repository.AllPages.AddNew().

When I call Repository.AllPages.Save() the data is stored in the database, however the _pages private shared variable is maintained.

Should I somehow force a refresh of this variable everytime a page is updated? Or should this be done through a Module / Static class and is my implementation wrong?

+1  A: 

When you're performing the save, you are invalidating the cache. You should force a refresh at that point or somehow expire the cache to any consumers. That depends how it's being consumed and how you designed your architecture.

Are you sure that caching is actually needed? SQL Server (and other DBs) have nice caching features and the LINQ dataContext for LINQ to SQL and Entity Framework have additional features that may be utilized. In short, have you tried using

Public Class PageRepository

    Shared Function AllPages() As BLL.PageCollection
        Return new BLL.PageCollection(LOADALL)
    End Function

End Class

If you have and you do need the caching, can you push the caching to the BLL?

greglev
I'm using Entityspaces, a 3d party ORM tool. They also suggest putting the collection into a static variable (inside the PageCollection class though). However that doesn't really change this issue. The AllPages() function is used all over the place to make sure only 1 database query is performed. However at this point zero queries are performed after the first request because the collection is cached at application level. Isn't it good practice to cache pages? (used for sitemap)
Ropstah
I was just making sure caching is actually needed. There is nothing wrong with it.I'm unfamiliar with using EntitySpaces. If it doesn't support caching the way you would like, you could try using a separate thread / DispatchTimer to perform the query on a specified interval and update the cache as needed.
greglev
What I'd actually want is just to 'cache' the results at request level, not at application level. And by cache the result I mean store the value of that static variable. Isn't possible in a simple way?
Ropstah