none
MemoryCache custom ChangeMonitor RRS feed

  • Question

  • I couldn't find any good examples of how to use ChangeMonitor classes.

    Here's my problem: I'm caching a few method of my Repository, building a key to uniquely identify the cached data. The cached data has a expiration time, but the data can change before the expiration takes place.

    Take a look:

     public class CachedUserRepository : CachedRepositoryBase, IUserRepository
        {
            private readonly CacheKeyBuilder keyBuilder;
            private readonly IUserRepository repository;
            private readonly CacheManager cacheManager;
    
            public CachedUserRepository(IUserRepository repository)
            {
                this.repository = repository;
                keyBuilder = new CacheKeyBuilder();
                cacheManager = CacheManager.Instance;
            }
    
            public IEnumerable<Content> GetWatchList(User user, Size cover = null, Pagination pagination = null)
            {
                var key = keyBuilder.Create(GetType()).With(user, cover, pagination).Build();
                var entry = cacheManager.Get(key);
                if (entry != null)
                    return (IEnumerable<Content>)entry;
    
                var result = repository.GetWatchList(user, cover, pagination);
                cacheManager.Add(user, key, result, Duration(CacheType.VeryLow));
    
                return result;
            }

    I'm caching the WatchList of the users. It's a web app and there could be several users, so I'm building a key for each one of them. That's fine. However, my repository has the following method as well:

    public void AddBookmark(User user, Identifier contentId)
            {
                repository.AddBookmark(user, contentId);
            }

    When the following method is called, I need to clear any cached data of the given user (method's parameter). How to accomplish that? ChangeMonitor is the way? And if it is, how can I write the code for it?


    Take a look at WPF FlashMessage
    About.me

    Tuesday, November 19, 2013 7:34 PM

All replies