daxu daxu -3 years ago 94
C# Question

What is a good way to implement cache aside pattern?

For example, I have some repositories to grab some data I need:

Addressrepository.GetAddress(string para1, string para2)

UserRepository.GetUserDetail(string userName)

FinancialRepository.GetFinancialInfo(int userId)


To apply a cache aside pattern, I would like to do this:


  1. base on parameters passing in and some identifier for each
    repository, construct a key.

  2. Check Memory cache(or redis cache if we do down that route).

  3. If info can't be found or expired, call the repository function to grab data and put into cache.



Ideally I would like to write a generic helper class to do this for all data loading functions.Something like the cache aside pattern described here:
https://blog.cdemi.io/design-patterns-cache-aside-pattern/

However, in my case, I need to pass different parameters to different methods. In this case, is it possible to use Func and pass different parameters?

I checked msdn about this:

Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult> Delegate


But how do I pass different type parameter and different number of parameters?

Answer Source

The easiest way is by no passing the parameters in at all and instead doing variable capture.

public T GetOrAdd<T>(string key, Func<T> builder)
{
    var result = _someCache.Get<T>(key);
    if(result != null)
       return result;

    result = builder();
    _someCache.Add(key, result);
    return result;
}

used like

var address = myCache.GetOrAdd<Address>(AddressKeyBuilder(para1, para2), 
                                        () => Addressrepository.GetAddress(para1, para2)
                                       );

This is the same pattern ConcurrentDictionary uses for it's GetOrAdd method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download