That's all. The cache name (in the distributed cache server) will be the full name (with namespace) of theProduct class. You can use the[CacheName] attribute to change it. Please refer to thecaching document for details.
Using a Cache Item Class
In the previous section, we've directly cached theProduct entity. In that case, theProduct class must be serializable to JSON (and deserializable from JSON). Sometimes that might not be possible or you may want to use another class to store the cache data. For example, we may want to use theProductDto class instead of theProduct class for the cached object of theProduct entity.
Assume that we've created aProductDto class as shown below:
public class ProductDto : EntityDto<Guid>{ public string Name { get; set; } public string Description { get; set; } public float Price { get; set; } public int StockCount { get; set; }}
Now, we can register the entity cache services todependency injection in theConfigureServices method of yourmodule class with three generic parameters, as shown below:
Since the entity cache system will perform theobject mapping (fromProduct toProductDto), we should configure the object map. Here, an example configuration withAutoMapper:
public class MyMapperProfile : Profile{ public MyMapperProfile() { CreateMap<Product, ProductDto>(); }}
If you are usingMapperly, you can create a new mapping class that implements theMapperBase<Product, ProductDto> class with the[Mapper] attribute as follows:
[Mapper]public partial class ProductToProductDtoMapper : MapperBase<Product, ProductDto>{ public override partial ProductDto Map(Product source); public override partial void Map(Product source, ProductDto destination);}
Now, you can inject theIEntityCache<ProductDto, Guid> service wherever you want:
public class ProductAppService : ApplicationService, IProductAppService{ private readonly IEntityCache<ProductDto, Guid> _productCache; public ProductAppService(IEntityCache<ProductDto, Guid> productCache) { _productCache = productCache; } public async Task<ProductDto> GetAsync(Guid id) { return await _productCache.GetAsync(id); }}
Notice that the_productCache.GetAsync method already returns aProductDto object, so we could directly return it from our application service.
Configuration
All of thecontext.Services.AddEntityCache() methods get an optionalDistributedCacheEntryOptions parameter where you can easily configure the caching options:
context.Services.AddEntityCache<Product, ProductDto, Guid>( new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(30) });
The default cache duration is2 minutes with theAbsoluteExpirationRelativeToNow configuration.
Additional Notes
Entity classes should be serializable/deserializable to/from JSON to be cached (because it's serialized to JSON when saving in theDistributed Cache). If your entity class is not serializable, you can consider using a cache-item/DTO class instead, as explained before.
Entity Caching System is designed asread-only. You should use the standardrepository methods to manipulate the entity if you need to. If you need to manipulate (update) the entity, do not get it from the entity cache. Instead, read it from the repository, change it and update using the repository.