- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 CacheService
使用 GetOrCreateAsync
基于键创建缓存。我正在缓存一个照片实体,它有一个 byte[]
属性(property)。
这可以很好地缓存并按预期检索。但是,如果照片实体已更新,缓存仍会像您预期的那样保留旧实体,因为它尚未过期,如何在保存此实体时强制更新缓存?我是否删除现有的缓存实体并重新添加更新的实体?
我的 FromCacheAsync
的例子我的方法 CacheService
public async Task<T> FromCacheAsync<T>(string entityName, int clientId, Func<Task<T>> function)
{
string cacheKey = GetClientCacheKey(entityName, clientId, function);
if (!_cache.TryGetValue(cacheKey, out T entry))
{
async Task<T> factory(ICacheEntry cacheEntry)
{
return await function();
}
return await _cache.GetOrCreateAsync(cacheKey, factory);
}
return entry;
}
var existingPhotograph = await _cacheService.FromCacheAsync(nameof(_context.Photograph), clientId, async () =>
await _photographRepository.GetByStaffIdAsync(staff.StaffId));
最佳答案
当实体更改时,您需要使缓存键无效。
如果直接对 DbContext 进行操作,那可能有点棘手。但是由于您使用的是存储库模式,所以更容易做到。
归结为注入(inject)IMemoryCache
进入您的存储库并在更新图片时使其无效。
public class PhotographRepository : IPhotograpRepository
{
private readonly IMemoryCache _cache;
public PhotographReposiory(IMemoryCache cache, ...)
{
_cache = cache ?? throw new ArgumentNullException(nameof(cache));
}
public async Task Update(PhotographEntity entity)
{
// update your entity here
await _context.SaveChangesAsync();
// this invalidates your memory cache. Next call to _cache.TryGetValue
// results in a cache miss and the new entity is fetched from the database
_cache.Remove(GetClientCacheKey(entityName, clientId));
}
}
public class PhotographRepository : IPhotograpRepository
{
private readonly ApplicationDbContext _context;
public PhotographReposiory(ApplicationDbContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
}
public async Task Update(PhotographEntity entity)
{
// update your entity here
await _context.SaveChangesAsync();
}
}
public class CachedPhotographRepository : IPhotograpRepository
{
private readonly IMemoryCache _cache;
private readonly IPhotograpRepository _repository;
public CachedPhotographRepository(IPhotograpRepository repository, IMemoryCache cache)
{
_cache = cache ?? throw new ArgumentNullException(nameof(cache));
_repository = _repository ?? throw new ArgumentNullException(nameof(repository));
}
public async Task Update(PhotographEntity entity)
{
// do the update in the passed in repository
await _repository.Update(entity);
// if no exception is thrown, it was successful
_cache.Remove(GetClientCacheKey(entityName, clientId));
}
}
services.AddScoped<IPhotograpRepository>(provider =>
// Create an instance of PhotographRepository and inject the memory cache
new CachedPhotographRepository(
// create an instance of the repository and resolve the DbContext and pass to it
new PhotographRepository(provider.GetRequiredService<ApplicationDbContext>()),
provider.GetRequiredService<IMemoryCache>()
)
);
PhotographRepository
使用 IoC 容器并解决它。但这也将允许您注入(inject)
PhotographRepository
进入您的服务,而上述阻止它,因为只有
IPhotographRepository
接口(interface)已注册。
关于c# - 实体更改后更新 IMemoryCache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48743516/
我有一个 CacheService使用 GetOrCreateAsync基于键创建缓存。我正在缓存一个照片实体,它有一个 byte[]属性(property)。 这可以很好地缓存并按预期检索。但是,如
我正在尝试将我的 .Net 框架应用程序迁移到 .Net Core,在此过程中,我想将我的内存缓存从 System.Runtime.Caching/MemoryCache至Microsoft.Exte
我创建了一个全新的 ASP.NET Core Web API 项目。这是 Startup.cs 中的 ConfigureServices: public void ConfigureServi
我对 ASP.Net Core、C#、OOP、Javascript ……基本上是我目前使用的所有内容的新手。过去几个月我一直在阅读和学习以开始一个新的开发项目。总而言之,我正在稳步前进,但我遇到了 I
我正在使用 asp net core 1.0 和 xunit。 我正在尝试为一些使用 IMemoryCache 的代码编写单元测试。但是,每当我尝试在 IMemoryCache 中设置一个值时,我都会
我正在尝试模拟 IMemoryCache最小起订量。我收到此错误: An exception of type 'System.NotSupportedException' occurred in Mo
我目前正在执行以下操作以将列表存储在 IMemoryCache 中: _memoryCache.Set("PriceList", horsepricelist); 并访问它/返回数据: public
我正在创建一个 .net core 3.1 Worker Service 应用程序。我有一个使用 IMemoryCache 接口(interface)进行依赖注入(inject)的类。我有以下类的构造
我正在使用 IMemoryCache缓存从身份服务器检索到的 token 。 过去我用过 GetOrCreateAsync Microsoft.Extensions.Caching.Abstracti
我有一个从 Visual Studio 2015 Community Edition 创建的 ASP.NET Web 应用程序。 .NET Framework 是 4.6.1。 我不知道我的 Web
我一直在使用类似于示例 1 的操作来为我的 .NET Core API 异步缓存 json 结果。 MemoryCache 是 IMemoryCache 的实例。 示例 1(按预期工作): [Http
我正在尝试使用 Microsoft.Extensions.Caching.Memory.IMemoryCache 接口(interface)/类。 我需要向缓存中添加一个新项目,并确保我不会覆盖任何其
我有一个带有 API 的 ASP.NET Core MVC 项目。 然后我在名为 Infrastructure. 的同一个解决方案中有一个类库。 我的 API 在类库基础结构中调用存储库方法,在类 U
我们正在使用 .NET Core 3.1 和 Microsoft.Extensions.Caching.Memory.IMemoryCache (v3.1.24) 及其实现 Microsoft.Ext
.Net Core 为两个接口(interface)( MemoryCache 和 DistributedMemoryCache )提供内存实现,但假设我们有一个可用的 IDistributedCac
我知道 IMemoryCache.Set 是一个扩展方法,所以它不能被模拟。人们已经为这种情况提供了解决方法,例如 NKosi here 的解决方法。 .我想知道如何为我的 MemoryCache 返
我正在寻找一种方法来包装为某些数据调用远程 WCF 服务的服务。例如,这样的服务可能看起来像这样 public interface IAsyncSvc { Task GetData(int k
我正在编写一个简单的测试用例来测试我的 Controller 在调用我的服务之前调用缓存。我正在使用 xUnit 和 Moq 来完成这项任务。 我遇到了一个问题,因为 GetOrCreateAsync
我向 IMemoryCache 添加一个值 _cache.Set("Key", "Value", DateTimeOffset.UtcNow); 我想通过其他方法获取这个key的绝对过期值。 或者 我
我正在使用 IMemoryCache 将数据存储在缓存中...但它会不断向 SQL Server 发送 sql 调用,即使在缓存存在的情况下执行不会运行该代码。 我的查找服务 public class
我是一名优秀的程序员,十分优秀!