- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要使用 IDistributedCache
在我的应用程序中,它可能是 MemoryDistributedCache
或 RedisCache
作为底层实现。我的理解是这些不是线程安全的,所以我创建了自己的包装器,使用 ReaderWriterLockSlim
.ReaderWriterLockSlim
允许多个线程进行读取或独占访问进行写入。现在我想知道它是否适合这项工作以及是否 MemoryDistributedCache
和 RedisCache
读取方法是线程安全的。
至于整体解决方案 - 我知道从像 Redis 这样的分布式缓存中存储和读取值需要时间。但我没有太多选择,因为我存储的值获取和管理的速度更慢。
这是片段:
...
public async Task<byte[]> GetAsync(string key, CancellationToken token = new CancellationToken())
{
_cacheLock.EnterReadLock();
try
{
return await _cache.GetAsync(GetCacheKey(key), token);
}
finally
{
_cacheLock.ExitReadLock();
}
}
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = new CancellationToken())
{
_cacheLock.EnterWriteLock();
try
{
await _cache.SetAsync(GetCacheKey(key), value, options, token);
}
finally
{
_cacheLock.ExitWriteLock();
}
}
public async Task RemoveAsync(string key, CancellationToken token = new CancellationToken())
{
_cacheLock.EnterWriteLock();
try
{
await _cache.RemoveAsync(GetCacheKey(key), token);
}
finally
{
_cacheLock.ExitWriteLock();
}
}
最佳答案
全部 IDistributedCache
实现应该是线程安全的。如果不是,那么请创建一个错误报告以修复它。
即使其中之一不是线程安全的,使用 ReaderWriterLockSlim
守卫async
代码无效。来自 the docs :
ReaderWriterLockSlim has managed thread affinity; that is, each Thread object must make its own method calls to enter and exit lock modes. No thread can change the mode of another thread.
SemaphoreSlim
而不是
ReaderWriterLockSlim
.
关于c# - 在 .Net Core 3.1 中使 IDistributedCache 线程安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60792384/
IDistributedCache作为标准 API 提供,用于从 ASP.NET 应用程序中访问分布式缓存。提供的 API 非常简单,基本上将缓存呈现为键值对的容器,DistributedCacheE
我正在使用 services.AddDistributedMemoryCache()在我的 .Net Core 3 Web API 中 startup.cs类(class)。当我第一次设置缓存时: p
我有一个 asp.net core 项目,我正在尝试使用 IDistributedCache (Redis)。 我想在自定义类中访问 IDistributedCache,而不仅仅是在 Controll
我将用户生成的一些 key 存储在 IDistributedCache 设置中。 key 是长期存在的,如果用户知道它们中的每一个,它们可以由用户手动撤销: public void Configure
我正在使用 .NET Core 2.0 和 Microsoft.Extensions.Caching.Redis 2.0。为什么 IDistributedCache.SetStringAsync 实际
我有一个基于asp.net core 2.1解决方案使用redis分布式缓存的项目。有些东西我还不明白。 我有一个类 myClassName,它的构造函数需要注入(inject)。 public cl
我有一个 Azure Redis 和 .Net Core 2 的工作实现,使用的代码与此 article 中描述的非常相似 我的问题是,如何从单元测试类实例化缓存实例?我查看了许多资源,但一无所获。
.Net Core 为两个接口(interface)( MemoryCache 和 DistributedMemoryCache )提供内存实现,但假设我们有一个可用的 IDistributedCac
我在 ASP.Net Core 2.2 API 中将 IDistributedCache 与 SQL Server 结合使用。 我注意到过期的记录仍然保留在表中。有没有办法删除缓存表中过期的记录? 最
希望我能得到一些关于哪种缓存机制最适合 Azure 的说明。 目前我的应用程序使用内存中 (IMemoryCache) 来保存经常访问的数据。 此功能是否与在单个 Web 服务器上一样?即存储在内存中
为了实现多个 IDistributedCache 定义,我有以下解决方案: public interface IDBCache : IDistributedCache { } public class
我正在 .net core 2.0 中构建 web api 并将其部署在 kubernetes 上。我想将 IDistributedCache(带有 redis)与哨兵和主/从配置一起使用。我找不到任
我需要使用 IDistributedCache在我的应用程序中,它可能是 MemoryDistributedCache或 RedisCache作为底层实现。我的理解是这些不是线程安全的,所以我创建了自
我正在使用带有 aws redis 缓存的 .net core api (2.1)。我没有看到将到期时间设置为 IDistributedCache.SetAsync 的方法.这怎么可能? 我的代码段如
我有一个 ASP.NET Core2 应用程序。我同时使用内置和 Autofac IoC 容器。我正在 Startup.cs 文件中设置所有组件注册。在执行此操作时,我还设置了我的 DBContext
我想在 ASP.NET Core 项目中实现分布式缓存(Redis)。经过一番研究后,我发现有两种方法可以使用 Startup.cs 和 ConnectionMultiplexer 中的 AddSta
我正在尝试在 MVC 6 中使用 Redis 缓存,但我无法构建我的项目。 我遵循了 https://github.com/aspnet/Caching/tree/dev/samples 中的示例-
我是一名优秀的程序员,十分优秀!