public override CacheDependency GetCacheDependency(
            string virtualPath,
            IEnumerable virtualPathDependencies,
            DateTime utcStart)
        {
            bool isLess;

            if (IsStyleSheet(virtualPath, out isLess))
            {
                if (isLess)
                {
                    // the LESS HTTP handler made the call
                    // [...]
                }
                else
                {
                    // the Bundler made the call
                    var bundle = BundleTable.Bundles.GetBundleFor(virtualPath);
                    if (bundle == null)
                    {
                        return(_previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart));
                    }
                }

                var arrPathDependencies = virtualPathDependencies.Cast <string>().ToArray();

                // determine the virtual themevars.less import reference
                var themeVarsFile = arrPathDependencies.Where(x => ThemeHelper.PathIsThemeVars(x)).FirstOrDefault();

                if (themeVarsFile.IsEmpty())
                {
                    // no themevars import... so no special considerations here
                    return(_previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart));
                }

                // exclude the themevars import from the file dependencies list,
                // 'cause this one cannot be monitored by the physical file system
                var fileDependencies = arrPathDependencies.Except(new string[] { themeVarsFile });

                if (arrPathDependencies.Any())
                {
                    int    storeId   = ResolveCurrentStoreId();
                    string themeName = ResolveCurrentThemeName();
                    // invalidate the cache when variables change
                    string cacheKey        = AspNetCache.BuildKey(FrameworkCacheConsumer.BuildThemeVarsCacheKey(themeName, storeId));
                    var    cacheDependency = new CacheDependency(fileDependencies.Select(x => HostingEnvironment.MapPath(x)).ToArray(), new string[] { cacheKey }, utcStart);
                    return(cacheDependency);
                }

                return(null);
            }

            return(_previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart));
        }
Exemple #2
0
 public override CacheDependency GetCacheDependency(
     string virtualPath,
     IEnumerable virtualPathDependencies,
     DateTime utcStart)
 {
     return(IsEmbeddedView(virtualPath)
                ? null
                : _DefaultProvider.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart));
 }
Exemple #3
0
 public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
 {
     if (IsEmbeddedPath(virtualPath))
     {
         return(null);
     }
     else
     {
         return(_previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart));
     }
 }
 /// <inheritdoc />
 public override CacheDependency GetCacheDependency(
     string virtualPath,
     IEnumerable virtualPathDependencies,
     DateTime utcStart)
 {
     if (this.IsEmbeddedPath(virtualPath))
     {
         return(_cacheDependencyBuilder.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart));
     }
     return(_previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart));
 }
Exemple #5
0
 GetCacheDependency(string virtualPath,
                    System.Collections.IEnumerable virtualPathDependencies,
                    DateTime utcStart)
 {
     if (IsAppResourcePath(virtualPath))
     {
         return(null);
     }
     else
     {
         return(parent.GetCacheDependency(virtualPath,
                                          virtualPathDependencies, utcStart));
     }
 }
        void ListenForChanges(string virtualPath)
        {
            // Get a CacheDependency from the BuildProvider,
            // so that we know anytime something changes
            var virtualPathDependencies = new List <string>();

            virtualPathDependencies.Add(virtualPath);
            CacheDependency cacheDependency = _vpp.GetCacheDependency(
                virtualPath, virtualPathDependencies, DateTime.UtcNow);

            HttpRuntime.Cache.Insert(virtualPath /*key*/,
                                     virtualPath /*value*/,
                                     cacheDependency,
                                     Cache.NoAbsoluteExpiration,
                                     Cache.NoSlidingExpiration,
                                     CacheItemPriority.NotRemovable,
                                     new CacheItemRemovedCallback(OnConfigFileChanged));
        }
 internal CacheDependency CreateCacheDependency(CacheDependencyType dependencyType, CacheDependency dependency)
 {
     if (this._dependencies != null)
     {
         if ((dependencyType == CacheDependencyType.Files) || (dependencyType == CacheDependencyType.CacheItems))
         {
             foreach (ResponseDependencyInfo info in this._dependencies)
             {
                 using (CacheDependency dependency2 = dependency)
                 {
                     if (dependencyType == CacheDependencyType.Files)
                     {
                         dependency = new CacheDependency(0, info.items, null, dependency2, info.utcDate);
                     }
                     else
                     {
                         dependency = new CacheDependency(null, info.items, dependency2, DateTimeUtil.ConvertToLocalTime(info.utcDate));
                     }
                 }
             }
             return(dependency);
         }
         CacheDependency     dependency3         = null;
         VirtualPathProvider virtualPathProvider = HostingEnvironment.VirtualPathProvider;
         if ((virtualPathProvider != null) && (this._requestVirtualPath != null))
         {
             dependency3 = virtualPathProvider.GetCacheDependency(this._requestVirtualPath, this.GetDependencies(), this._oldestDependency);
         }
         if (dependency3 == null)
         {
             return(dependency);
         }
         AggregateCacheDependency dependency4 = new AggregateCacheDependency();
         dependency4.Add(new CacheDependency[] { dependency3 });
         if (dependency != null)
         {
             dependency4.Add(new CacheDependency[] { dependency });
         }
         dependency = dependency4;
     }
     return(dependency);
 }
        /// <summary>
        /// 利用指定 VirtualPathProvider 将虚拟路径所指向文件当作静态文件加载。
        /// </summary>
        /// <param name="context">当前请求上下文</param>
        /// <param name="provider">指定的 VirtualPathProvider</param>
        /// <param name="virtualPath">虚拟路径</param>
        /// <returns>加载结果</returns>
        public HtmlContentResult LoadContent(HttpContextBase context, VirtualPathProvider provider, string virtualPath)
        {
            if (!VirtualPathUtility.IsAppRelative(virtualPath))
            {
                return(null);
            }

            if (!provider.FileExists(virtualPath))
            {
                return(null);
            }

            var file = provider.GetFile(virtualPath);

            if (file == null)
            {
                return(null);
            }



            var key = provider.GetCacheKey(virtualPath) ?? "StaticFile_" + virtualPath;

            var content = context.Cache.Get(key) as string;


            if (content == null)
            {
                var now = DateTime.UtcNow;

                content = LoadContent(file);
                var dependency = provider.GetCacheDependency(virtualPath, new[] { virtualPath }, now) ?? new CacheDependency(HostingEnvironment.MapPath(virtualPath));


                context.Cache.Insert(key, content, dependency);
            }


            return(new HtmlContentResult(this, content, new Uri(baseUri, VirtualPathUtility.ToAbsolute(virtualPath)), key));
        }
