I would like to save in Cache an object with a constant Key.
public TItem Set<TItem>(string key, TItem value)
{
return = _memoryCache.Set(key, value);
}
I get my object with a function like:
public TItem Get(
TPrimaryKey id,
params object[] args,
bool byPassCaching = false
)
{
if (byPassCaching || !_cacheManager.TryGetValue<TItem>(GetKey(id, args), out TItem result)){
item = [FUNCTION TO GET ITEM];
_cacheManager.Set(GetKey(id, args), item)
}
return item;
}
I would like to generate a constant key for a TItem, Id (int) and some params (object[] args).
Here is my function to generate the constant key:
internal static string GetKey<TItem>(int id, params object[] args)
{
return string.Format("{0}#{1}[{2}]", typeof(TItem).FullName, id, args?[FUNCTION TO CALL]);
}
[FUNCTION TO CALL] is the function I m looking for.
So next time I call the function with the same parameter, I will have the same key.
For exemple
GetKey<MyClass>(1, null) => "MyApp.MyClass#1[]"
GetKey<MyClass>(1, "Hello", "World") => "MyApp.MyClass#1[sdas5d1as5d4sd8]"
GetKey<MyClass>(1, "Hello", "World") => "MyApp.MyClass#1[sdas5d1as5d4sd8]"
GetKey<MyClass>(1, item => item.Include(s => s.Categories)) => "MyApp.MyClass#1[asdasdasd87wqw]"
At the beginning, I thought to use GetHashCode, but the int generated is always different.
How can I do that?