Browse Source

Merge pull request #19870 from abpframework/CachedBundleDynamicFileProvider

Add `CachedBundleDynamicFileProvider` to sync bundle files using cache.
pull/19955/head
Halil İbrahim Kalkan 2 years ago
committed by GitHub
parent
commit
37eb21847e
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 57
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/CachedBundleDynamicFileProvider.cs
  2. 22
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/InMemoryFileInfoCacheItem.cs
  3. 6
      framework/src/Volo.Abp.VirtualFileSystem/Volo/Abp/VirtualFileSystem/DynamicFileProvider.cs

57
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/CachedBundleDynamicFileProvider.cs

@ -0,0 +1,57 @@
using System;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.VirtualFileSystem;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling;
[Dependency(ReplaceServices = true)]
public class CachedBundleDynamicFileProvider : DynamicFileProvider
{
protected IDistributedCache<InMemoryFileInfoCacheItem> Cache { get; }
protected IOptions<AbpBundlingOptions> BundlingOptions { get; }
public CachedBundleDynamicFileProvider(
IDistributedCache<InMemoryFileInfoCacheItem> cache,
IOptions<AbpBundlingOptions> bundlingOptions)
{
Cache = cache;
BundlingOptions = bundlingOptions;
}
public override IFileInfo GetFileInfo(string? subpath)
{
var fileInfo = base.GetFileInfo(subpath);
if (!subpath.IsNullOrWhiteSpace() && fileInfo is NotFoundFileInfo &&
subpath.Contains(BundlingOptions.Value.BundleFolderName, StringComparison.OrdinalIgnoreCase))
{
var filePath = NormalizePath(subpath);
var cacheItem = Cache.Get(filePath);
if (cacheItem == null)
{
return fileInfo;
}
fileInfo = new InMemoryFileInfo(filePath, cacheItem.FileContent, cacheItem.Name);
DynamicFiles.AddOrUpdate(filePath, fileInfo, (key, value) => fileInfo);
}
return fileInfo;
}
public override void AddOrUpdate(IFileInfo fileInfo)
{
var filePath = fileInfo.GetVirtualOrPhysicalPathOrNull();
Cache.GetOrAdd(filePath!, () => new InMemoryFileInfoCacheItem(filePath!, fileInfo.ReadBytes(), fileInfo.Name));
base.AddOrUpdate(fileInfo);
}
public override bool Delete(string filePath)
{
Cache.Remove(filePath);
return base.Delete(filePath);
}
}

22
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/InMemoryFileInfoCacheItem.cs

@ -0,0 +1,22 @@
using System;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling;
[Serializable]
[IgnoreMultiTenancy]
public class InMemoryFileInfoCacheItem
{
public InMemoryFileInfoCacheItem(string dynamicPath, byte[] fileContent, string name)
{
DynamicPath = dynamicPath;
Name = name;
FileContent = fileContent;
}
public string DynamicPath { get; set; }
public string Name { get; set; }
public byte[] FileContent { get; set; }
}

6
framework/src/Volo.Abp.VirtualFileSystem/Volo/Abp/VirtualFileSystem/DynamicFileProvider.cs

@ -29,14 +29,14 @@ public class DynamicFileProvider : DictionaryBasedFileProvider, IDynamicFileProv
DynamicFiles = new ConcurrentDictionary<string, IFileInfo>();
}
public void AddOrUpdate(IFileInfo fileInfo)
public virtual void AddOrUpdate(IFileInfo fileInfo)
{
var filePath = fileInfo.GetVirtualOrPhysicalPathOrNull();
DynamicFiles.AddOrUpdate(filePath!, fileInfo, (key, value) => fileInfo);
ReportChange(filePath!);
}
public bool Delete(string filePath)
public virtual bool Delete(string filePath)
{
if (!DynamicFiles.TryRemove(filePath, out _))
{
@ -65,7 +65,7 @@ public class DynamicFileProvider : DictionaryBasedFileProvider, IDynamicFileProv
return tokenInfo.ChangeToken;
}
private void ReportChange(string filePath)
protected virtual void ReportChange(string filePath)
{
if (FilePathTokenLookup.TryRemove(filePath, out var tokenInfo))
{

Loading…
Cancel
Save