I have found code for Get/Set cache item, but I don't know how I can call this method, eg. how pass proper Func<T> getData
to this method?
public class Cache<T> : MemoryCache where T : class
{
public void Set(string cacheKey, T cacheItem, CacheItemPolicy policy = null)
{
//...
}
public bool TryGet(string cacheKey, out T returnItem)
{
//...
}
public bool TryGetOrSet( string cacheKey, Func<T> getData, out T returnData, CacheItemPolicy policy = null )
{
if( TryGet( cacheKey, out returnData ) )
return true;
lock( WriteLock )
{
if( TryGet( cacheKey, out returnData ) )
return true;
returnData = getData();
Set( cacheKey, returnData, policy );
}
return false;
}
}
For example let's assume that cache type is string
.
First question: By using method TryGetOrSet
how I can add item (key : userName variable, value: lastName variable) to cache? Of course when this item doesn't exists in cache
var cache = new Cache<string>("UserInfo");
var userName = "test";
var lastName = "test2";
TryGetOrSet(userName, ???, out var _) // <- what should I pass to Func<T>?