-
Notifications
You must be signed in to change notification settings - Fork 18
TimedCache interceptor
The TimedCache interceptor can be used to cache a method's return value for a period of time. The time period can be defined directly in the attribute. The default time-span is 30 minutes.
The TimeCache interceptor is only available for the Net45x and Net46x version of Cauldron.
The cache is implemented using the default System.Runtime.Caching.MemoryCache from the .NET Framework.
The key required to identify the cached value is a hash (Sha256) of the method's full name and the method's arguments. The passed arguments needs to have a proper implementation of Object.GetHashCode and the Object.ToString (not the default type name) method in order to guarantee the uniqueness of the key.
A method with e.g. 2 arguments, both accepting null, will generate a different key depending on which of the values is null.
For example:
[TimedCache]
public string GetData(long? arg1, long? arg2)
{
...
}
The method above can be invoked like following:
GetData(null, null); // 1
GetData(null, 20); // 2
GetData(20, null); // 3
Call 2 and 3 will generate different keys.
[TimedCache(2000 /* 2000 seconds */)]
public UserDto GetUserDto(string username, int userId)
{
...
}
[TimedCache]
public async Task<AddressDataDto> GetSenderAddressDataAsync(IUser sender)
{
...
}
[TimedCache(10)]
public IEnumerable<int> GetEverything()
{
...
}
The cache can be reseted by invoking the TimedCacheChangeMonitor.Clear() method.
- It does not work on methods that returns void and Task
- It does not work on async methods without the async keyword
- Unable to dig for gold