/// <summary> /// Sets a value in cache using the provided settings /// </summary> public void Set <TValue>(string key, TValue value, ICacheExpirationSettings cacheExpirationSettings = null) where TValue : class { if (this.IsInCache(key)) { this.Remove(key); } if (cacheExpirationSettings == null) { cacheExpirationSettings = AbsoluteCacheExpirationSettings.Make(5); } if (value != null) { this.Context.Cache.Insert(key, value, null, cacheExpirationSettings.AbsoluteExpiration, cacheExpirationSettings.SlidingExpiration, cacheExpirationSettings.Priority, null); } }
/// <summary> /// Gets a value from cache if it exists, otherwise invokes the func setting its result in Cache /// </summary> public TValue Get <TValue>(string key, ICacheExpirationSettings cacheExpirationSettings = null, Func <TValue> retrieveFunc = null) where TValue : class { if (this.IsInCache(key)) { return(this.Context.Cache.Get(key) as TValue); } if (retrieveFunc == null) { return(null); } // Invoke Func to Get Data var value = retrieveFunc.Invoke(); if (value != null) { this.Set(key, value, cacheExpirationSettings); } return(value); }