Exemple #9
0
        public void ProcessRequest(HttpContext context)
        {
            if (File.Exists(context.Request.PhysicalPath))
            {
                var fileModified = File.GetLastWriteTimeUtc(context.Request.PhysicalPath);
                if (CacheUtility.IsUnmodifiedSince(context.Request, fileModified))
                {
                    CacheUtility.NotModified(context.Response);
                }

                logger.DebugFormat("Transmitting virtual file {0} available on disk {1}", context.Request.AppRelativeCurrentExecutionFilePath, context.Request.PhysicalPath);
                N2.Web.CacheUtility.SetValidUntilExpires(context.Response, DateTime.UtcNow);
                context.Response.ContentType = GetContentType(context.Request.PhysicalPath);
                context.Response.TransmitFile(context.Request.PhysicalPath);
            }
            else if (vpp.FileExists(context.Request.AppRelativeCurrentExecutionFilePath))
            {
                if (Modified.HasValue && CacheUtility.IsUnmodifiedSince(context.Request, Modified.Value))
                {
                    logger.DebugFormat("Not modified: {0}", context.Request.AppRelativeCurrentExecutionFilePath);
                    CacheUtility.NotModified(context.Response);
                }

                byte[] cached = context.Cache["VirtualPathFileHandler:" + context.Request.AppRelativeCurrentExecutionFilePath] as byte[];
                if (cached != null)
                {
                    logger.DebugFormat("Transmitting cached file: {0}", context.Request.AppRelativeCurrentExecutionFilePath);
                    context.Response.ContentType = GetContentType(context.Request.AppRelativeCurrentExecutionFilePath);
                    context.Response.OutputStream.Write(cached, 0, cached.Length);
                    return;
                }


                var f = vpp.GetFile(context.Request.AppRelativeCurrentExecutionFilePath);
                using (var s = f.Open())
                {
                    byte[] buffer    = new byte[131072];
                    int    readBytes = ReadBlock(s, buffer);
                    if (readBytes <= 0)
                    {
                        logger.DebugFormat("Empty file: {0}", context.Request.AppRelativeCurrentExecutionFilePath);
                        return;
                    }

                    N2.Web.CacheUtility.SetValidUntilExpires(context.Response, DateTime.UtcNow);
                    context.Response.ContentType = GetContentType(context.Request.AppRelativeCurrentExecutionFilePath);
                    logger.DebugFormat("Writing file: {0}", context.Request.AppRelativeCurrentExecutionFilePath);
                    context.Response.OutputStream.Write(buffer, 0, readBytes);

                    if (readBytes < buffer.Length)
                    {
                        cached = new byte[readBytes];
                        Array.Copy(buffer, cached, readBytes);
                        logger.DebugFormat("Adding to cache: {0}", context.Request.AppRelativeCurrentExecutionFilePath);
                        context.Cache.Add("VirtualPathFileHandler:" + context.Request.AppRelativeCurrentExecutionFilePath, cached, vpp.GetCacheDependency(context.Request.AppRelativeCurrentExecutionFilePath, new[] { context.Request.AppRelativeCurrentExecutionFilePath }, DateTime.UtcNow), DateTime.MaxValue, TimeSpan.FromMinutes(1), System.Web.Caching.CacheItemPriority.BelowNormal, null);
                        return;
                    }
                    logger.DebugFormat("Transmitting rest of file {0}", context.Request.AppRelativeCurrentExecutionFilePath);
                    TransferBetweenStreams(buffer, s, context.Response.OutputStream);
                }
            }
        }
Exemple #10
0
        /// <summary>
        /// 创建指定虚拟路径文件的缓存依赖项,当文件发生变化时可以清除缓存。
        /// </summary>
        /// <param name="provider">当前所使用的虚拟路径提供程序</param>
        /// <param name="virtualPath">需要监视的文件虚拟路径</param>
        /// <returns>监视路径的缓存依赖项</returns>
        public static CacheDependency CreateCacheDependency(VirtualPathProvider provider, string virtualPath)
        {
            var now = DateTime.UtcNow;

            return(provider.GetCacheDependency(virtualPath, new[] { virtualPath }, now) ?? new CacheDependency(HostingEnvironment.MapPath(virtualPath)));
        }
 public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
 {
     return((IsEmbeddedPath(virtualPath) || _previous == null) ? null : _previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart));
 }