How to Cache entities?
I donot found the answer in docs!
https://docs.abp.io/en/abp/latest/Caching isn't helpful?
It's just for dto? I want to use it in domain or repository? Are there any examples of this
using FS.Abp.CodingManagement.Coding;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Json;
using Volo.Abp.Json.Newtonsoft;
using Volo.Abp.Threading;
namespace FS.Abp.CodingManagement.Caching
{
[ExposeServices(typeof(NewtonsoftJsonSerializer))]
public class NewtonsoftJsonSerializer : Volo.Abp.Json.Newtonsoft.NewtonsoftJsonSerializer
{
public NewtonsoftJsonSerializer(AbpJsonIsoDateTimeConverter dateTimeConverter) : base(dateTimeConverter)
{
}
protected override JsonSerializerSettings CreateSerializerSettings(bool camelCase = true, bool indented = false)
{
var result = base.CreateSerializerSettings(camelCase, indented);
//todo :option
result.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
result.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
return result;
}
}
[ExposeServices(typeof(Utf8JsonDistributedCacheSerializer))]
public class Utf8JsonDistributedCacheSerializer : Volo.Abp.Caching.Utf8JsonDistributedCacheSerializer
{
public Utf8JsonDistributedCacheSerializer(NewtonsoftJsonSerializer jsonSerializer)
: base(jsonSerializer)
{
}
}
[ExposeServices(typeof(CodesCache))]
public class CodesCache : Volo.Abp.Caching.DistributedCache<Codes>, Volo.Abp.DependencyInjection.ISingletonDependency
{
public CodesCache(
IOptions<AbpDistributedCacheOptions> distributedCacheOption,
IDistributedCache cache,
ICancellationTokenProvider cancellationTokenProvider,
Utf8JsonDistributedCacheSerializer serializer,
IDistributedCacheKeyNormalizer keyNormalizer) : base(
distributedCacheOption: distributedCacheOption,
cache: cache,
cancellationTokenProvider: cancellationTokenProvider,
serializer: serializer,
keyNormalizer: keyNormalizer)
{
}
}
}
client code
public class CodesService : ICodesService
{
private readonly IDistributedCache<Codes> _cache;
private readonly ICodesTreeRepository _codesTreeRepository;
private readonly ICurrentTenant _currentTenant;
public CodesService(
FS.Abp.CodingManagement.Caching.CodesCache cache,
ICodesTreeRepository codesTreeRepository,
ICurrentTenant currentTenant
)
{
_cache = cache;
_codesTreeRepository = codesTreeRepository;
_currentTenant = currentTenant;
}
private string GetCacheKey(string definitionNo)
{
var currentTenant = _currentTenant.Id.HasValue ? _currentTenant.Id.Value.ToString() : "host";
return $"{currentTenant}-{definitionNo}";
}
public async Task<Codes> GetDefinitionAsync(string definitionNo)
{
var result = await _cache.GetOrAddAsync(
GetCacheKey(definitionNo),
async () =>
{
return await _codesTreeRepository.GetDefinitionAsync(definitionNo).ConfigureAwait(false);
},
() => new DistributedCacheEntryOptions
{
SlidingExpiration = TimeSpan.FromHours(1)
}
).ConfigureAwait(false);
return result;
}
public async Task ClearCacheAsync(string definitionNo)
{
await _cache.RemoveAsync(GetCacheKey(definitionNo)).ConfigureAwait(false);
}
}
thanks @yinchang0626