Browse Source

CmsKit: Global resource cache

pull/11705/head
Yunus Emre Kalkan 5 years ago
parent
commit
f03dd10b0d
  1. 1
      modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo.CmsKit.Common.Application.Contracts.csproj
  2. 4
      modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/CmsKitCommonApplicationContractsModule.cs
  3. 33
      modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/GlobalResources/Handlers/GlobalResourceEventHandler.cs
  4. 2
      modules/cms-kit/src/Volo.CmsKit.Public.Web/CmsKitPublicWebModule.cs
  5. 46
      modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicGlobalResourcesController.cs

1
modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo.CmsKit.Common.Application.Contracts.csproj

@ -12,6 +12,7 @@
<ProjectReference Include="..\Volo.CmsKit.Domain.Shared\Volo.CmsKit.Domain.Shared.csproj" /> <ProjectReference Include="..\Volo.CmsKit.Domain.Shared\Volo.CmsKit.Domain.Shared.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Ddd.Application.Contracts\Volo.Abp.Ddd.Application.Contracts.csproj" /> <ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Ddd.Application.Contracts\Volo.Abp.Ddd.Application.Contracts.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Authorization\Volo.Abp.Authorization.csproj" /> <ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Authorization\Volo.Abp.Authorization.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Caching\Volo.Abp.Caching.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

4
modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/CmsKitCommonApplicationContractsModule.cs

@ -1,5 +1,6 @@
using Volo.Abp.Application; using Volo.Abp.Application;
using Volo.Abp.Authorization; using Volo.Abp.Authorization;
using Volo.Abp.Caching;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
namespace Volo.CmsKit; namespace Volo.CmsKit;
@ -7,7 +8,8 @@ namespace Volo.CmsKit;
[DependsOn( [DependsOn(
typeof(CmsKitDomainSharedModule), typeof(CmsKitDomainSharedModule),
typeof(AbpDddApplicationContractsModule), typeof(AbpDddApplicationContractsModule),
typeof(AbpAuthorizationModule) typeof(AbpAuthorizationModule),
typeof(AbpCachingModule)
)] )]
public class CmsKitCommonApplicationContractsModule : AbpModule public class CmsKitCommonApplicationContractsModule : AbpModule
{ {

33
modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/GlobalResources/Handlers/GlobalResourceEventHandler.cs

@ -0,0 +1,33 @@
using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.EventBus;
using Volo.Abp.ObjectMapping;
using Volo.CmsKit.GlobalResources;
using Volo.CmsKit.Public.GlobalResources;
namespace Volo.CmsKit.Public.GlobalResources.Handlers;
public class GlobalResourceEventHandler:
ILocalEventHandler<EntityUpdatedEventData<GlobalResource>>,
ITransientDependency
{
public IObjectMapper ObjectMapper { get; }
private readonly IDistributedCache<GlobalResourceDto> _resourceCache;
public GlobalResourceEventHandler(
IDistributedCache<GlobalResourceDto> resourceCache,
IObjectMapper objectMapper)
{
ObjectMapper = objectMapper;
_resourceCache = resourceCache;
}
public async Task HandleEventAsync(EntityUpdatedEventData<GlobalResource> eventData)
{
await _resourceCache.SetAsync(
eventData.Entity.Name,
ObjectMapper.Map<GlobalResource, GlobalResourceDto>(eventData.Entity));
}
}

2
modules/cms-kit/src/Volo.CmsKit.Public.Web/CmsKitPublicWebModule.cs

@ -98,7 +98,7 @@ public class CmsKitPublicWebModule : AbpModule
typeof(GlobalStyleViewComponent) typeof(GlobalStyleViewComponent)
); );
options.Add( options.Add(
LayoutHooks.Head.Last, LayoutHooks.Body.Last,
typeof(GlobalScriptViewComponent) typeof(GlobalScriptViewComponent)
); );
}); });

46
modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicGlobalResourcesController.cs

@ -1,7 +1,11 @@
using System.Text; using System;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Caching;
using Volo.CmsKit.GlobalResources;
using Volo.CmsKit.Public.GlobalResources; using Volo.CmsKit.Public.GlobalResources;
namespace Volo.CmsKit.Public.Web.Controllers; namespace Volo.CmsKit.Public.Web.Controllers;
@ -10,31 +14,49 @@ namespace Volo.CmsKit.Public.Web.Controllers;
public class CmsKitPublicGlobalResourcesController: AbpController public class CmsKitPublicGlobalResourcesController: AbpController
{ {
private readonly IGlobalResourcePublicAppService _globalResourcePublicAppService; private readonly IGlobalResourcePublicAppService _globalResourcePublicAppService;
private readonly IDistributedCache<GlobalResourceDto> _resourceCache;
public CmsKitPublicGlobalResourcesController(IGlobalResourcePublicAppService globalResourcePublicAppService) public CmsKitPublicGlobalResourcesController(
IGlobalResourcePublicAppService globalResourcePublicAppService,
IDistributedCache<GlobalResourceDto> resourceCache)
{ {
_globalResourcePublicAppService = globalResourcePublicAppService; _globalResourcePublicAppService = globalResourcePublicAppService;
_resourceCache = resourceCache;
} }
[HttpGet] [HttpGet]
[Route("style")] [Route("style")]
public async Task<IActionResult> GetGlobalStyleAsync(string entityType, string entityId) public async Task<IActionResult> GetGlobalStyleAsync()
{ {
var style = await _globalResourcePublicAppService.GetGlobalStyleAsync(); var style = await _resourceCache.GetOrAddAsync(
GlobalResourceConsts.GlobalStyleName, //Cache key
async () => await _globalResourcePublicAppService.GetGlobalStyleAsync(),
() => new DistributedCacheEntryOptions
{
AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(2)
}
);
var contents = Encoding.ASCII.GetBytes(style.Value); return new FileContentResult(
Encoding.ASCII.GetBytes(style.Value),
return new FileContentResult(contents, "text/css"); "text/css");
} }
[HttpGet] [HttpGet]
[Route("script")] [Route("script")]
public async Task<IActionResult> GetGlobalScriptAsync(string entityType, string entityId) public async Task<IActionResult> GetGlobalScriptAsync()
{ {
var script = await _globalResourcePublicAppService.GetGlobalScriptAsync(); var script = await _resourceCache.GetOrAddAsync(
GlobalResourceConsts.GlobalScriptName, //Cache key
var contents = Encoding.ASCII.GetBytes(script.Value); async () => await _globalResourcePublicAppService.GetGlobalScriptAsync(),
() => new DistributedCacheEntryOptions
{
AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(2)
}
);
return new FileContentResult(contents, "application/javascript"); return new FileContentResult(
Encoding.ASCII.GetBytes(script.Value),
"application/javascript");
} }
} }
Loading…
Cancel
Save