Open Closed

About the problem concerning the distributed cache KeyPrefix... #9560


User avatar
0
rexhxiao created

We typically divide the system into a backend and a public site, and use prefixes like AbpDistributedCacheOptions options.KeyPrefix = "MySitePublic:" to distinguish between different caches.

However, we’ve designed some FeatureDefinition configurations and use the SaaS feature system to manage tenant-specific functionality. The issue is that the public site’s cache doesn’t update immediately when these features change.

Are there any effective mechanisms to handle this problem—not just for Feature caching, but also for broader cache synchronization?


1 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    • To distinguish cache data between your backend and public site, setting a unique KeyPrefix via AbpDistributedCacheOptions is correct. This ensures cache isolation, but it does not provide automatic cache synchronization between applications.

    • For feature management and other cross-app cache scenarios, ABP’s distributed cache (e.g., Redis) does not natively propagate cache invalidation events across different applications or key prefixes. This means that when a feature value changes (such as via the SaaS feature system), the cache in the public site will not be immediately updated unless the cache entry expires or is manually cleared.

    • To address this, ABP v9+ introduces HybridCache (AbpHybridCache), which combines local (in-memory) and distributed cache with automatic synchronization. When a cache entry is updated in one instance, HybridCache can notify other instances to invalidate their local cache, ensuring more immediate consistency across your backend and public site.

    • To use HybridCache in ABP:

      1. Upgrade to ABP v9.0+.
      2. Configure AbpHybridCacheOptions in your modules.
      3. Use IHybridCache<T> instead of IDistributedCache<T> for shared cache scenarios.

      Example configuration:

      Configure<AbpHybridCacheOptions>(options =>
      {
          options.GlobalHybridCacheEntryOptions = new HybridCacheEntryOptions
          {
              Expiration = TimeSpan.FromMinutes(20),
              LocalCacheExpiration = TimeSpan.FromMinutes(10)
          };
      });
      

      Example usage:

      public class FeatureAppService : ApplicationService
      {
          private readonly IHybridCache<FeatureCacheItem> _hybridCache;
      
          public FeatureAppService(IHybridCache<FeatureCacheItem> hybridCache)
          {
              _hybridCache = hybridCache;
          }
      
          public async Task<FeatureCacheItem> GetFeatureAsync(string key)
          {
              return await _hybridCache.GetOrCreateAsync(key, async () =>
              {
                  // Load feature from source
              });
          }
      }
      
    • For broader cache synchronization (not just features), use HybridCache for any shared data that must be consistent across applications. This approach ensures that cache invalidation and updates are propagated efficiently.

    • If you remain on distributed cache only, you must implement manual cache clearing or use shorter expiration times, but this is less efficient and can lead to stale data.

    Sources:


    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

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.0.0-preview. Updated on July 09, 2025, 06:20