mirror of https://github.com/abpframework/abp.git
committed by
GitHub
6 changed files with 67 additions and 11 deletions
@ -0,0 +1,7 @@ |
|||
namespace Volo.CmsKit.Menus |
|||
{ |
|||
public static class MenuApplicationConsts |
|||
{ |
|||
public static string MainMenuCacheKey = "MainMenu"; |
|||
} |
|||
} |
|||
@ -0,0 +1,31 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Caching; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Entities.Events; |
|||
using Volo.Abp.EventBus; |
|||
|
|||
namespace Volo.CmsKit.Menus |
|||
{ |
|||
public class MenuChangedHandler : |
|||
ILocalEventHandler<EntityUpdatedEventData<Menu>>, |
|||
ILocalEventHandler<EntityDeletedEventData<Menu>>, |
|||
ITransientDependency |
|||
{ |
|||
protected IDistributedCache<MenuWithDetailsDto> DistributedCache { get; } |
|||
|
|||
public MenuChangedHandler(IDistributedCache<MenuWithDetailsDto> distributedCache) |
|||
{ |
|||
DistributedCache = distributedCache; |
|||
} |
|||
|
|||
public Task HandleEventAsync(EntityUpdatedEventData<Menu> eventData) |
|||
{ |
|||
return DistributedCache.RemoveAsync(MenuApplicationConsts.MainMenuCacheKey); |
|||
} |
|||
|
|||
public Task HandleEventAsync(EntityDeletedEventData<Menu> eventData) |
|||
{ |
|||
return DistributedCache.RemoveAsync(MenuApplicationConsts.MainMenuCacheKey); |
|||
} |
|||
} |
|||
} |
|||
@ -1,30 +1,45 @@ |
|||
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> DistributedCache { get; } |
|||
|
|||
public MenuPublicAppService( |
|||
IMenuRepository menuRepository, |
|||
IDistributedCache<MenuWithDetailsDto> distributedCache) |
|||
{ |
|||
MenuRepository = menuRepository; |
|||
DistributedCache = distributedCache; |
|||
} |
|||
|
|||
|
|||
public async Task<MenuWithDetailsDto> GetMainMenuAsync() |
|||
{ |
|||
var menu = await MenuRepository.FindMainMenuAsync(includeDetails: true); |
|||
var cachedMenu = await DistributedCache.GetOrAddAsync( |
|||
MenuApplicationConsts.MainMenuCacheKey, |
|||
async () => |
|||
{ |
|||
var menu = await MenuRepository.FindMainMenuAsync(includeDetails: true); |
|||
|
|||
if (menu == null) |
|||
{ |
|||
return new MenuWithDetailsDto(); |
|||
} |
|||
|
|||
return ObjectMapper.Map<Menu, MenuWithDetailsDto>(menu); |
|||
}); |
|||
|
|||
if (menu == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return ObjectMapper.Map<Menu, MenuWithDetailsDto>(menu); |
|||
return cachedMenu; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue