mirror of https://github.com/abpframework/abp
parent
c18fb0467b
commit
7b68e0189a
@ -0,0 +1,10 @@
|
||||
namespace Volo.CmsKit.Public.Menus
|
||||
{
|
||||
public class MainMenuCacheKey
|
||||
{
|
||||
public override string ToString()
|
||||
{
|
||||
return "MainMenu";
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
using System.Threading.Tasks;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.Domain.Entities.Events;
|
||||
using Volo.Abp.EventBus;
|
||||
using Volo.Abp.EventBus.Distributed;
|
||||
|
||||
namespace Volo.CmsKit.Menus
|
||||
{
|
||||
public class MenuUpdatedHandler : ILocalEventHandler<EntityUpdatedEventData<Menu>>, ITransientDependency
|
||||
{
|
||||
public IDistributedEventBus EventBus { get; }
|
||||
|
||||
public MenuUpdatedHandler(IDistributedEventBus eventBus)
|
||||
{
|
||||
EventBus = eventBus;
|
||||
}
|
||||
|
||||
public async Task HandleEventAsync(EntityUpdatedEventData<Menu> eventData)
|
||||
{
|
||||
await EventBus.PublishAsync(new MenuUpdatedEto
|
||||
{
|
||||
MenuId = eventData.Entity.Id
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using Volo.Abp.Domain.Entities.Events.Distributed;
|
||||
using Volo.Abp.EventBus;
|
||||
|
||||
namespace Volo.CmsKit.Menus
|
||||
{
|
||||
[Serializable]
|
||||
[EventName("Volo.CmsKit.Menus.Changed")]
|
||||
public class MenuChangedEto : EtoBase
|
||||
{
|
||||
public Guid MenuId { get; set; }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using Volo.Abp.Domain.Entities.Events.Distributed;
|
||||
using Volo.Abp.EventBus;
|
||||
|
||||
namespace Volo.CmsKit.Menus
|
||||
{
|
||||
[EventName("Volo.CmsKit.Menus.Updated")]
|
||||
public class MenuUpdatedEto : EtoBase
|
||||
{
|
||||
public Guid MenuId { get; set; }
|
||||
}
|
||||
}
|
||||
@ -1,30 +1,50 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.GlobalFeatures;
|
||||
using Volo.CmsKit.GlobalFeatures;
|
||||
using Volo.CmsKit.Menus;
|
||||
|
||||
namespace Volo.CmsKit.Public.Menus
|
||||
{
|
||||
{
|
||||
[RequiresGlobalFeature(typeof(MenuFeature))]
|
||||
public class MenuPublicAppService : CmsKitPublicAppServiceBase, IMenuPublicAppService
|
||||
{
|
||||
protected IMenuRepository MenuRepository { get; }
|
||||
|
||||
public MenuPublicAppService(IMenuRepository menuRepository)
|
||||
protected IDistributedCache<MenuWithDetailsDto, MainMenuCacheKey> DistributedCache { get; }
|
||||
|
||||
public MenuPublicAppService(
|
||||
IMenuRepository menuRepository,
|
||||
IDistributedCache<MenuWithDetailsDto, MainMenuCacheKey> distributedCache)
|
||||
{
|
||||
MenuRepository = menuRepository;
|
||||
DistributedCache = distributedCache;
|
||||
}
|
||||
|
||||
|
||||
public async Task<MenuWithDetailsDto> GetMainMenuAsync()
|
||||
{
|
||||
var menu = await MenuRepository.FindMainMenuAsync(includeDetails: true);
|
||||
var cachedMenu = await DistributedCache.GetOrAddAsync(
|
||||
new MainMenuCacheKey(),
|
||||
async () =>
|
||||
{
|
||||
var menu = await MenuRepository.FindMainMenuAsync(includeDetails: true);
|
||||
|
||||
if (menu == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (menu == null)
|
||||
return ObjectMapper.Map<Menu, MenuWithDetailsDto>(menu);
|
||||
});
|
||||
|
||||
if (cachedMenu == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return ObjectMapper.Map<Menu, MenuWithDetailsDto>(menu);
|
||||
|
||||
return cachedMenu;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
using System.Threading.Tasks;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.EventBus.Distributed;
|
||||
using Volo.CmsKit.Menus;
|
||||
|
||||
namespace Volo.CmsKit.Public.Menus
|
||||
{
|
||||
public class MenuUpdatedHandler : IDistributedEventHandler<MenuUpdatedEto>, ITransientDependency
|
||||
{
|
||||
protected IDistributedCache<MenuWithDetailsDto, MainMenuCacheKey> DistributedCache { get; }
|
||||
|
||||
public MenuUpdatedHandler(IDistributedCache<MenuWithDetailsDto, MainMenuCacheKey> distributedCache)
|
||||
{
|
||||
DistributedCache = distributedCache;
|
||||
}
|
||||
|
||||
public async Task HandleEventAsync(MenuUpdatedEto eventData)
|
||||
{
|
||||
await DistributedCache.RemoveAsync(new MainMenuCacheKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue