public T GetOrSet(string key, CacheSetter <T> setter) { if (items.ContainsKey(key)) { return(Get(key)); } return(Set(key, setter())); }
public static IList <T> BulkGetByGuidIdWithLocalCache <T>(IEnumerable <Guid> idList, Func <IEnumerable <Guid>, IList <T> > realGetter, Func <T, Guid> keyPropertyGetter = null) { CacheSetter cacheSetter = (k, o) => Cache.SetLocalCache(k, o); if (keyPropertyGetter == null) { keyPropertyGetter = obj => (Guid)Invoker.PropertyGet(obj, "Id"); } return(BulkGetWithCache <T, Guid>(idList, realGetter, keyPropertyGetter, GetFromLocalCache <T>, cacheSetter, id => "Get_" + id)); }
public static IList <T> BulkGetWithCache <T, K>(IEnumerable <K> idList, Func <IEnumerable <K>, IList <T> > realGetter, Func <T, K> keyPropertyGetter, CacheGetter <T> cacheGeter, CacheSetter cacheSetter, Func <K, string> keyGenerator) { if (idList == null || idList.Count() <= 0) { return(new List <T>(0)); } idList = idList.Distinct(); List <T> list = new List <T>(idList.Count()); List <K> noCacheIdList = new List <K>(idList.Count()); foreach (var id in idList) { string key = keyGenerator(id); bool find; var t = cacheGeter(key, out find); if (find) { list.Add(t); } else { noCacheIdList.Add(id); } } if (noCacheIdList.Count > 0) { var rst = realGetter(noCacheIdList); if (rst != null && rst.Count > 0) { list.AddRange(rst); foreach (T obj in rst) { K id = keyPropertyGetter(obj); string key = keyGenerator(id); cacheSetter(key, obj); } } } return(list); }
public static IList <T> BulkGetWithLocalCache <T, K>(IEnumerable <K> idList, Func <IEnumerable <K>, IList <T> > realGetter, Func <T, K> keyPropertyGetter, Func <K, string> keyGenerator) { CacheSetter cacheSetter = (k, o) => Cache.SetLocalCache(k, o); return(BulkGetWithCache <T, K>(idList, realGetter, keyPropertyGetter, GetFromLocalCache <T>, cacheSetter, keyGenerator)); }