diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCache.cs b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCache.cs new file mode 100644 index 0000000000..297d2cc5c5 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCache.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling +{ + public class BundleCache : IBundleCache, ISingletonDependency + { + private readonly ConcurrentDictionary> _cache; + + public BundleCache() + { + _cache = new ConcurrentDictionary>(); + } + + public List GetFiles(string bundleName, Func> factory) + { + return _cache.GetOrAdd(bundleName, factory); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleManager.cs b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleManager.cs index 4548b500d7..99b265ad9a 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleManager.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleManager.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Concurrent; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Text; using Microsoft.AspNetCore.Hosting; @@ -9,16 +7,12 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.Mvc.UI.Bundling.Scripts; using Volo.Abp.AspNetCore.Mvc.UI.Bundling.Styles; -using Volo.Abp.AspNetCore.Mvc.UI.Theming; using Volo.Abp.DependencyInjection; -using Volo.Abp.IO; using Volo.Abp.VirtualFileSystem; -using Volo.Abp.VirtualFileSystem.Embedded; namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling { - //TODO: Do not make this singleton (since it's using scoped services), instead separate a singleton bundlecache! - public class BundleManager : IBundleManager, ISingletonDependency + public class BundleManager : IBundleManager, ITransientDependency { private readonly BundlingOptions _options; private readonly IHostingEnvironment _hostingEnvironment; @@ -26,24 +20,24 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling private readonly IStyleBundler _styleBundler; private readonly IServiceProvider _serviceProvider; private readonly IDynamicFileProvider _dynamicFileProvider; - - private readonly ConcurrentDictionary _cache; + private readonly IBundleCache _bundleCache; public BundleManager( IOptions options, IScriptBundler scriptBundler, IStyleBundler styleBundler, IHostingEnvironment hostingEnvironment, - IServiceProvider serviceProvider, IDynamicFileProvider dynamicFileProvider) + IServiceProvider serviceProvider, + IDynamicFileProvider dynamicFileProvider, + IBundleCache bundleCache) { _hostingEnvironment = hostingEnvironment; _scriptBundler = scriptBundler; _serviceProvider = serviceProvider; _dynamicFileProvider = dynamicFileProvider; + _bundleCache = bundleCache; _styleBundler = styleBundler; _options = options.Value; - - _cache = new ConcurrentDictionary(); } public List GetStyleBundleFiles(string bundleName) @@ -58,27 +52,26 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling protected virtual List GetBundleFiles(BundleConfigurationCollection bundles, string bundleName, IBundler bundler) { - //TODO: Caching - //TODO: Concurrency - - - var files = CreateFileList(bundles, bundleName); + var bundleRelativePath = _options.BundleFolderName.EnsureEndsWith('/') + bundleName + "." + bundler.FileExtension; - if (!IsBundlingEnabled()) + return _bundleCache.GetFiles(bundleRelativePath, () => { - return files; - } + var files = CreateFileList(bundles, bundleName); - var bundleRelativePath = _options.BundleFolderName.EnsureEndsWith('/') + bundleName + "." + bundler.FileExtension; + if (!IsBundlingEnabled()) + { + return files; + } - var bundleResult = bundler.Bundle(new BundlerContext(bundleRelativePath, files)); + var bundleResult = bundler.Bundle(new BundlerContext(bundleRelativePath, files)); - SaveBundleResult(bundleRelativePath, bundleResult); + SaveBundleResult(bundleRelativePath, bundleResult); - return new List - { - "/" + bundleRelativePath - }; + return new List + { + "/" + bundleRelativePath + }; + }); } protected virtual void SaveBundleResult(string bundleRelativePath, BundleResult bundleResult) diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/IBundleCache.cs b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/IBundleCache.cs new file mode 100644 index 0000000000..bb2ac9d69c --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/IBundleCache.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; + +namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling +{ + public interface IBundleCache + { + List GetFiles(string bundleName, Func> factory); + } +} \ No newline at end of file