public Task RemoveResponseAsync(BundleCacheKey cacheKey)
        {
            if (cacheKey.Key.HasValue())
            {
                InvalidateAssetInternal(GetCacheDirectory(cacheKey));
            }

            return(Task.CompletedTask);
        }
        public async Task PutResponseAsync(BundleCacheKey cacheKey, Bundle bundle, BundleResponse response)
        {
            if (_options.CurrentValue.EnableDiskCache == false)
            {
                return;
            }

            Guard.NotNull(cacheKey.Key, nameof(cacheKey.Key));
            Guard.NotNull(bundle, nameof(bundle));
            Guard.NotNull(response, nameof(response));

            var dir = GetCacheDirectory(cacheKey);

            using (await AsyncLock.KeyedAsync(BuildLockKey(dir.Name)))
            {
                _fs.TryCreateDirectory(dir.SubPath);

                try
                {
                    // Save main content file
                    await CreateFileFromEntries(dir, ResolveBundleContentFileName(bundle), new[] { response.Content });

                    // Save dependencies file
                    var deps = response.IncludedFiles;
                    await CreateFileFromEntries(dir, "bundle.dependencies", deps);

                    // Save hash file
                    var currentHash = GetFileHash(bundle, deps);
                    await CreateFileFromEntries(dir, "bundle.hash", new[] { currentHash });

                    // Save codes file
                    await CreateFileFromEntries(dir, "bundle.pcodes", response.ProcessorCodes);

                    Logger.Debug("Succesfully inserted bundle '{0}' to disk cache.", bundle.Route);
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, "Error while inserting bundle '{0}' to disk cache.", bundle.Route);
                    InvalidateAssetInternal(dir);
                }
            }
        }
        public async Task <BundleResponse> GetResponseAsync(BundleCacheKey cacheKey, Bundle bundle)
        {
            if (_options.CurrentValue.EnableDiskCache == false)
            {
                return(null);
            }

            Guard.NotNull(cacheKey.Key, nameof(cacheKey.Key));
            Guard.NotNull(bundle, nameof(bundle));

            var dir = GetCacheDirectory(cacheKey);

            if (dir.Exists)
            {
                try
                {
                    var deps = await ReadFile(dir, "bundle.dependencies");

                    var hash = await ReadFile(dir, "bundle.hash");

                    var(valid, parsedDeps, currentHash) = await TryValidate(bundle, deps, hash);

                    if (!valid)
                    {
                        Logger.Debug("Invalidating cached bundle for '{0}' because it is not valid anymore.", bundle.Route);
                        InvalidateAssetInternal(dir);
                        return(null);
                    }

                    var content = await ReadFile(dir, ResolveBundleContentFileName(bundle));

                    if (content == null)
                    {
                        using (await AsyncLock.KeyedAsync(BuildLockKey(dir.Name)))
                        {
                            InvalidateAssetInternal(dir);
                            return(null);
                        }
                    }

                    var pcodes = await ParseFileContent(await ReadFile(dir, "bundle.pcodes"));

                    var response = new BundleResponse
                    {
                        Route          = bundle.Route,
                        CreationDate   = dir.LastModified,
                        Content        = content,
                        ContentType    = bundle.ContentType,
                        ProcessorCodes = pcodes.ToArray(),
                        IncludedFiles  = parsedDeps
                    };

                    Logger.Debug("Succesfully read bundle '{0}' from disk cache.", bundle.Route);

                    return(response);
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, "Error while resolving bundle '{0}' from the disk cache.", bundle.Route);
                }
            }

            return(null);
        }
 private static string ResolveBundleDirectoryName(BundleCacheKey cacheKey)
 => PathUtility.SanitizeFileName(cacheKey.Key.Trim('/', '\\'));
 private IDirectory GetCacheDirectory(BundleCacheKey cacheKey)
 => _fs.GetDirectory(_fs.PathCombine(DirName, ResolveBundleDirectoryName(cacheKey)));