mirror of https://github.com/abpframework/abp.git
Browse Source
Add `CachedBundleDynamicFileProvider` to sync bundle files using cache.pull/19955/head
committed by
GitHub
3 changed files with 82 additions and 3 deletions
@ -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); |
|||
} |
|||
} |
|||
@ -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; } |
|||
} |
|||
Loading…
Reference in new issue