Open Closed

Access host cache from tenant #2447


User avatar
0
bozkan created

Hi,

Is it possible to access host cache from tenant via IDistributedCache?

ps: I cannot use IgnoreMultiTenancyAttribute for the cached type, because it is Identity Server's type and cache for the type: IdentityServer4.Models.Client


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

    hi

    Can you try to switch the current tenant?

    using (CurrentTenant.Change(tenantId))
    {
        //IDistributedCache<IdentityServer4.Models.Client> Cache
    }
    
  • User Avatar
    0
    bozkan created

    Hi maliming,

    Thanks for the reply. The situation is:

    using (CurrentTenant.Change(null))
    {
        // GetAsync works on host! It gets cached item from host.
        var cachedClient = await _clientCache.GetAsync(identityClient.ClientId);
    
        cachedClient.Enabled = false;
    
        // But SetAsync does not work on host! It adds a new cached item under tenant folder on redis.
        // Instead of overwriting the item on host folder.
        await _clientCache.SetAsync(identityClient.ClientId, cachedClient, new DistributedCacheEntryOptions
        {
            AbsoluteExpirationRelativeToNow = _identityServerOptions.Caching.ClientStoreExpiration
        },
        considerUow: true);
    }
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Can you try considerUow: false

  • User Avatar
    0
    bozkan created

    Yeap. When considerUow: false, it works!

    What is the reason of this behavior? Should I give up on considerUow? But I need it, because I execute this cache update together with some database operation .

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can create a new unit of work.

    
    using (CurrentTenant.Change(null))
    {
        using (var uow = _unitOfWorkManager.Begin(
            requiresNew: true
        ))
        {
            // GetAsync works on host! It gets cached item from host.
            var cachedClient = await _clientCache.GetAsync(identityClient.ClientId);
        
            cachedClient.Enabled = false;
        
            // But SetAsync does not work on host! It adds a new cached item under tenant folder on redis.
            // Instead of overwriting the item on host folder.
            await _clientCache.SetAsync(identityClient.ClientId, cachedClient, new DistributedCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = _identityServerOptions.Caching.ClientStoreExpiration
            },
            considerUow: true);
                
            await uow.CompleteAsync();
        }
    }
    
  • User Avatar
    0
    bozkan created

    Thank you

Made with ❤️ on ABP v9.1.0-preview. Updated on November 18, 2024, 05:54