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

Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

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?

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • 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;		
    	}
    }
    
    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • 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;	
        }     
        	
    }
    
    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • 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.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • 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.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    moinahmed created

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

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • 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

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on September 16, 2026, 14:50
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.