/// <summary> /// 创建一个标准缓存策略 /// </summary> /// <param name="context">请求上下文</param> /// <param name="token">缓存标示</param> /// <param name="provider">缓存策略提供程序</param> /// <param name="duration">缓存持续时间</param> /// <param name="enableClientCache">是否启用客户端缓存</param> /// <param name="localcacheVirtualPath">静态文件缓存虚拟路径</param> /// <param name="enableMemoryCache">是否启用内存缓存</param> public StandardCachePolicy( HttpContextBase context, CacheToken token, ICachePolicyProvider provider, TimeSpan duration, bool enableClientCache, string localcacheVirtualPath, bool enableMemoryCache ) : base( context, token, provider ) { if ( context == null ) throw new ArgumentNullException( "context" ); if ( provider == null ) throw new ArgumentNullException( "provider" ); Duration = duration; EnableClientCache = enableClientCache; if ( localcacheVirtualPath != null ) { var physicalPath = context.Server.MapPath( localcacheVirtualPath ); CacheStorageProvider = new StaticFileCacheStorageProvider( physicalPath, enableMemoryCache ); } else { CacheStorageProvider = new WebCacheStorageProvider( HttpRuntime.Cache ); } }
/// <summary> /// 创建一个标准缓存策略 /// </summary> /// <param name="context">请求上下文</param> /// <param name="token">缓存标示</param> /// <param name="provider">缓存策略提供程序</param> /// <param name="duration">缓存持续时间</param> /// <param name="enableClientCache">是否启用客户端缓存</param> /// <param name="localcacheVirtualPath">静态文件缓存虚拟路径</param> /// <param name="enableMemoryCache">是否启用内存缓存</param> public StandardCachePolicy(HttpContextBase context, CacheToken token, ICachePolicyProvider provider, TimeSpan duration, bool enableClientCache, string localcacheVirtualPath, bool enableMemoryCache) : base(context, token, provider) { if (context == null) { throw new ArgumentNullException("context"); } if (provider == null) { throw new ArgumentNullException("provider"); } Duration = duration; EnableClientCache = enableClientCache; if (localcacheVirtualPath != null) { var physicalPath = context.Server.MapPath(localcacheVirtualPath); CacheStorageProvider = new StaticFileCacheStorageProvider(physicalPath, enableMemoryCache); } else { CacheStorageProvider = new WebCacheStorageProvider(HttpRuntime.Cache); } }
/// <summary> /// 创建持久化输出流 /// </summary> /// <param name="token"></param> /// <returns></returns> protected override Stream CreateSaveStream(CacheToken token) { Directory.CreateDirectory(PhysicalPath); var path = Path.Combine(PhysicalPath, CreateFilename(token)); return(File.OpenWrite(path)); }
/// <summary> /// 缓存项是否还有效 /// </summary> /// <param name="token">缓存标识,检查检查缓存项的标识是否与提供的一致,否则也认为缓存项无效</param> /// <returns>是否有效</returns> public bool IsValid(CacheToken token) { if (IsValid()) { return(CacheToken == token); } return(false); }
/// <summary> /// 获取缓存项 /// </summary> /// <param name="token"></param> /// <returns></returns> public virtual CacheItem GetCacheItem(CacheToken token) { var cacheItem = LoadCacheItem(token); if (cacheItem != null && cacheItem.IsValid(token))//检查缓存是否已过期 { return(cacheItem); } return(null); }
/// <summary> /// 获取缓存项 /// </summary> /// <param name="token"></param> /// <returns></returns> public virtual CacheItem GetCacheItem( CacheToken token ) { var cacheItem = LoadCacheItem( token ); if ( cacheItem != null && cacheItem.IsValid( token ) )//检查缓存是否已过期 return cacheItem; return null; }
/// <summary> /// 创建一个缓存项 /// </summary> /// <param name="token">缓存项的缓存依据</param> /// <param name="cached">缓存的数据</param> /// <param name="duration">最大缓存时间</param> public CacheItem( CacheToken token, ICachedResponse cached, TimeSpan duration ) { CacheToken = token; CachedResponse = cached; var shake = Math.Min( DurationWhenCreated.TotalMilliseconds / 50, maxShake.TotalMilliseconds ); var random = new Random( DateTime.Now.Millisecond ); var offset = TimeSpan.FromMilliseconds( random.NextDouble() * shake ); Expiration = DateTime.UtcNow + duration + offset; DurationWhenCreated = duration; }
/// <summary> /// 创建持久化输入流 /// </summary> /// <param name="token"></param> /// <returns></returns> protected override Stream CreateLoadStream(CacheToken token) { var path = Path.Combine(PhysicalPath, CreateFilename(token)); if (File.Exists(path)) { return(File.OpenRead(path)); } else { return(null); } }
/// <summary> /// 创建一个缓存项 /// </summary> /// <param name="token">缓存项的缓存依据</param> /// <param name="cached">缓存的数据</param> /// <param name="duration">最大缓存时间</param> public CacheItem(CacheToken token, ICachedResponse cached, TimeSpan duration) { CacheToken = token; CachedResponse = cached; var shake = Math.Min(DurationWhenCreated.TotalMilliseconds / 50, maxShake.TotalMilliseconds); var random = new Random(DateTime.Now.Millisecond); var offset = TimeSpan.FromMilliseconds(random.NextDouble() * shake); Expiration = DateTime.UtcNow + duration + offset; DurationWhenCreated = duration; }
/// <summary> /// 根据缓存标识创建静态缓存的文件名 /// </summary> /// <param name="token">缓存标识</param> /// <returns></returns> protected virtual string CreateFilename(CacheToken token) { var cacheKey = token.CacheKey(); var name = invalidPathCharactor.Replace(token.CacheKey(), ""); if (name.Length > 20) { name = name.Substring(0, 20); } var hash = FormsAuthentication.HashPasswordForStoringInConfigFile(cacheKey, "SHA1"); return(name + "_" + hash + ".cache"); }
/// <summary> /// 从 WebCache 获取缓存的响应 /// </summary> /// <param name="cache"></param> /// <param name="token"></param> /// <returns></returns> public static ICachedResponse GetCachedResponse(this Cache cache, CacheToken token) { var cacheItem = cache.GetCacheItem(token); if (cacheItem == null) { return(null); } else { return(cacheItem.CachedResponse); } }
/// <summary> /// 创建一个标准缓存策略 /// </summary> /// <param name="context">请求上下文</param> /// <param name="token">缓存标示</param> /// <param name="provider">缓存策略提供程序</param> /// <param name="duration">缓存持续时间</param> /// <param name="enableClientCache">是否启用客户端缓存</param> /// <param name="storageProvider">缓存储存提供程序</param> public StandardCachePolicy( HttpContextBase context, CacheToken token, ICachePolicyProvider provider, TimeSpan duration, bool enableClientCache, ICacheStorageProvider storageProvider ) : base(context, token, provider) { if ( context == null ) throw new ArgumentNullException( "context" ); if ( provider == null ) throw new ArgumentNullException( "provider" ); Duration = duration; EnableClientCache = enableClientCache; CacheStorageProvider = storageProvider; }
/// <summary> /// 从持久化设备中加载缓存项 /// </summary> /// <param name="token"></param> /// <returns></returns> protected virtual CacheItem LoadCacheItem( CacheToken token ) { using ( var stream = CreateLoadStream( token ) ) { if ( stream == null ) return null; var cacheItem = Deserialize( stream ); if ( cacheItem != null && cacheItem.IsValid( token ) ) return cacheItem; else return null; } }
/// <summary> /// 创建一个标准缓存策略 /// </summary> /// <param name="context">请求上下文</param> /// <param name="token">缓存标示</param> /// <param name="provider">缓存策略提供程序</param> /// <param name="duration">缓存持续时间</param> /// <param name="enableClientCache">是否启用客户端缓存</param> /// <param name="storageProvider">缓存储存提供程序</param> public StandardCachePolicy(HttpContextBase context, CacheToken token, ICachePolicyProvider provider, TimeSpan duration, bool enableClientCache, ICacheStorageProvider storageProvider) : base(context, token, provider) { if (context == null) { throw new ArgumentNullException("context"); } if (provider == null) { throw new ArgumentNullException("provider"); } Duration = duration; EnableClientCache = enableClientCache; CacheStorageProvider = storageProvider; }
/// <summary> /// 获取缓存项 /// </summary> /// <param name="token">缓存标识</param> /// <returns></returns> public override CacheItem GetCacheItem(CacheToken token) { if (EnableMemoryCache) { var cacheItem = HttpRuntime.Cache.GetCacheItem(token); if (cacheItem != null) { if (!cacheItem.IsValid())//缓存已过期 { return(null); } return(cacheItem); } } return(base.GetCacheItem(token)); }
/// <summary> /// 从持久化设备中加载缓存项 /// </summary> /// <param name="token"></param> /// <returns></returns> protected virtual CacheItem LoadCacheItem(CacheToken token) { using (var stream = CreateLoadStream(token)) { if (stream == null) { return(null); } var cacheItem = Deserialize(stream); if (cacheItem != null && cacheItem.IsValid(token)) { return(cacheItem); } else { return(null); } } }
/// <summary> /// 创建持久化输入流 /// </summary> /// <param name="token"></param> /// <returns></returns> protected abstract Stream CreateLoadStream( CacheToken token );
public MyCachePolicy( HttpContextBase context, CacheToken cacheToken, ICachePolicyProvider provider ) : base(context, cacheToken, provider, TimeSpan.FromDays( 1 ), true, "~/StaticCaches", true) { }
/// <summary> /// 创建一个标准缓存策略 /// </summary> /// <param name="context">请求上下文</param> /// <param name="token">缓存标示</param> /// <param name="provider">缓存策略提供程序</param> /// <param name="duration">缓存持续时间</param> /// <param name="enableClientCache">是否启用客户端缓存</param> public StandardCachePolicy(HttpContextBase context, CacheToken token, ICachePolicyProvider provider, TimeSpan duration, bool enableClientCache) : this(context, token, provider, duration, enableClientCache, new WebCacheStorageProvider()) { }
/// <summary> /// 从 WebCache 获取一个缓存项 /// </summary> /// <param name="cache"></param> /// <param name="token"></param> /// <returns></returns> public static CacheItem GetCacheItem(this Cache cache, CacheToken token) { return(cache.Get(token.CacheKey()) as CacheItem); }
/// <summary> /// 创建持久化输入流 /// </summary> /// <param name="token"></param> /// <returns></returns> protected abstract Stream CreateLoadStream(CacheToken token);
/// <summary> /// 创建持久化输出流 /// </summary> /// <param name="token"></param> /// <returns></returns> protected abstract Stream CreateSaveStream(CacheToken token);
/// <summary> /// 创建 CachePolicy 实例 /// </summary> /// <param name="context">HTTP 上下文</param> /// <param name="token">缓存标识</param> /// <param name="provider">缓存策略提供程序</param> public CachePolicy( HttpContextBase context, CacheToken token, ICachePolicyProvider provider ) { HttpContext = context; CacheToken = token; Provider = provider; }
/// <summary> /// 根据缓存标识创建静态缓存的文件名 /// </summary> /// <param name="token">缓存标识</param> /// <returns></returns> protected virtual string CreateFilename( CacheToken token ) { var cacheKey = token.CacheKey(); var name = invalidPathCharactor.Replace( token.CacheKey(), "" ); if ( name.Length > 20 ) name = name.Substring( 0, 20 ); var hash = FormsAuthentication.HashPasswordForStoringInConfigFile( cacheKey, "SHA1" ); return name + "_" + hash + ".cache"; }
/// <summary> /// 创建持久化输出流 /// </summary> /// <param name="token"></param> /// <returns></returns> protected override Stream CreateSaveStream( CacheToken token ) { Directory.CreateDirectory( PhysicalPath ); var path = Path.Combine( PhysicalPath, CreateFilename( token ) ); return File.OpenWrite( path ); }
/// <summary> /// 缓存项是否还有效 /// </summary> /// <param name="token">缓存标识,检查检查缓存项的标识是否与提供的一致,否则也认为缓存项无效</param> /// <returns>是否有效</returns> public bool IsValid( CacheToken token ) { if ( IsValid() ) return CacheToken == token; return false; }
/// <summary> /// 创建 CachePolicy 实例 /// </summary> /// <param name="context">HTTP 上下文</param> /// <param name="token">缓存标识</param> /// <param name="provider">缓存策略提供程序</param> public CachePolicy(HttpContextBase context, CacheToken token, ICachePolicyProvider provider) { HttpContext = context; CacheToken = token; Provider = provider; }
/// <summary> /// 从 WebCache 获取一个缓存项 /// </summary> /// <param name="cache"></param> /// <param name="token"></param> /// <returns></returns> public static CacheItem GetCacheItem( this Cache cache, CacheToken token ) { return cache.Get( token.CacheKey() ) as CacheItem; }
/// <summary> /// 从 WebCache 获取缓存的响应 /// </summary> /// <param name="cache"></param> /// <param name="token"></param> /// <returns></returns> public static ICachedResponse GetCachedResponse( this Cache cache, CacheToken token ) { var cacheItem = cache.GetCacheItem( token ); if ( cacheItem == null ) return null; else return cacheItem.CachedResponse; }
/// <summary> /// 创建一个标准缓存策略 /// </summary> /// <param name="context">请求上下文</param> /// <param name="token">缓存标示</param> /// <param name="provider">缓存策略提供程序</param> /// <param name="duration">缓存持续时间</param> /// <param name="enableClientCache">是否启用客户端缓存</param> public StandardCachePolicy( HttpContextBase context, CacheToken token, ICachePolicyProvider provider, TimeSpan duration, bool enableClientCache ) : this(context, token, provider, duration, enableClientCache, new WebCacheStorageProvider()) { }
/// <summary> /// 获取一个缓存项 /// </summary> /// <param name="token">缓存标示</param> /// <returns></returns> public CacheItem GetCacheItem(CacheToken token) { return(_cache.GetCacheItem(token)); }
/// <summary> /// 获取缓存项 /// </summary> /// <param name="token">缓存标识</param> /// <returns></returns> public override CacheItem GetCacheItem( CacheToken token ) { if ( EnableMemoryCache ) { var cacheItem = HttpRuntime.Cache.GetCacheItem( token ); if ( cacheItem != null ) { if ( !cacheItem.IsValid() )//缓存已过期 return null; return cacheItem; } } return base.GetCacheItem( token ); }
/// <summary> /// 创建持久化输出流 /// </summary> /// <param name="token"></param> /// <returns></returns> protected abstract Stream CreateSaveStream( CacheToken token );
/// <summary> /// 创建持久化输入流 /// </summary> /// <param name="token"></param> /// <returns></returns> protected override Stream CreateLoadStream( CacheToken token ) { var path = Path.Combine( PhysicalPath, CreateFilename( token ) ); if ( File.Exists( path ) ) return File.OpenRead( path ); else return null; }