Created
March 19, 2016 04:34
-
-
Save benrick/6848bb961d54f2cd12a3 to your computer and use it in GitHub Desktop.
Cached Repository Example - CachedAlbumRepository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CachedAlbumRepository : IAlbumRepository | |
{ | |
private readonly IAlbumRepository _albumRepository; | |
public CachedAlbumRepository(IAlbumRepository albumRepository) | |
{ | |
_albumRepository = albumRepository; | |
} | |
private static readonly object CacheLockObject = new object(); | |
public IEnumerable<Album> GetTopSellingAlbums(int count) | |
{ | |
Debug.Print("CachedAlbumRepository:GetTopSellingAlbums"); | |
string cacheKey = "TopSellingAlbums-" + count; | |
var result = HttpRuntime.Cache[cacheKey] as List<Album>; | |
if (result == null) | |
{ | |
lock (CacheLockObject) | |
{ | |
result = HttpRuntime.Cache[cacheKey] as List<Album>; | |
if (result == null) | |
{ | |
result = _albumRepository.GetTopSellingAlbums(count).ToList(); | |
HttpRuntime.Cache.Insert(cacheKey, result, null, | |
DateTime.Now.AddSeconds(60), TimeSpan.Zero); | |
} | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment