Base solution for your next web application
Open Closed

Cache items by key pattern #5061


User avatar
0
hitaspdotnet created

Hi,

How can you remove items from cache by key pattern when using ITypedCache?

I want remove only just updated entities in myAppService

Please give me an example. Thank you


13 Answer(s)
  • User Avatar
    0
    alper created
    Support Team

    it's simple

    private void RemoveCache()
            {
                _cacheManager.GetCache("MyCacheName").Remove("myCacheKey");
            }
    

    [attachment=1:27jcd50z]remove-cache.jpg[/attachment:27jcd50z]

    and remember [attachment=0:27jcd50z]cache-docs.jpg[/attachment:27jcd50z]

  • User Avatar
    0
    hitaspdotnet created

    Thank you! I missed few lines of documents.

  • User Avatar
    0
    hitaspdotnet created

    Hi again. I can't find my solution in ABP docs. I have a cache named "App.Adresses". In this cache I have more keys pattern named "Addresses." , "AddressAttribute." and "AddressAttributeValue." Then I writing my items with something like this method :

    public async Task<IList<AddressAttributeDto>> GetAllAddressAttributes()
            {
                var key = "AddressAttribute.";
    
                return await _cacheManager.GetCache("App.Adresses").Get(key, async () =>
                 {
                     var query = from aa in (await _addressAttributeRepository.GetAllListAsync())
                                 orderby aa.DisplayOrder, aa.Id
                                 select aa;
                     return ObjectMapper.Map<IList<AddressAttributeDto>>(query.ToList());
                 });
    
           }
    
    public async Task<AddressAttributeDto> GetAddressAttributeById(EntityDto<long> input)
            {
                var key = string.Format("AddressAttribute.id-{0}", input.Id);
    
                return await _cacheManager.GetCache("App.Adresses").Get(key, async () =>
                {
                    var addressAttribute = await _addressAttributeRepository.FirstOrDefaultAsync(input.Id);
                    var output = ObjectMapper.Map<AddressAttributeDto>(addressAttribute);
                });
    
            }
    

    Question is How you can remove all items in App.Addresses WHERE cache item name contains "AddressAttribute." No matter the item identifier and clear all(only AddressAttribute items) .

  • User Avatar
    0
    aaron created
    Support Team

    That's not supported.

    You can replace AbpMemoryCacheManager and AbpMemoryCache, then cast ICache to your implementation to do that.

    Related issue: aspnetboilerplate/aspnetboilerplate#2060 (comment)

  • User Avatar
    0
    hitaspdotnet created

    Hi, Thank you for reply. So how can you get/set item list in memory cache using Abp cache for entities with minor modifications and high usage? e.x Countries list and they States or Provinces (for a address based app)?

  • User Avatar
    0
    hitaspdotnet created

    Could someone explain to me with a real code? For example, How to caching the list of all the countries from DB in the cache then calling the caches for country list output? :( :? :cry: :| :roll:

  • User Avatar
    0
    hitaspdotnet created

    Thanks for any tips! :(

  • User Avatar
    0
    hitaspdotnet created

    Can not guide me? I'm waiting for two days here :cry: :cry: :cry: :cry: :cry:

  • User Avatar
    0
    hitaspdotnet created
    [AutoMapFrom(typeof(Country))]
        public class CountryCacheItem
        {
            public long Id { get; set; }
    
            public string Name { get; set; }
    
            public string TwoLetterIsoCode { get; set; }
    
            public string ThreeLetterIsoCode { get; set; }
    
            public int NumericIsoCode { get; set; }
    
            public bool AllowBilling { get; set; }
    
            public bool AllowShipping { get; set; }
    
            public bool Published { get; set; }
    
            public int DisplayOrder { get; set; }
    
            public ICollection<StateOrProvince> StateOrProvinces { get; set; }
        }
    
    public interface ICountryCache : IEntityCache<CountryCacheItem, long>
        {
        }
    
    public class CountryCache 
            : EntityCache<Country, CountryCacheItem, long>,
            ICountryCache,
            ISingletonDependency
        {
            public CountryCache(ICacheManager cacheManager,
                IRepository<Country, long> repository): base(cacheManager, repository)
            {
            }
        }
    

    What's my next step to get list of countries without query to database

  • User Avatar
    0
    alper created
    Support Team

    see <a class="postlink" href="https://stackoverflow.com/a/47654289/1767482">https://stackoverflow.com/a/47654289/1767482</a>

  • User Avatar
    0
    alper created
    Support Team

    If the current cache mechanism doesn't fit for you can create a new domain service.

    using System.Collections.Generic;
    using Abp.Domain.Repositories;
    using Abp.Domain.Entities;
    using Abp.Domain.Services;
    
    namespace MyCompanyName.AbpZeroTemplate.Friendships.Cache
    {
        public class Country : Entity
        {
            public string Name { get; set; }
    
            public string TwoLetterIsoCode { get; set; }
    
            public string ThreeLetterIsoCode { get; set; }
    
            public int NumericIsoCode { get; set; }
    
            public bool AllowBilling { get; set; }
    
            public bool Published { get; set; }
    
            public int DisplayOrder { get; set; }
        }
    
        public class CountryManager : IDomainService
        {
            private readonly IRepository<Country> _countryRepository;
    
            private List<Country> _countries = null;
    
            public List<Country> Countries
            {
                get
                {
                    if (_countries == null)
                    {
                        _countries = GetAllCountries();
                    }
    
                    return _countries;
                }
            }
    
            public CountryManager(IRepository<Country> countryRepository)
            {
                _countryRepository = countryRepository;
            }
    
            private List<Country> GetAllCountries()
            {
                return _countryRepository.GetAllList();
            }
        }
    
        public class MyTestUsage
        {
            private readonly CountryManager _countryManager;
    
            public MyTestUsage(CountryManager countryManager)
            {
                _countryManager = countryManager;
            }
    
            private void Test()
            {
                var listOfCountries = _countryManager.Countries;
            }
        }
    }
    
  • User Avatar
    0
    hitaspdotnet created

    This made me study more and check UserFriendsCache. I noticed that the UserFriendsCache is a domain service and should coding like a CRUD domain service. A line in the ABP docs made me wrong. The guide says That's it. Our person cache is ready to use!. Because of this, I thought I could use CountryCache like a repository in my AppServices!

    Since my English is weak, this mistake is not understood. Thanks again to the Master @Alper

  • User Avatar
    0
    alper created
    Support Team

    you're welcome ;)