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>?
Tdefined? On the class containingTryGetOrSet?T(as invoid TryGetOrSet<T>(...), or is it in the type declaration?) and you have avoidmethod returning abool?() => "my string"or() => CallStringReturningMethod()public class Cache<T> : MemoryCache where T : classFunc<T>at all? You don't know how to pass one to a method? You have found somebody doing it in a way you don't understand? It might be easiest to show us not just the code you are trying to call but more importantly the code where you are trying to call it so we can advise you. As it stands its going to be quite tricky to say exactly how to create the rightFunc<T>without knowing what you expect thatFuncto do...