예제 #1
0
        public CachedAssetEntry InsertAsset(string virtualPath, IEnumerable <string> virtualPathDependencies, string content, params string[] processorCodes)
        {
            if (!_isEnabled)
            {
                return(null);
            }

            Guard.NotEmpty(virtualPath, nameof(virtualPath));
            Guard.NotEmpty(content, nameof(content));

            var cacheDirectoryName = ResolveCacheDirectoryName(virtualPath, out string themeName, out int storeId);

            using (KeyedLock.Lock(BuildLockKey(cacheDirectoryName)))
            {
                CacheFolder.CreateDirectory(cacheDirectoryName);

                try
                {
                    // Save main content file
                    // TODO: determine correct file extension
                    CreateFileFromEntries(cacheDirectoryName, "asset.css", new[] { content });

                    // Save dependencies file
                    var deps = ResolveVirtualPathDependencies(virtualPath, virtualPathDependencies, themeName);
                    CreateFileFromEntries(cacheDirectoryName, "asset.dependencies", deps);

                    // Save hash file
                    var currentHash = BundleTable.VirtualPathProvider.GetFileHash(virtualPath, deps);
                    CreateFileFromEntries(cacheDirectoryName, "asset.hash", new[] { currentHash });

                    // Save codes file
                    CreateFileFromEntries(cacheDirectoryName, "asset.pcodes", processorCodes);

                    var entry = new CachedAssetEntry
                    {
                        Content                 = content,
                        HashCode                = currentHash,
                        OriginalVirtualPath     = virtualPath,
                        VirtualPathDependencies = deps,
                        PhysicalPath            = CacheFolder.MapPath(cacheDirectoryName),
                        ThemeName               = themeName,
                        StoreId                 = storeId,
                        ProcessorCodes          = processorCodes
                    };

                    SetupEvictionObserver(entry);

                    Logger.DebugFormat("Succesfully inserted asset '{0}' to cache.", virtualPath);

                    return(entry);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat(ex, "Error while inserting asset '{0}' to the asset cache.", virtualPath);
                    InvalidateAssetInternal(cacheDirectoryName, themeName, storeId);
                }
            }

            return(null);
        }
예제 #2
0
        /// <summary>
        /// Invalidates a cached asset when any themevar was changed or the theme was touched on file system
        /// </summary>
        /// <param name="entry"></param>
        private static void SetupEvictionObserver(CachedAssetEntry entry)
        {
            if (entry.ThemeName == null)
            {
                return;
            }

            var cacheKey = CacheKeyPrefix + "{0}:{1}".FormatInvariant(entry.ThemeName, entry.StoreId);

            var cacheDependency = new CacheDependency(
                new string[0],
                new[] { FrameworkCacheConsumer.BuildThemeVarsCacheKey(entry.ThemeName, entry.StoreId) },
                DateTime.UtcNow);

            HttpRuntime.Cache.Insert(
                cacheKey,
                entry.PhysicalPath,
                cacheDependency,
                Cache.NoAbsoluteExpiration,
                Cache.NoSlidingExpiration,
                CacheItemPriority.Default,
                OnCacheItemRemoved);
        }
예제 #3
0
        public CachedAssetEntry GetAsset(string virtualPath)
        {
            if (!_isEnabled)
            {
                return(null);
            }

            Guard.NotEmpty(virtualPath, nameof(virtualPath));

            var cacheDirectoryName = ResolveCacheDirectoryName(virtualPath, out string themeName, out int storeId);

            if (CacheFolder.DirectoryExists(cacheDirectoryName))
            {
                try
                {
                    var deps = CacheFolder.ReadFile(CacheFolder.Combine(cacheDirectoryName, "asset.dependencies"));
                    var hash = CacheFolder.ReadFile(CacheFolder.Combine(cacheDirectoryName, "asset.hash"));

                    if (!TryValidate(virtualPath, deps, hash, out IEnumerable <string> parsedDeps, out string currentHash))
                    {
                        Logger.DebugFormat("Invalidating cached asset for '{0}' because it is not valid anymore.", virtualPath);
                        InvalidateAssetInternal(cacheDirectoryName, themeName, storeId);
                        return(null);
                    }

                    // TODO: determine correct file extension
                    var content = CacheFolder.ReadFile(CacheFolder.Combine(cacheDirectoryName, "asset.css"));
                    if (content == null)
                    {
                        using (KeyedLock.Lock(BuildLockKey(cacheDirectoryName)))
                        {
                            InvalidateAssetInternal(cacheDirectoryName, themeName, storeId);
                            return(null);
                        }
                    }

                    var codes = ParseFileContent(CacheFolder.ReadFile(CacheFolder.Combine(cacheDirectoryName, "asset.pcodes")));

                    var entry = new CachedAssetEntry
                    {
                        Content                 = content,
                        HashCode                = currentHash,
                        OriginalVirtualPath     = virtualPath,
                        VirtualPathDependencies = parsedDeps,
                        PhysicalPath            = CacheFolder.MapPath(cacheDirectoryName),
                        ThemeName               = themeName,
                        StoreId                 = storeId,
                        ProcessorCodes          = codes.ToArray()
                    };

                    SetupEvictionObserver(entry);

                    Logger.DebugFormat("Succesfully read asset '{0}' from cache.", virtualPath);

                    return(entry);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat(ex, "Error while resolving asset '{0}' from the asset cache.", virtualPath);
                }
            }

            return(null);
        }