【问题标题】:CacheDependency class in .Net Core to establishes a dependency relationship between cache and file.Net Core中的CacheDependency类建立缓存和文件的依赖关系
【发布时间】:2018-02-22 13:10:47
【问题描述】:

.Net Core 中是否有 CacheDependency 类?好像还没有实现。

我们有任何替代或解决方法吗?或者有没有关于这个类的实施的未来计划?

这是.net框架中支持的非常有用的功能,没有它如何建立缓存和文件之间的依赖关系?

【问题讨论】:

    标签: caching asp.net-core .net-core asp.net-core-2.0 .net-standard


    【解决方案1】:

    您应该能够使用ChangeToken 来实现类似于CacheDependency 的功能。您必须使用文件提供程序之一来获取IChangeToken 的实例。然后可以将此令牌用作缓存的参数。

    非常基本的示例:

    IMemoryCache cache = GetCache();
    string fileContent = GetFileContent("sample.txt");
    
    // instance of file provider should be ideally taken from IOC container
    var fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()); 
    
    IChangeToken token = _fileProvider.Watch("sample.txt");
    
    // file will be removed from cache only if it is changed
    var cacheEntryOptions = new MemoryCacheEntryOptions()
                .AddExpirationToken(changeToken);
    
    // put file content into cache 
    cache.Set("sample.txt", fileContent, cacheEntryOptions);
    

    详细解释见microsoft documentation for change tokens。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-30
      • 1970-01-01
      • 1970-01-01
      • 2014-03-11
      • 2014-06-14
      • 1970-01-01
      • 1970-01-01
      • 2010-10-06
      相关资源
      最近更新 更多