locked
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

  • Hi Joba,

    When you add instance to the MemoryCache, you could add ChangeMonitor and CallBack method to it. Code like this page. http://www.codeproject.com/Articles/290935/Using-MemoryCache-in-Net-4-0.

    When the source changes, it will call CallBack method to remove the cache. This page shows you some kind of ChagneMonitor, you could refer to it to see more information. http://msdn.microsoft.com/en-us/library/system.runtime.caching.changemonitor(v=vs.110).aspx.

    Hope useful to you.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 20, 2013 8:33 AM
  • Sorry, but it isn't useful. I've already seen those pages.

    I don't know how a callback will help. I mean, the callback only occurs when the item is already deleted. I don't want to log anything or know when it is deleted.

    I want to, somehow, delete all the keys associated with an User.


    Take a look at WPF FlashMessage
    About.me

    Wednesday, November 20, 2013 11:52 AM
  • Hi Joba,

    We could set NotifyOnChanged method to a dependency instance. When the instance changed, the event handler can be triggered, then you could do something in the event. See a code sample from this below page.

    http://stackoverflow.com/questions/7738244/c-sharp-4-0-memorycache-how-to-evict-dependent-cache-entries-when-changes-are.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 26, 2013 7:56 AM
  • Sorry, but I couldn't figure out what's going on in the example you provided.

    I couldn't understand how am I supposed to handle this in my particular case. Can you detail more?


    Take a look at WPF FlashMessage
    About.me

    Tuesday, December 3, 2013 10:02 PM
  • Sorry, but I couldn't figure out what's going on in the example you provided.

    I couldn't understand how am I supposed to handle this in my particular case. Can you detail more?


    Take a look at WPF FlashMessage
    About.me

    Tuesday, December 3, 2013 10:02 PM