Starts in:
1 DAY
12 HRS
52 MIN
30 SEC
Starts in:
1 D
12 H
52 M
30 S
Open Closed

Cache access across services #4773


User avatar
0
moinahmed created

Hi,

Can we share/access cache across services? I have a service that initializes the cache upon module load, but when I access the same cache into another service I always get a cache miss. I have thoroughly verified that the cache is getting initialized but items not accessible from the other service. Is this a default behavior or am I missing something? If it is something by design how can we overcome this?

Thanks, Moin


7 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you share some code to reproduce?

    Had you checked the cache on Redis?

  • User Avatar
    0
    moinahmed created

    I'm sharing the code below; removing/obfuscating many things for privacy but have included bare-minimum set that give you a clear idea.

    Here's module code:

    [DependsOn(
    	typeof(AbpAutoMapperModule),        
    	typeof(AbpCachingModule)
    	)]
    public class CustomModule : AbpModule
    {
    	public override void ConfigureServices(ServiceConfigurationContext context)
    	{
    		var configuration = context.Services.GetConfiguration();
    		Configure<AbpAutoMapperOptions>(options => {
    			options.AddMaps<CustomModule>();
    		});
    	}
    
    	public override void OnApplicationInitialization(ApplicationInitializationContext context)
    	{
    		var service = context
    			.ServiceProvider
    			.GetRequiredService<CustomInit>();
    		service.Initialize();
    	}
    }
    

    Init class:

    public class CustomInit : ISingletonDependency
    {
    	private IDistributedCache<CacheItem> _cache;	
    
    	public CustomInit(
    		IDistributedCache<CacheItem> cache)
    	{
    		_cache = cache;
    	}
    
    	public void Initialize()
    	{
    		// load records from API
    		var records = client.Records.GetAllAsync(request).Result;
    
    		// initialize abp cache
    		foreach (var rec in records) {
    			_cache.SetAsync(
    				rec.Id, // Cache key
    				new CacheItem { Id = rec.Id, Name = rec.Name }, // Cache value
    				new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromDays(1000) } // Cache options
    			);
    		}
    	}
    }
    

    Service class where I'm accessing cache:

    public class CustomService : ApplicationService, ICustomService, ITransientDependency
    {        
    	private IDistributedCache<CacheItem> _cache;
    	
    	public CustomService(
    		IDistributedCache<CacheItem> cache)
    	{
    		_cache = cache;
    	}
    
    	public async Task<CacheItem> GetAsync(string id)
    	{            
    		var rec = await _cache.GetAsync(id); // rec is always null
    		return rec;		
    	}
    }
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Had you checked the cache on Redis?

    You can try to change the current tenant to host

    public async Task<CacheItem> GetAsync(string id)
    {       
        using (_currentTenant.Change(null))
        {
            var rec = await _cache.GetAsync(id);
            return rec;	
        }     
        	
    }
    
  • User Avatar
    0
    moinahmed created

    Our cache needs are very limited and in-memory/in-process is sufficient for it; that's why do not want to try Redis.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    ok, can you try to change the current tenant to host?

    If it still not working, I will try to reproduce it in a new project.

    You can also try to reproduce it.

  • User Avatar
    0
    moinahmed created

    It didn't work; I tried a new project too. Were you able to reproduce at your end?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Can you share a project and then I will check it locally?

    liming.ma@volosoft.com

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06