Home  /  Questions  /  Question

67   95.8
Sep 25, 2013

Spark 4.5: How to update or refresh the cache?

Are there any examples as to how I should update the cache if a new artist is added from the MVC layer? What if I expose a web api method that allows an artist to be inserted into the database, how do I then refresh the cache on MVC layer? Do I move the ArtCache.cs from the MVC project into the Domain layer?

508   99.9
Sep 26, 2013
Parity Bit: 

Good question.

Once a change is made (for example an Artist is updated or a new Artist is added) you have these options: 

  1. Manually add the new Artist to the Cache or update the one already in cache, or 
  2. Clear the entire Artist cache. (You will find a ClearArtists method in the ArtCache that handles this for you)

The last method is the simplest and also the preferred way. Note that the cache will refill automatically through the lazy loading mechanism. 

The first method should only be used if your cache is very large and/or frequent updates take place (essentially you want to avoid frequent large database selects).  If you choose to use method 1. then remember to place locks around the critical code to maintain thread safety. 

By the way: you can add the extra code that interacts with the cache in your code behind or (even better) in the service layer. 
There is no need move it to the Domain project because caching here is rather application specific. (I would only move it to the Domain project if caching were a cross-cutting concern in which you wish to automatically cache just about all tables. In that case there are probably better 3rd party solutions to do that). 

Hope this helps. 
Jack Poorte