How to invalidate cached property?

616 views Asked by At

In my ASP.NET web site I persist objects retrieved from DB for some time and make sure that the only single instance of object is created per DB record.

I also use [InstanceCache] to cache calculated value for one of business objects, object is kept in cache for the duration of user session.

Today, though, I came across the use-case when I would like to invalidate cached value.

How can I do this for properties that are cached using [InstanceCache]?

Please advise.

Thank you.

1

There are 1 answers

1
Alex On

I think you may be able to do that by using one of the ClearCache static methods of the CacheAspect class, that allow clearing the cache at different levels of granularity:

public static void ClearCache(MethodInfo methodInfo);
public static void ClearCache(Type declaringType, string methodName, params Type[] types);
public static void ClearCache(Type declaringType, string methodName, Type[] types, object[] values);
public static void ClearCache(Type declaringType);
public static void ClearCache();

If you want to clear the cache only for the specific [InstanceCache] property, you will need to get its MethodInfo (for the "get" method of the property I think) using reflection and pass that along to the first variant.

This seems to work for regular [Cache] attribute annotations, so if clearing the cache for [InstanceCache] items is at all supported, this would be the route to go.