ebb
ebb

Reputation: 9377

Distributed cache key generator

Having multiple processes, using the same cache (memcached in this case). Those processes are:

How could I generate the cache keys?

I've been thinking about using a strategy that takes the name of the assembly, full type name, method name and it's parameters. That would look something like:

public static string GenerateKeyFor<T>(Expression<Action<T>> expr)
{
    var method = Symbols.GetMethodInfo(expr);
    var parameters = Symbols.GetParameters(expr);

    var sb = new StringBuilder();

    sb.Append(method.DeclaringType.FullName)
        .Append(" - ")
        .Append(method.Name)
        .Append(" - ");

    return parameters
        .Aggregate(sb, AddParams)
        .ToString();
}

Usage:

var key = CacheUtils.GenerateKeyFor<HomeController>(x => x.List());

However, if I then want to evict ex. a ActionResult in the ASP.NET MVC application, from the Console application, then I would need to reference the ASP.NET MVC application in my Console application, in order to could reference the HomeController type. Is there a better way to generate keys for such scenarios?

Upvotes: 2

Views: 970

Answers (1)

Roger
Roger

Reputation: 1950

Is the console application some monitoring tool for the web app? If so, you can create a public API on the web app evicting cache entries and call this/these from the console app on a use case basis (instead of evicting specific entries).

If you really need to evict "both direction" or the number of "use case evicts" are too many, use interfaces of the types/methods you want to cache. Put these interface in a assembly that both/all your clients references. Then build keys of these interface methods instead. Or, as an alternative, use constant strings as cache keys (instead of building them dynamically) and put these in a common assembly.

Upvotes: 1

Related Questions