using System; namespace PalGain.Core { /// /// Extensions /// public static class CacheExtensions { /// /// Variable (lock) to support thread-safe /// private static readonly object _syncObject = new object(); /// /// Get a cached item. If it's not in the cache yet, then load and cache it /// /// Type /// Cache manager /// Cache key /// Function to load item if it's not in the cache yet /// Cached item public static T Get(this ICacheManager cacheManager, string key, Func acquire) { return Get(cacheManager, key, 60, acquire); } /// /// Get a cached item. If it's not in the cache yet, then load and cache it /// /// Type /// Cache manager /// Cache key /// Cache time in minutes (0 - do not cache) /// Function to load item if it's not in the cache yet /// Cached item public static T Get(this ICacheManager cacheManager, string key, int cacheTime, Func acquire) { lock (_syncObject) { if (cacheManager.IsSet(key)) { return cacheManager.Get(key); } var result = acquire(); if (cacheTime > 0) cacheManager.Set(key, result, cacheTime); return result; } } } }