Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Entity Cache #9550


User avatar
0
mahendra created

Hi Expert,

This is related to caching in ABP. I am referring to the below link: https://aspnetboilerplate.com/Pages/Documents/Caching

Is the statement "It automatically invalidates a cached entity if this entity is updated or deleted. Thus, it will be retrieved from the database in the next call." still valid. I don't see in my code this getting invalidated.

In the below code: the someMethod gets called once only. even after the productGroup 10000000000001 being updated....

Any clue?

private async Task<ProductGroupDto> someMethod() { return ObjectMapper.Map<ProductGroupDto>(await _productGroupRepository.FirstOrDefaultAsync("10000000000001")); }

var abc = _cacheManager .GetCache(ProductGroupCacheItem.CacheName) .Get("10000000000001", ()=> someMethod());


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @mahendra,

    Could you also share your Entity definition and the code you update this entity ?

    Thanks,

  • User Avatar
    0
    mahendra created

    Here is my Entity Class, CacheItem and Cahe Class...

    `[Table("InvProductGroup")]
    public class ProductGroup : MCFullAuditedEntity
    {
        [Column("ProductGroupKey")]
        [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
        [MaxLength(BaseEntityConsts.MaxKeyLength)]
        public override string Id { get; set; }
    
        [Column("Id")]
        [Required]
        [MaxLength(BaseEntityConsts.MaxEntityIDLength)]
        public virtual string EntityId { get; set; }
    
        [MaxLength(BaseEntityConsts.MaxDescriptionLength)]
        public virtual string Description { get; set; }
    
        
    }
    
    [AutoMapFrom(typeof(ProductGroup))]
    public class ProductGroupCacheItem
    {
        public const string CacheName = "ProductGroup";
        public string EntityId { get; set; }
        public string Description { get; set; }
        
    }
    
    
    public class ProductGroupCache: MustHaveTenantEntityCache<ProductGroup, ProductGroupCacheItem, string>, ITransientDependency
    {
        public ProductGroupCache(ICacheManager cacheManager, IUnitOfWorkManager unitOfWorkManager, IRepository<ProductGroup, string> repository)
        : base(cacheManager, unitOfWorkManager, repository)
        {
        }
    
        
    }`
    
    
    Now in the appService, when I try to get a productGroup having id - 10000000000001 using below code
    
    `var abc = _cacheManager
                .GetCache(ProductGroupCacheItem.CacheName)
                .Get("10000000000001", ()=> someMethod());`
    
    
    it goes to someMethod (below)...which is fine and then in the subsequent call, it does not go into someMethod...which is also fine.    
                
     `private async Task<ProductGroupDto> someMethod() 
        {
            return ObjectMapper.Map<ProductGroupDto>(await _productGroupRepository.FirstOrDefaultAsync("10000000000001"));
        }`
        
        
    

    But when I update the productGroup having id 10000000000001, it updates the productGroup using following code

    `ProductGroup productGroup = await _productGroupRepository.GetAsync(createOrUpdateProductGroupDto.Id); ..... .....

    ObjectMapper.Map(createOrUpdateProductGroupDto, productGroup); await _productGroupRepository.UpdateAsync(productGroup);`

    After the above update, when I try to fetch the productGroup 10000000000001 again, I am expecting the someMethod function to be called as it should invalidate the cache, but it does not....

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @mahendra,

    I tried to reproduce this problem but your code has some problems. ProductGroup doesn't have TenantId field but it must implement IMustHaveTenant interface.

    Could you share classes below without removing anything ?

    1. ProductGroup
    2. MCFullAuditedEntity
    3. ProductGroupCache
    4. and the AppService class