2012-10-31 11:36:22 +06:00
using System ;
using System.Collections ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
2013-03-16 08:47:55 +06:00
using System.Text.RegularExpressions ;
2012-10-31 11:36:22 +06:00
using System.Web ;
using System.Web.Caching ;
2013-03-23 04:01:52 +06:00
using Umbraco.Core.Cache ;
2012-10-31 11:36:22 +06:00
using Umbraco.Core.Logging ;
namespace Umbraco.Core
{
/// <summary>
/// Class that is exposed by the ApplicationContext for application wide caching purposes
/// </summary>
2013-05-13 19:31:27 -10:00
public class CacheHelper
2012-10-31 11:36:22 +06:00
{
2013-03-23 04:01:52 +06:00
private readonly bool _enableCache ;
2013-08-08 19:46:58 +10:00
private readonly ICacheProvider _requestCache ;
private readonly ICacheProvider _nullRequestCache = new NullCacheProvider ( ) ;
private readonly ICacheProvider _staticCache ;
private readonly ICacheProvider _nullStaticCache = new NullCacheProvider ( ) ;
private readonly IRuntimeCacheProvider _httpCache ;
private readonly IRuntimeCacheProvider _nullHttpCache = new NullCacheProvider ( ) ;
2012-10-31 11:36:22 +06:00
2013-08-09 11:37:57 +10:00
/// <summary>
/// Creates a cache helper with disabled caches
/// </summary>
/// <returns></returns>
/// <remarks>
/// Good for unit testing
/// </remarks>
internal static CacheHelper CreateDisabledCacheHelper ( )
2013-05-13 19:31:27 -10:00
{
2013-08-09 11:37:57 +10:00
return new CacheHelper ( null , null , null , false ) ;
2013-05-13 19:31:27 -10:00
}
2013-08-09 11:37:57 +10:00
/// <summary>
/// Initializes a new instance for use in the web
/// </summary>
public CacheHelper ( )
: this (
new HttpRuntimeCacheProvider ( HttpRuntime . Cache ) ,
new StaticCacheProvider ( ) ,
new HttpRequestCacheProvider ( ( ) = > new HttpContextWrapper ( HttpContext . Current ) ) )
{
}
/// <summary>
/// Initializes a new instance for use in the web
/// </summary>
/// <param name="cache"></param>
public CacheHelper ( System . Web . Caching . Cache cache )
: this (
new HttpRuntimeCacheProvider ( cache ) ,
new StaticCacheProvider ( ) ,
new HttpRequestCacheProvider ( ( ) = > new HttpContextWrapper ( HttpContext . Current ) ) )
{
}
/// <summary>
/// Initializes a new instance based on the provided providers
/// </summary>
/// <param name="httpCacheProvider"></param>
/// <param name="staticCacheProvider"></param>
/// <param name="requestCacheProvider"></param>
public CacheHelper (
2013-08-08 19:46:58 +10:00
IRuntimeCacheProvider httpCacheProvider ,
ICacheProvider staticCacheProvider ,
2013-08-09 11:37:57 +10:00
ICacheProvider requestCacheProvider )
: this ( httpCacheProvider , staticCacheProvider , requestCacheProvider , true )
{
}
2013-09-18 10:05:44 +02:00
internal void ClearStaticCacheObjectTypes < T > ( Func < string , T , bool > predicate )
{
if ( _enableCache )
_staticCache . ClearCacheObjectTypes ( predicate ) ;
else
_nullStaticCache . ClearCacheObjectTypes ( predicate ) ;
}
2013-08-09 11:37:57 +10:00
/// <summary>
/// Private ctor used for creating a disabled cache helper
/// </summary>
/// <param name="httpCacheProvider"></param>
/// <param name="staticCacheProvider"></param>
/// <param name="requestCacheProvider"></param>
/// <param name="enableCache"></param>
private CacheHelper (
IRuntimeCacheProvider httpCacheProvider ,
ICacheProvider staticCacheProvider ,
2013-08-08 19:46:58 +10:00
ICacheProvider requestCacheProvider ,
bool enableCache )
2013-05-13 19:31:27 -10:00
{
2013-08-09 11:37:57 +10:00
if ( enableCache )
{
_httpCache = httpCacheProvider ;
_staticCache = staticCacheProvider ;
_requestCache = requestCacheProvider ;
}
else
{
_httpCache = null ;
_staticCache = null ;
_requestCache = null ;
}
2013-05-13 19:31:27 -10:00
_enableCache = enableCache ;
}
2013-08-08 19:46:58 +10:00
/// <summary>
2013-08-09 11:16:10 +10:00
/// Returns the current Request cache
2013-08-08 19:46:58 +10:00
/// </summary>
2013-08-09 11:16:10 +10:00
public ICacheProvider RequestCache
2013-08-08 19:46:58 +10:00
{
2013-08-09 11:16:10 +10:00
get { return _enableCache ? _requestCache : _nullRequestCache ; }
2013-08-08 19:46:58 +10:00
}
/// <summary>
2013-08-09 11:16:10 +10:00
/// Returns the current Runtime cache
2013-08-08 19:46:58 +10:00
/// </summary>
2013-08-09 11:16:10 +10:00
public ICacheProvider StaticCache
2013-08-08 19:46:58 +10:00
{
2013-08-09 11:16:10 +10:00
get { return _enableCache ? _staticCache : _nullStaticCache ; }
2013-08-08 19:46:58 +10:00
}
/// <summary>
2013-08-09 11:16:10 +10:00
/// Returns the current Runtime cache
2013-08-08 19:46:58 +10:00
/// </summary>
2013-08-09 11:16:10 +10:00
public IRuntimeCacheProvider RuntimeCache
{
get { return _enableCache ? _httpCache : _nullHttpCache ; }
}
2013-08-08 19:46:58 +10:00
2013-08-09 11:16:10 +10:00
#region Legacy Runtime / Http Cache accessors
2013-05-13 19:31:27 -10:00
/// <summary>
/// Clears the item in umbraco's runtime cache
/// </summary>
2013-08-09 11:16:10 +10:00
[Obsolete("Do not use this method, access the runtime cache from the RuntimeCache property")]
2012-10-31 11:36:22 +06:00
public void ClearAllCache ( )
2013-05-13 19:31:27 -10:00
{
2013-08-09 11:16:10 +10:00
if ( _enableCache = = false )
2013-05-13 19:31:27 -10:00
{
_nullHttpCache . ClearAllCache ( ) ;
}
else
{
_httpCache . ClearAllCache ( ) ;
}
2012-10-31 11:36:22 +06:00
}
/// <summary>
/// Clears the item in umbraco's runtime cache with the given key
/// </summary>
/// <param name="key">Key</param>
2013-08-09 11:16:10 +10:00
[Obsolete("Do not use this method, access the runtime cache from the RuntimeCache property")]
2012-10-31 11:36:22 +06:00
public void ClearCacheItem ( string key )
{
2013-08-09 11:16:10 +10:00
if ( _enableCache = = false )
2013-03-23 04:01:52 +06:00
{
2013-05-13 19:31:27 -10:00
_nullHttpCache . ClearCacheItem ( key ) ;
2013-03-23 04:01:52 +06:00
}
else
2012-10-31 11:36:22 +06:00
{
2013-03-23 04:01:52 +06:00
_httpCache . ClearCacheItem ( key ) ;
2012-10-31 11:36:22 +06:00
}
}
/// <summary>
/// Clears all objects in the System.Web.Cache with the System.Type name as the
/// input parameter. (using [object].GetType())
/// </summary>
/// <param name="typeName">The name of the System.Type which should be cleared from cache ex "System.Xml.XmlDocument"</param>
2013-08-09 11:16:10 +10:00
[Obsolete("Do not use this method, access the runtime cache from the RuntimeCache property")]
2012-10-31 11:36:22 +06:00
public void ClearCacheObjectTypes ( string typeName )
{
2013-08-09 11:16:10 +10:00
if ( _enableCache = = false )
2012-10-31 11:36:22 +06:00
{
2013-05-13 19:31:27 -10:00
_nullHttpCache . ClearCacheObjectTypes ( typeName ) ;
2012-10-31 11:36:22 +06:00
}
2013-03-23 04:01:52 +06:00
else
2012-10-31 11:36:22 +06:00
{
2013-03-23 04:01:52 +06:00
_httpCache . ClearCacheObjectTypes ( typeName ) ;
2012-10-31 11:36:22 +06:00
}
}
/// <summary>
2013-03-12 22:58:21 +04:00
/// Clears all objects in the System.Web.Cache with the System.Type specified
/// </summary>
2013-08-09 11:16:10 +10:00
[Obsolete("Do not use this method, access the runtime cache from the RuntimeCache property")]
2013-05-13 19:31:27 -10:00
public void ClearCacheObjectTypes < T > ( )
{
2013-08-09 11:16:10 +10:00
if ( _enableCache = = false )
2013-03-12 22:58:21 +04:00
{
2013-05-13 19:31:27 -10:00
_nullHttpCache . ClearCacheObjectTypes < T > ( ) ;
2013-03-12 22:58:21 +04:00
}
2013-03-23 04:01:52 +06:00
else
2013-03-12 22:58:21 +04:00
{
2013-03-23 04:01:52 +06:00
_httpCache . ClearCacheObjectTypes < T > ( ) ;
2013-03-12 22:58:21 +04:00
}
2013-05-13 19:31:27 -10:00
}
2013-03-12 22:58:21 +04:00
2013-05-13 19:31:27 -10:00
/// <summary>
2012-10-31 11:36:22 +06:00
/// Clears all cache items that starts with the key passed.
/// </summary>
/// <param name="keyStartsWith">The start of the key</param>
2013-08-09 11:16:10 +10:00
[Obsolete("Do not use this method, access the runtime cache from the RuntimeCache property")]
2012-10-31 11:36:22 +06:00
public void ClearCacheByKeySearch ( string keyStartsWith )
2013-05-13 19:31:27 -10:00
{
2013-08-09 11:16:10 +10:00
if ( _enableCache = = false )
2013-03-23 04:01:52 +06:00
{
2013-05-13 19:31:27 -10:00
_nullHttpCache . ClearCacheByKeySearch ( keyStartsWith ) ;
2013-03-23 04:01:52 +06:00
}
else
{
_httpCache . ClearCacheByKeySearch ( keyStartsWith ) ;
}
2013-05-13 19:31:27 -10:00
}
2013-03-12 22:58:21 +04:00
2013-03-16 08:47:55 +06:00
/// <summary>
/// Clears all cache items that have a key that matches the regular expression
/// </summary>
/// <param name="regexString"></param>
2013-08-09 11:16:10 +10:00
[Obsolete("Do not use this method, access the runtime cache from the RuntimeCache property")]
2013-05-13 19:31:27 -10:00
public void ClearCacheByKeyExpression ( string regexString )
{
2013-08-09 11:16:10 +10:00
if ( _enableCache = = false )
2013-03-23 04:01:52 +06:00
{
2013-05-13 19:31:27 -10:00
_nullHttpCache . ClearCacheByKeyExpression ( regexString ) ;
2013-03-23 04:01:52 +06:00
}
else
2013-03-16 08:47:55 +06:00
{
2013-03-23 04:01:52 +06:00
_httpCache . ClearCacheByKeyExpression ( regexString ) ;
2013-03-16 08:47:55 +06:00
}
2013-05-13 19:31:27 -10:00
}
2013-03-16 08:47:55 +06:00
2013-08-09 11:16:10 +10:00
[Obsolete("Do not use this method, access the runtime cache from the RuntimeCache property")]
2013-03-22 04:34:57 +06:00
public IEnumerable < T > GetCacheItemsByKeySearch < T > ( string keyStartsWith )
{
2013-08-09 11:16:10 +10:00
if ( _enableCache = = false )
2013-03-23 04:01:52 +06:00
{
2013-05-13 19:31:27 -10:00
return _nullHttpCache . GetCacheItemsByKeySearch < T > ( keyStartsWith ) ;
2013-03-23 04:01:52 +06:00
}
else
{
return _httpCache . GetCacheItemsByKeySearch < T > ( keyStartsWith ) ;
}
2013-03-22 04:34:57 +06:00
}
2013-05-13 19:31:27 -10:00
/// <summary>
2013-03-12 22:58:21 +04:00
/// Returns a cache item by key, does not update the cache if it isn't there.
/// </summary>
/// <typeparam name="TT"></typeparam>
/// <param name="cacheKey"></param>
/// <returns></returns>
2013-08-09 11:16:10 +10:00
[Obsolete("Do not use this method, access the runtime cache from the RuntimeCache property")]
2013-03-12 22:58:21 +04:00
public TT GetCacheItem < TT > ( string cacheKey )
{
2013-08-09 11:16:10 +10:00
if ( _enableCache = = false )
2013-03-23 04:01:52 +06:00
{
2013-05-13 19:31:27 -10:00
return _nullHttpCache . GetCacheItem < TT > ( cacheKey ) ;
2013-03-23 04:01:52 +06:00
}
else
2013-03-12 22:58:21 +04:00
{
2013-03-23 04:01:52 +06:00
return _httpCache . GetCacheItem < TT > ( cacheKey ) ;
2013-03-12 22:58:21 +04:00
}
2012-10-31 11:36:22 +06:00
}
2013-02-06 09:53:13 +06:00
/// <summary>
/// Gets (and adds if necessary) an item from the cache with all of the default parameters
/// </summary>
/// <typeparam name="TT"></typeparam>
/// <param name="cacheKey"></param>
/// <param name="getCacheItem"></param>
/// <returns></returns>
2013-08-09 11:16:10 +10:00
[Obsolete("Do not use this method, access the runtime cache from the RuntimeCache property")]
2013-02-06 09:53:13 +06:00
public TT GetCacheItem < TT > ( string cacheKey , Func < TT > getCacheItem )
{
2013-08-09 11:16:10 +10:00
if ( _enableCache = = false )
2013-03-23 04:01:52 +06:00
{
2013-05-13 19:31:27 -10:00
return _nullHttpCache . GetCacheItem < TT > ( cacheKey , getCacheItem ) ;
2013-03-23 04:01:52 +06:00
}
else
{
return _httpCache . GetCacheItem < TT > ( cacheKey , getCacheItem ) ;
}
2013-02-06 09:53:13 +06:00
}
/// <summary>
/// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW)
/// </summary>
/// <typeparam name="TT"></typeparam>
/// <param name="cacheKey"></param>
2013-03-12 22:58:21 +04:00
/// <param name="timeout">This will set an absolute expiration from now until the timeout</param>
2013-02-06 09:53:13 +06:00
/// <param name="getCacheItem"></param>
/// <returns></returns>
2013-08-09 11:16:10 +10:00
[Obsolete("Do not use this method, access the runtime cache from the RuntimeCache property")]
2012-10-31 11:36:22 +06:00
public TT GetCacheItem < TT > ( string cacheKey ,
TimeSpan timeout , Func < TT > getCacheItem )
{
2013-08-09 11:16:10 +10:00
if ( _enableCache = = false )
2013-03-23 04:01:52 +06:00
{
2013-08-09 11:16:10 +10:00
return _nullHttpCache . GetCacheItem < TT > ( cacheKey , getCacheItem , timeout ) ;
2013-03-23 04:01:52 +06:00
}
else
{
2013-08-09 11:16:10 +10:00
return _httpCache . GetCacheItem < TT > ( cacheKey , getCacheItem , timeout ) ;
2013-03-23 04:01:52 +06:00
}
2012-10-31 11:36:22 +06:00
}
2013-03-12 22:58:21 +04:00
/// <summary>
/// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW)
/// </summary>
/// <typeparam name="TT"></typeparam>
/// <param name="cacheKey"></param>
/// <param name="refreshAction"></param>
/// <param name="timeout">This will set an absolute expiration from now until the timeout</param>
/// <param name="getCacheItem"></param>
/// <returns></returns>
2013-08-09 11:16:10 +10:00
[Obsolete("Do not use this method, access the runtime cache from the RuntimeCache property")]
2012-10-31 11:36:22 +06:00
public TT GetCacheItem < TT > ( string cacheKey ,
CacheItemRemovedCallback refreshAction , TimeSpan timeout ,
Func < TT > getCacheItem )
{
2013-03-23 04:01:52 +06:00
if ( ! _enableCache )
{
2013-08-09 11:16:10 +10:00
return _nullHttpCache . GetCacheItem < TT > ( cacheKey , getCacheItem , timeout , removedCallback : refreshAction ) ;
2013-03-23 04:01:52 +06:00
}
else
{
2013-08-09 11:16:10 +10:00
return _httpCache . GetCacheItem < TT > ( cacheKey , getCacheItem , timeout , removedCallback : refreshAction ) ;
2013-03-23 04:01:52 +06:00
}
2012-10-31 11:36:22 +06:00
}
2013-03-12 22:58:21 +04:00
/// <summary>
/// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW)
/// </summary>
/// <typeparam name="TT"></typeparam>
/// <param name="cacheKey"></param>
/// <param name="priority"></param>
/// <param name="refreshAction"></param>
/// <param name="timeout">This will set an absolute expiration from now until the timeout</param>
/// <param name="getCacheItem"></param>
/// <returns></returns>
2013-08-09 11:16:10 +10:00
[Obsolete("Do not use this method, access the runtime cache from the RuntimeCache property")]
2012-10-31 11:36:22 +06:00
public TT GetCacheItem < TT > ( string cacheKey ,
CacheItemPriority priority , CacheItemRemovedCallback refreshAction , TimeSpan timeout ,
Func < TT > getCacheItem )
{
2013-08-09 11:16:10 +10:00
if ( _enableCache = = false )
2013-03-23 04:01:52 +06:00
{
2013-08-09 11:16:10 +10:00
return _nullHttpCache . GetCacheItem < TT > ( cacheKey , getCacheItem , timeout , false , priority , refreshAction ) ;
2013-03-23 04:01:52 +06:00
}
else
{
2013-08-09 11:16:10 +10:00
return _httpCache . GetCacheItem < TT > ( cacheKey , getCacheItem , timeout , false , priority , refreshAction ) ;
2013-03-23 04:01:52 +06:00
}
2012-10-31 11:36:22 +06:00
}
2013-03-12 22:58:21 +04:00
/// <summary>
/// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW)
/// </summary>
/// <typeparam name="TT"></typeparam>
/// <param name="cacheKey"></param>
/// <param name="priority"></param>
/// <param name="refreshAction"></param>
/// <param name="cacheDependency"></param>
/// <param name="timeout">This will set an absolute expiration from now until the timeout</param>
/// <param name="getCacheItem"></param>
/// <returns></returns>
2013-08-09 11:16:10 +10:00
[Obsolete("Do not use this method, we no longer support the caching overloads with references to CacheDependency, use the overloads specifying a file collection instead")]
2012-10-31 11:36:22 +06:00
public TT GetCacheItem < TT > ( string cacheKey ,
2013-05-13 19:31:27 -10:00
CacheItemPriority priority ,
CacheItemRemovedCallback refreshAction ,
CacheDependency cacheDependency ,
TimeSpan timeout ,
Func < TT > getCacheItem )
2012-10-31 11:36:22 +06:00
{
2013-08-09 11:16:10 +10:00
if ( _enableCache = = false )
2013-03-23 04:01:52 +06:00
{
2013-08-09 11:16:10 +10:00
return _nullHttpCache . GetCacheItem < TT > ( cacheKey , getCacheItem , timeout , false , priority , refreshAction , null ) ;
2013-03-23 04:01:52 +06:00
}
else
{
2013-08-09 11:16:10 +10:00
var cache = _httpCache as HttpRuntimeCacheProvider ;
if ( cache ! = null )
{
var result = cache . GetCacheItem ( cacheKey , ( ) = > getCacheItem ( ) , timeout , false , priority , refreshAction , cacheDependency ) ;
return result = = null ? default ( TT ) : result . TryConvertTo < TT > ( ) . Result ;
}
throw new InvalidOperationException ( "Cannot use this obsoleted overload when the current provider is not of type " + typeof ( HttpRuntimeCacheProvider ) ) ;
2013-03-23 04:01:52 +06:00
}
2012-10-31 11:36:22 +06:00
}
2013-04-03 22:34:40 +06:00
2013-04-03 23:39:51 +06:00
/// <summary>
/// Gets (and adds if necessary) an item from the cache
/// </summary>
/// <typeparam name="TT"></typeparam>
/// <param name="cacheKey"></param>
/// <param name="priority"></param>
/// <param name="cacheDependency"></param>
/// <param name="getCacheItem"></param>
/// <returns></returns>
2013-08-09 11:16:10 +10:00
[Obsolete("Do not use this method, we no longer support the caching overloads with references to CacheDependency, use the overloads specifying a file collection instead")]
2013-04-03 23:39:51 +06:00
public TT GetCacheItem < TT > ( string cacheKey ,
CacheItemPriority priority ,
CacheDependency cacheDependency ,
Func < TT > getCacheItem )
{
if ( ! _enableCache )
{
2013-08-09 11:16:10 +10:00
return _nullHttpCache . GetCacheItem < TT > ( cacheKey , getCacheItem , null , false , priority , null , null ) ;
2013-04-03 23:39:51 +06:00
}
else
{
2013-08-09 11:16:10 +10:00
var cache = _httpCache as HttpRuntimeCacheProvider ;
if ( cache ! = null )
{
var result = cache . GetCacheItem ( cacheKey , ( ) = > getCacheItem ( ) , null , false , priority , null , cacheDependency ) ;
return result = = null ? default ( TT ) : result . TryConvertTo < TT > ( ) . Result ;
}
throw new InvalidOperationException ( "Cannot use this obsoleted overload when the current provider is not of type " + typeof ( HttpRuntimeCacheProvider ) ) ;
2013-04-03 23:39:51 +06:00
}
}
2013-04-03 22:34:40 +06:00
/// <summary>
/// Inserts an item into the cache, if it already exists in the cache it will be replaced
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cacheKey"></param>
/// <param name="priority"></param>
/// <param name="getCacheItem"></param>
public void InsertCacheItem < T > ( string cacheKey ,
CacheItemPriority priority ,
Func < T > getCacheItem )
{
2013-08-09 11:16:10 +10:00
if ( _enableCache = = false )
2013-04-03 22:34:40 +06:00
{
2013-08-09 11:16:10 +10:00
_nullHttpCache . InsertCacheItem < T > ( cacheKey , getCacheItem , priority : priority ) ;
2013-04-03 22:34:40 +06:00
}
else
{
2013-08-09 11:16:10 +10:00
_httpCache . InsertCacheItem < T > ( cacheKey , getCacheItem , priority : priority ) ;
2013-04-03 22:34:40 +06:00
}
}
2013-05-13 19:31:27 -10:00
2013-03-12 22:58:21 +04:00
/// <summary>
/// Inserts an item into the cache, if it already exists in the cache it will be replaced
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cacheKey"></param>
/// <param name="priority"></param>
/// <param name="timeout">This will set an absolute expiration from now until the timeout</param>
/// <param name="getCacheItem"></param>
public void InsertCacheItem < T > ( string cacheKey ,
CacheItemPriority priority ,
TimeSpan timeout ,
Func < T > getCacheItem )
{
2013-08-09 11:16:10 +10:00
if ( _enableCache = = false )
2013-03-23 04:01:52 +06:00
{
2013-08-09 11:16:10 +10:00
_nullHttpCache . InsertCacheItem < T > ( cacheKey , getCacheItem , timeout , priority : priority ) ;
2013-03-23 04:01:52 +06:00
}
else
{
2013-08-09 11:16:10 +10:00
_httpCache . InsertCacheItem < T > ( cacheKey , getCacheItem , timeout , priority : priority ) ;
2013-03-23 04:01:52 +06:00
}
2013-03-12 22:58:21 +04:00
}
2013-05-13 19:31:27 -10:00
/// <summary>
/// Inserts an item into the cache, if it already exists in the cache it will be replaced
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cacheKey"></param>
/// <param name="priority"></param>
/// <param name="cacheDependency"></param>
/// <param name="timeout">This will set an absolute expiration from now until the timeout</param>
/// <param name="getCacheItem"></param>
2013-08-09 11:16:10 +10:00
[Obsolete("Do not use this method, we no longer support the caching overloads with references to CacheDependency, use the overloads specifying a file collection instead")]
2013-05-13 19:31:27 -10:00
public void InsertCacheItem < T > ( string cacheKey ,
CacheItemPriority priority ,
CacheDependency cacheDependency ,
TimeSpan timeout ,
Func < T > getCacheItem )
{
2013-08-09 11:16:10 +10:00
if ( _enableCache = = false )
2013-03-23 04:01:52 +06:00
{
2013-08-09 11:16:10 +10:00
_nullHttpCache . InsertCacheItem < T > ( cacheKey , getCacheItem , timeout , priority : priority , dependentFiles : null ) ;
2013-03-23 04:01:52 +06:00
}
else
{
2013-08-09 11:16:10 +10:00
var cache = _httpCache as HttpRuntimeCacheProvider ;
if ( cache ! = null )
{
cache . InsertCacheItem ( cacheKey , ( ) = > getCacheItem ( ) , timeout , false , priority , null , cacheDependency ) ;
}
throw new InvalidOperationException ( "Cannot use this obsoleted overload when the current provider is not of type " + typeof ( HttpRuntimeCacheProvider ) ) ;
2013-03-23 04:01:52 +06:00
}
2013-05-13 19:31:27 -10:00
}
2013-03-12 22:58:21 +04:00
/// <summary>
/// Inserts an item into the cache, if it already exists in the cache it will be replaced
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cacheKey"></param>
/// <param name="priority"></param>
/// <param name="refreshAction"></param>
/// <param name="cacheDependency"></param>
/// <param name="timeout">This will set an absolute expiration from now until the timeout</param>
/// <param name="getCacheItem"></param>
2013-08-09 11:16:10 +10:00
[Obsolete("Do not use this method, we no longer support the caching overloads with references to CacheDependency, use the overloads specifying a file collection instead")]
2013-05-13 19:31:27 -10:00
public void InsertCacheItem < T > ( string cacheKey ,
CacheItemPriority priority ,
CacheItemRemovedCallback refreshAction ,
CacheDependency cacheDependency ,
TimeSpan ? timeout ,
Func < T > getCacheItem )
{
2013-08-09 11:16:10 +10:00
if ( _enableCache = = false )
2013-03-23 04:01:52 +06:00
{
2013-08-09 11:16:10 +10:00
_nullHttpCache . InsertCacheItem < T > ( cacheKey , getCacheItem , timeout , false , priority , refreshAction , null ) ;
2013-03-23 04:01:52 +06:00
}
else
{
2013-08-09 11:16:10 +10:00
var cache = _httpCache as HttpRuntimeCacheProvider ;
if ( cache ! = null )
{
cache . InsertCacheItem ( cacheKey , ( ) = > getCacheItem ( ) , timeout , false , priority , refreshAction , cacheDependency ) ;
}
throw new InvalidOperationException ( "Cannot use this obsoleted overload when the current provider is not of type " + typeof ( HttpRuntimeCacheProvider ) ) ;
2013-03-23 04:01:52 +06:00
}
2013-05-13 19:31:27 -10:00
}
#endregion
2013-03-12 22:58:21 +04:00
}
2012-10-31 11:36:22 +06:00
}