Cache bundles in-memory.

pull/301/head
Halil ibrahim Kalkan 7 years ago
parent 8c110a2059
commit b60cc57459

@ -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<string, List<string>> _cache;
public BundleCache()
{
_cache = new ConcurrentDictionary<string, List<string>>();
}
public List<string> GetFiles(string bundleName, Func<List<string>> factory)
{
return _cache.GetOrAdd(bundleName, factory);
}
}
}

@ -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<string, string> _cache;
private readonly IBundleCache _bundleCache;
public BundleManager(
IOptions<BundlingOptions> 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<string, string>();
}
public List<string> GetStyleBundleFiles(string bundleName)
@ -58,27 +52,26 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
protected virtual List<string> 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<string>
{
"/" + bundleRelativePath
};
return new List<string>
{
"/" + bundleRelativePath
};
});
}
protected virtual void SaveBundleResult(string bundleRelativePath, BundleResult bundleResult)

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
{
public interface IBundleCache
{
List<string> GetFiles(string bundleName, Func<List<string>> factory);
}
}
Loading…
Cancel
Save