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)
-
0
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) -
0
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) -
0
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)