public static T Put <T>(this RedisCache cache, string cacheKey, Func <T> sourceGetter, DateTime absoluteExpiration) where T : class { return(Get( (out T cacheData) => { cacheData = cache.Get <T>(cacheKey); return cacheData != null; }, sourceGetter, cacheData => cache.Set(cacheKey, cacheData, absoluteExpiration))); }
public static void Set <T>(this RedisCache cache, string key, T data, DateTime absoluteExpiration) { using (var stream = new MemoryStream()) { Serializer.Serialize(stream, data); cache.Set(key, stream.ToArray(), new DistributedCacheEntryOptions() { AbsoluteExpiration = absoluteExpiration }); } }
private void SetCache() { var cache = new RedisCache(_redisCacheOptions); var key = "CacheKey"; var value = Encoding.UTF8.GetBytes($"Hello From Redis Caching {DateTime.Now.ToLongTimeString()}"); cache.Set(key, value, new DistributedCacheEntryOptions()); var fromCache = Encoding.UTF8.GetString(cache.Get(key)); ViewData["FromCache"] = fromCache; }