protected virtual Func<Meta<ICache>, Boolean> CacheTypeChecker(ECacheType cacheType) { return cache => cache.Metadata["CacheType"] .Equals(cacheType); }
public ActionResult Index2(EFileType type, string library, string version, string file = null, ECacheType renew = ECacheType.cache) { return(View("Index")); }
public ICacher GetCacher(ECacheType cacheType) { switch (cacheType) { case ECacheType.MemCache: return(new MemCache()); case ECacheType.WinCache: return(new MemCache()); default: return(null); } }
public void ProcessRequest(HttpContext context) { HttpRequest request = context.Request; string phyFilePath = request.PhysicalPath; if (!File.Exists(phyFilePath)) { string phyDirPath = Path.GetDirectoryName(phyFilePath); if (!string.IsNullOrWhiteSpace(phyDirPath)) { string fileName = Path.GetFileNameWithoutExtension(phyFilePath); if (!string.IsNullOrWhiteSpace(fileName)) { string fileExtension = Path.GetExtension(phyFilePath); if (!string.IsNullOrWhiteSpace(fileExtension)) { string[] fileNames = fileName.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries); if (fileNames != null) { int length = fileNames.Length; if (length > 1) { fileName = string.Join("_", fileNames, 0, length - 1); phyFilePath = Path.Combine(phyDirPath, string.Concat(fileName, fileExtension)); } Array.Clear(fileNames, 0, length); fileNames = null; } fileExtension = null; } fileName = null; } phyDirPath = null; } } if (File.Exists(phyFilePath)) { HttpResponse response = context.Response; ECacheType cacheType = ECacheType.None; string subfix = Path.GetExtension(phyFilePath); if (".css".Equals(subfix, StringComparison.CurrentCultureIgnoreCase)) { cacheType = ECacheType.Css; response.ContentType = "text/css"; } else if (".js".Equals(subfix, StringComparison.CurrentCultureIgnoreCase)) { cacheType = ECacheType.Js; response.ContentType = "application/x-javascript"; } #if !DEBUG if (cacheType != ECacheType.None) { //http://www.cnblogs.com/shanyou/archive/2012/05/01/2477500.html const int DAYS = 30; string ifModifiedSince = request.Headers["If-Modified-Since"]; if (!string.IsNullOrEmpty(ifModifiedSince) && TimeSpan.FromTicks(DateTime.Now.Ticks - DateTime.Parse(ifModifiedSince).Ticks).Days < DAYS) { response.StatusCode = (int)System.Net.HttpStatusCode.NotModified; response.StatusDescription = "Not Modified"; response.End(); return; } else { string fileContent = null; string ifNoneMatch = request.Headers["If-None-Match"]; string eTag = string.Format("\"{0}\"", this.GetFileMd5(string.Concat(phyFilePath, fileContent = this.GetFileContent(phyFilePath)))); if (!string.IsNullOrEmpty(ifNoneMatch) && ifNoneMatch.Equals(eTag)) { response.StatusCode = (int)System.Net.HttpStatusCode.NotModified; response.StatusDescription = "Not Modified"; response.End(); return; } else { HttpCachePolicy cache = response.Cache; //cache.SetLastModifiedFromFileDependencies();//程序自动读取文件的LastModified cache.SetLastModified(context.Timestamp); //因为静态文件的URL是特殊处理过,所以程序自动读取文件是在磁盘上找不到的,故改成手工代码设置文件的LastModified的方式。wangyunpeng,2016-4-12 //cache.SetETagFromFileDependencies();//程序自动读取文件的ETag cache.SetETag(eTag); //因为静态文件的URL是特殊处理过,所以程序自动读取文件是在磁盘上找不到的,故改成手工代码设置文件的ETag的方式。wangyunpeng,2016-4-12 cache.SetCacheability(HttpCacheability.Public); cache.SetExpires(DateTime.Now.AddDays(DAYS)); TimeSpan timeSpan = TimeSpan.FromDays(DAYS); cache.SetMaxAge(timeSpan); cache.SetProxyMaxAge(timeSpan); cache.SetValidUntilExpires(true); cache.SetSlidingExpiration(true); #region 压缩js和css文件 if (!string.IsNullOrWhiteSpace(fileContent)) { if (cacheType == ECacheType.Js) { string fileName = Path.GetFileName(phyFilePath); if (_MiniJsFiles.Contains(fileName, new StringComparer())) { fileContent = this.PackJavascript(fileContent);//ECMAScript压缩 } } //输出内容 response.ContentEncoding = _GlobalizationSection == null ? Encoding.UTF8 : _GlobalizationSection.ResponseEncoding; response.Write(fileContent); } #endregion } } } else {//直接输出文件 response.WriteFile(phyFilePath); } #else //直接输出文件 response.WriteFile(phyFilePath); #endif //取消GZIP压缩,IE7,IE8,Safari支持GZIP压缩显示css和js有问题。wangyunpeng if (this.IsAcceptEncoding(request, GZIP)) { response.Filter = new GZipStream(response.Filter, CompressionMode.Compress); this.SetResponseEncoding(response, GZIP); } else if (this.IsAcceptEncoding(request, DEFLATE)) { response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress); this.SetResponseEncoding(response, DEFLATE); } response.End(); } }