Using a Cache5:29 with James Churchill
Now that we've built our cache, let's see how to use it.
Our cache implementation is completely unaware of our database context. This works fine for our example, as we're integrating with the cache in our repository. But what if we wanted our cache to integrate with our database context?
This type of caching is called second level caching. For a walkthrough of how to implement a second level cache in EF, see this excellent article:
Let's update the Series repository to make use of our entity cache. 0:00 To start, let's comment out the existing implementation. 0:04 Then update the GetList method to check if the entity cache contains the list of 0:11 serious entities. 0:16 Remember, the EntityCache class is a static class and, 0:18 therefore, all of its methods are also static. 0:21 So we don't need to instantiate an instance of the class 0:24 in order to call the Get method. 0:28 We could just call it directly on the class. 0:30 For the Get method's generic type parameter, let's specify List<Series>. 0:38 The Get method expects a key value, 0:46 which will be used to identify the object that we're adding to the cache. 0:47 It could really be just about any string literal value, 0:51 provided that it's unique enough across our web app. 0:54 Let's keep it simple and use SeriesList. 0:57 Looks like we're missing a closing bracket here. 1:03 There, that looks better. 1:06 Now that we've attempted to retrieve the SeriesList from the EntityCache 1:09 we need to handle the case when the list hasn't yet been added to the cache. 1:13 We can do that by checking if the seriesList variable is null, 1:17 Then if the seriesList is null, 1:24 we need to execute the query to retrieve the list of series entities. 1:25 We can reuse the query from the previous method implementation. 1:29 And add the SeriesList to the cache so that the next call to the GetList method 1:50 will be able to retrieve the list from the cache. 1:54 And don't forget to return the seriesList variable. 2:00 Notice that we've repeated our SeriesList literal string value twice already. 2:07 This literal string value is an example of a magic string. 2:13 We want to avoid repeating magic strings throughout our code. 2:17 Doing so will make it more difficult to find and 2:20 change all these literal string values later on. 2:23 We can easily fix this issue by adding a constant for a cache item key. 2:25 And replace each string literal instance with a constant. 2:43 As the consumer of the EntityCache, here in the Series repository, 2:57 we have an opportunity to ensure that the Series entity 3:01 stored in the cache don't grow stale. 3:04 We can do that because the Series repository provides methods for 3:07 adding, updating and deleting Series entities. 3:11 But those methods are defined in the base repository class. 3:15 Modifying the implementation of these methods here in the base class wouldn't 3:20 work as these methods are used across all of the descended repositories. 3:24 But we can make these methods virtual so 3:28 the Series repository descendant class can override them and extend their behavior. 3:30 Now we can override each of those methods in the Series repository. 3:38 Then we could add to each method a call to the EntityCache's Remove method. 3:58 By calling the Remove method, we're ensuring the Series entity stored in 4:09 the cache don't become stale when the Series is added, updated or deleted. 4:13 By removing the Series entity from the cache when the GetList method is called 4:19 it'll execute the query to retrieve the entities from the database 4:23 instead of the cache. 4:27 Now let's test our changes. 4:29 In order to monitor how the EntityCache is used let's set 4:31 a break point in the GetList method. 4:34 And run the web app. 4:36 Browse to the add Comic Book page. 4:38 Step to the code. 4:41 And the query to retrieve the Series entities is executed. 4:44 Now refresh the page. 4:52 And step to the code. 4:53 And now, the list of Series entities is retrieved from the EntityCache. 4:56 Add a new Series And browse back to the series list page. 5:09 And the list of Series entities is retrieved from the database again, so 5:19 we don't display the stale data that was in the cache. 5:23
You need to sign up for Treehouse in order to download course files.Sign up