如何在 ASP.NET 样板中使用缓存?

How to use caching in ASP.NET Boilerplate?

我在数据库中有一些主数据,我在整个应用程序中都需要这些数据。那么在应用程序中定义此数据的最佳方式是什么?我应该在应用程序中的什么地方定义,以便每次应用程序启动时我都会在我的应用程序中初始化这个主数据。我应该在哪里定义从数据库中获取数据的方法?

using System.Linq;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Runtime.Caching;
using Test.Authorization;
using Test.Caching.Dto;

namespace Test.Caching
{
    [AbpAuthorize(AppPermissions.Pages_Administration_Host_Maintenance)]
    public class CachingAppService : TestAppServiceBase, ICachingAppService
    {
        private readonly ICacheManager _cacheManager;

        public CachingAppService(ICacheManager cacheManager)
        {
            _cacheManager = cacheManager;
        }

        public ListResultDto<CacheDto> GetAllCaches()
        {
            var caches = _cacheManager.GetAllCaches()
                                        .Select(cache => new CacheDto
                                        {
                                            Name = cache.Name
                                        })
                                        .ToList();

            return new ListResultDto<CacheDto>(caches);
        }

        public async Task ClearCache(EntityDto<string> input)
        {
            var cache = _cacheManager.GetCache(input.Id);
            await cache.ClearAsync();
        }

        public async Task ClearAllCaches()
        {
            var caches = _cacheManager.GetAllCaches();
            foreach (var cache in caches)
            {
                await cache.ClearAsync();
            }
        }
    }
}

Startup.cs代码:

public IServiceProvider ConfigureServices(IServiceCollection services)
{
   services.AddMemoryCache();
}

此答案基于 Entity Caching 的文档。

What is the best way to define this data in the application?

作为缓存项:

[AutoMapFrom(typeof(Person))]
public class PersonCacheItem
{
    public string Name { get; set; }
}

Where should I define in the application so that every time applications starts I will initialize this master data in my application? How to add my data to cache? Where do I need to add data to cache?

您没有初始化主数据(也没有向 IEntityCache 添加数据)。它是延迟加载的。

默认缓存过期时间为 60 分钟。它在滑动。因此,如果您在 60 分钟内没有使用缓存中的某个项目,它会自动从缓存中删除。您可以为所有缓存或特定缓存配置它。

// Configuration for a specific cache
Configuration.Caching.Configure("MyCache", cache =>
{
    cache.DefaultSlidingExpireTime = TimeSpan.FromHours(8);
});

此代码应放在模块的 PreInitialize 方法中。

Where should I define the method which will fetch data from DB?

您没有定义方法。只需注入一个 IRepository。指定 cacheName 如果配置如上。

public class PersonCache : EntityCache<Person, PersonCacheItem>, IPersonCache, ITransientDependency
{
    public PersonCache(ICacheManager cacheManager, IRepository<Person> repository)
        : base(cacheManager, repository, "MyCache") // "MyCache" is optional
    {
    }
}

public interface IPersonCache : IEntityCache<PersonCacheItem>
{
}

How will I get data from cache?

使用 Person 缓存的示例 class:

public class MyPersonService : ITransientDependency
{
    private readonly IPersonCache _personCache;

    public MyPersonService(IPersonCache personCache)
    {
        _personCache = personCache;
    }

    public string GetPersonNameById(int id)
    {
        return _personCache[id].Name; // alternative: _personCache.Get(id).Name;
    }
}