public static T Get <T>(string cacheKey, Func <T> getData, TimeSpan expiration, bool loadFromCache = true) where T : class { // Modify the key cacheKey = ModifyKey(cacheKey); T data = null; if (loadFromCache && Enabled) { // Get the data data = CacheChannel.Get <T>(cacheKey); } if (data == null) { // Get data using the given function data = getData(); } // Check for the data if (data != null) { // Add the data to cache Add(cacheKey, data, expiration); } return(data); }
public static void Add(string cacheKey, object data) { if (!Enabled) { return; } // Modify the key cacheKey = ModifyKey(cacheKey); try { // Add the cache key AddCacheKeyToCollection(cacheKey); // Set the expiration time TimeSpan expiration = CacheConfig.DefaultExpirationTime; // Insert the cache item CacheChannel.Set(cacheKey, data, expiration); } catch (Exception exc) { // Log the error LogService.Log(LogMode.Error, "Can not insert the item into the cache", exc, data: cacheKey); } }
public static void Remove(string cacheKey) { if (!Enabled) { return; } // Modify the key cacheKey = ModifyKey(cacheKey); try { // Remove the cache item CacheChannel.Remove(cacheKey); } catch (Exception exc) { LogService.Log(LogMode.Error, "Can not remove the cache item", exc, cacheKey); } }
private static void AddCacheKeyToCollection(string cacheKey) { // Get current cache keys List <string> allCacheKeys = CacheChannel.Get <List <string> >("AllCacheKeys"); if (allCacheKeys == null) { allCacheKeys = new List <string>(); } // Check the key existence var key = ModifyKey(cacheKey); //if (key.Length > 150) //{ // key = SecurityService.Encrypt(key, EncryptorType.MD5Encryption); //} if (!allCacheKeys.Contains(key)) { allCacheKeys.Add(key); CacheChannel.Set("AllCacheKeys", allCacheKeys); } }
public static List <string> GetAllCacheKeys() { return(CacheChannel.Get <List <string> >("AllCacheKeys")); }