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 System.Threading.Tasks; |
||||
|
using Volo.Abp.Caching; |
||||
using Volo.Abp.GlobalFeatures; |
using Volo.Abp.GlobalFeatures; |
||||
using Volo.CmsKit.GlobalFeatures; |
using Volo.CmsKit.GlobalFeatures; |
||||
using Volo.CmsKit.Menus; |
using Volo.CmsKit.Menus; |
||||
|
|
||||
namespace Volo.CmsKit.Public.Menus |
namespace Volo.CmsKit.Public.Menus |
||||
{ |
{ |
||||
[RequiresGlobalFeature(typeof(MenuFeature))] |
[RequiresGlobalFeature(typeof(MenuFeature))] |
||||
public class MenuPublicAppService : CmsKitPublicAppServiceBase, IMenuPublicAppService |
public class MenuPublicAppService : CmsKitPublicAppServiceBase, IMenuPublicAppService |
||||
{ |
{ |
||||
protected IMenuRepository MenuRepository { get; } |
protected IMenuRepository MenuRepository { get; } |
||||
|
|
||||
public MenuPublicAppService(IMenuRepository menuRepository) |
protected IDistributedCache<MenuWithDetailsDto> DistributedCache { get; } |
||||
|
|
||||
|
public MenuPublicAppService( |
||||
|
IMenuRepository menuRepository, |
||||
|
IDistributedCache<MenuWithDetailsDto> distributedCache) |
||||
{ |
{ |
||||
MenuRepository = menuRepository; |
MenuRepository = menuRepository; |
||||
|
DistributedCache = distributedCache; |
||||
} |
} |
||||
|
|
||||
public async Task<MenuWithDetailsDto> GetMainMenuAsync() |
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 cachedMenu; |
||||
{ |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
return ObjectMapper.Map<Menu, MenuWithDetailsDto>(menu); |
|
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
Loading…
Reference in new issue