You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于EFCoreSecondLevelCacheInterceptor手动失效特定表缓存的问询

针对EFCoreSecondLevelCacheInterceptor特定表缓存失效的解决方案

该库支持针对特定表的手动缓存失效,具体实现方式如下:

  • 核心实现:利用IEFCacheServiceProvider接口的InvalidateCacheDependenciesAsync方法,传入目标表的缓存依赖键即可精准失效关联缓存。

    • 表的缓存依赖键可通过EFCacheDependenciesUtilities.GetCacheDependencyKeyForTable方法生成(默认基于表名,区分大小写,与数据库配置一致)。
    • 示例代码:
      // 注入IEFCacheServiceProvider实例
      private readonly IEFCacheServiceProvider _cacheService;
      
      public async Task InvalidateTableCacheAsync(string targetTableName)
      {
          var tableDependencyKey = EFCacheDependenciesUtilities.GetCacheDependencyKeyForTable(targetTableName);
          await _cacheService.InvalidateCacheDependenciesAsync(new[] { tableDependencyKey });
      }
      
  • 原理说明:当拦截器缓存查询结果时,会自动为查询关联其涉及的所有表的依赖键。调用上述方法传入指定表的依赖键,只会清除所有关联该表的缓存条目,不会影响其他表的缓存数据。

  • 适配你的跨主机场景:

    • 写服务在数据变更时,将变更的表名随信号一同发送给只读服务;
    • 只读服务接收信号后,调用上述方法仅失效对应表的缓存,无需全量清除所有缓存。

内容的提问来源于stack exchange,提问作者ZorgoZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 11:57:37