Beispiel #1
0
        public static T GetOrCreate <T>(
            this IDbCache cache,
            string key,
            TimeSpan?expirationRelativeToNow,
            Func <T> factory)
        {
            if (!cache.TryGetValue(key, out object result))
            {
                result = factory();
                cache.CreateEntry(key, result, expirationRelativeToNow);
            }

            return((T)result);
        }
Beispiel #2
0
        public static async Task <T> GetOrCreateAsync <T>(
            this IDbCache cache,
            string key,
            TimeSpan?expirationRelativeToNow,
            Func <Task <T> > factory)
        {
            if (!cache.TryGetValue(key, out object result))
            {
                // Async version of GetOrCreate is necessary, because
                // we want to store the result of Task<T> which is T.
                result = await factory();

                cache.CreateEntry(key, result, expirationRelativeToNow);
            }

            return((T)result);
        }