feat: 增加 ICacheManager 缓存管理类接口

This commit is contained in:
Argo-Tianyi 2022-01-10 14:44:38 +08:00
parent 78078e6d6f
commit 9720ae85ef
7 changed files with 105 additions and 4 deletions

View File

@ -0,0 +1,28 @@
using BootstrapAdmin.Web.Core.Services;
using BootstrapBlazor.Web.Core;
using Microsoft.Extensions.Caching.Memory;
namespace BootstrapAdmin.Web.Core;
/// <summary>
/// 缓存管理类
/// </summary>
public static class CacheManager
{
private static ICacheManager Cache { get; } = DefaultCacheManager.Instance;
/// <summary>
/// 获得或者新建数据
/// </summary>
/// <typeparam name="TItem"></typeparam>
/// <param name="key"></param>
/// <param name="valueFactory"></param>
/// <returns></returns>
public static TItem GetOrCreate<TItem>(string key, Func<ICacheEntry, TItem> valueFactory) => Cache.GetOrCreate(key, valueFactory);
/// <summary>
/// 清除指定键值缓存项
/// </summary>
/// <param name="key"></param>
public static void Clear(string? key) => Cache.Clear(key);
}

View File

@ -0,0 +1,23 @@
using BootstrapAdmin.Web.Core.Services;
using BootstrapBlazor.Web.Core;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
///
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
///
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddCacheManager(this IServiceCollection services)
{
services.AddMemoryCache();
services.TryAddSingleton<ICacheManager>(DefaultCacheManager.Instance);
return services;
}
}

View File

@ -0,0 +1,28 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using Microsoft.Extensions.Caching.Memory;
namespace BootstrapBlazor.Web.Core;
/// <summary>
/// CacheManager 接口类
/// </summary>
public interface ICacheManager
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="factory"></param>
/// <returns></returns>
T GetOrCreate<T>(string key, Func<ICacheEntry, T> factory);
/// <summary>
///
/// </summary>
/// <param name="key"></param>
void Clear(string? key = null);
}

View File

@ -0,0 +1,21 @@
using BootstrapBlazor.Web.Core;
using Microsoft.Extensions.Caching.Memory;
namespace BootstrapAdmin.Web.Core.Services;
class DefaultCacheManager : ICacheManager
{
private static readonly Lazy<ICacheManager> cache = new(() => new DefaultCacheManager());
public static ICacheManager Instance { get; } = cache.Value;
public T GetOrCreate<T>(string key, Func<ICacheEntry, T> factory)
{
throw new NotImplementedException();
}
public void Clear(string? key)
{
throw new NotImplementedException();
}
}

View File

@ -22,6 +22,9 @@ namespace Microsoft.Extensions.DependencyInjection
services.AddCors();
services.AddResponseCompression();
// 增加 缓存管理服务
services.AddCacheManager();
// 增加 健康检查服务
services.AddAdminHealthChecks();

View File

@ -38,7 +38,7 @@ public class Index : ComponentBase
protected override void OnInitialized()
{
var appId = UsersService.GetAppIdByUserName(Context.UserName);
Url = DictsService.GetHomeUrlByAppId(appId) ?? "Admin/Index";
Url = DictsService.GetHomeUrlByAppId(appId) ?? "/Admin/Index";
#if !DEBUG
Navigation.NavigateTo(Url, true);

View File

@ -1,6 +1,4 @@
using BootstrapAdmin.Web.Extensions;
var builder = WebApplication.CreateBuilder(args);
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();