/// <summary> /// Caches the insert. /// </summary> /// <param name="cacheObject"> /// The cache object. /// </param> /// <param name="key"> /// The cache key. /// </param> /// <param name="time"> /// The cache time. /// </param> /// <param name="cacheDependency"> /// The cache dependency. /// </param> public static void CacheInsert( this object cacheObject, string key, CachingTime time = CachingTime.Normal, CacheDependency cacheDependency = null) { Insert(HttpRuntime.Cache, key, cacheObject, time, cacheDependency); }
/// <summary> /// Caches the insert. /// </summary> /// <param name="cacheObject"> /// The cache object. /// </param> /// <param name="key"> /// The key. /// </param> /// <param name="time"> /// The time. /// </param> /// <param name="cacheDependency"> /// The cache dependency. /// </param> public static void CacheInsert( this IList <object> cacheObject, string key, CachingTime time = CachingTime.Normal, CacheDependency cacheDependency = null) { if (cacheObject != null && cacheObject.Count != 0) { Insert(HttpRuntime.Cache, key, cacheObject, time, cacheDependency); } }
/// <summary> /// Inserts the specified object to the cache. /// </summary> /// <param name="cache"> /// The cache. /// </param> /// <param name="key"> /// The cache key. /// </param> /// <param name="cacheObject"> /// The object to cache. /// </param> /// <param name="time"> /// The cache time. /// </param> /// <param name="cacheDependency"> /// The cache dependency. /// </param> public static void Insert( this Cache cache, string key, object cacheObject, CachingTime time = CachingTime.Normal, CacheDependency cacheDependency = null) { if (!string.IsNullOrWhiteSpace(key) && null != cacheObject) { cache.Insert( key, cacheObject, cacheDependency, DateTime.Now.AddMinutes((double)time), Cache.NoSlidingExpiration); } }
/// <summary> /// The cached method result. /// </summary> /// <param name="method"> /// The method. /// </param> /// <param name="keyPrefix"> /// The key prefix. /// </param> /// <param name="compoundKey"> /// The compound key. /// </param> /// <param name="cacheTime"> /// The cache time. /// </param> /// <param name="memberName"> /// The member name. /// </param> /// <param name="filePath"> /// The file path. /// </param> /// <param name="lineNumber"> /// The line number. /// </param> /// <typeparam name="TObject"> /// </typeparam> /// <returns> /// The TObject /// </returns> public static TObject CachedMethodResult <TObject>( Func <TObject> method, string keyPrefix, bool compoundKey = true, CachingTime cacheTime = CachingTime.BelowNormal, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0) where TObject : class { var result = default(TObject); var key = string.Empty; if (cacheTime > CachingTime.None && !Debugger.IsAttached) { key = compoundKey ? string.Concat( keyPrefix, ".CacheMethodResult-", method.GetMethodSignature(), '-', typeof(TObject), '-', memberName, '-', filePath, '-', lineNumber).ToLowerInvariant() : keyPrefix; result = HttpRuntime.Cache[key] as TObject; } if (result == null) { result = method(); if (cacheTime > CachingTime.None && !Debugger.IsAttached) { result.CacheInsert(key, cacheTime); } } return(result); }
/// <summary> /// Adds the specified key and object to the cache /// </summary> /// <param name="key">Cache key</param> /// <param name="data">Object instance</param> /// <param name="duration">Time duration</param> /// <param name="cachingTime">Cache settings <see cref="CachingTime"/></param> public void Add(string key, object data, TimeSpan duration, CachingTime cachingTime) { this.Add(key, data, duration); }
/// <summary> /// Set a new cache scan /// </summary> /// <param name="key">Cache key</param> /// <param name="data">Object instance</param> /// <param name="duration">Duration time</param> /// <param name="dependency">Cache dependency</param> /// <param name="cachingTime">Caching time</param> private void Add(string key, object data, TimeSpan duration, CacheDependency dependency, CachingTime cachingTime) { if (data == null) { return; } if (!this.IsEnabled) { return; } DateTime absoluteExpiration; TimeSpan slidingExpiration; switch (cachingTime) { case CachingTime.LongTermAbsolute: absoluteExpiration = Cache.NoAbsoluteExpiration; slidingExpiration = Cache.NoSlidingExpiration; break; case CachingTime.LongTermSliding: if (duration > TimeSpan.Zero) { absoluteExpiration = DateTime.UtcNow.Add(duration); } else { absoluteExpiration = DateTime.UtcNow.AddMinutes(20); } slidingExpiration = Cache.NoSlidingExpiration; break; case CachingTime.ShortTermSliding: absoluteExpiration = Cache.NoAbsoluteExpiration; if (duration > TimeSpan.Zero) { slidingExpiration = duration; } else { slidingExpiration = new TimeSpan(0, 0, 20, 0); } break; default: absoluteExpiration = Cache.NoAbsoluteExpiration; slidingExpiration = Cache.NoSlidingExpiration; break; } this.cache.Insert(key, data, dependency, absoluteExpiration, slidingExpiration, CacheItemPriority.AboveNormal, null); }