我有一个高效的C#应用程序,它在多线程CPU上以每秒5k到10k记录的速率接收80字节的数据.
我现在需要设置一个内存缓存来检测和过滤重复记录,这样我就可以抑制它们在管道中进一步移动.
缓存规格(最大阈值)
题
设置内存缓存,字典,哈希表,数组等的最佳方法是什么,它将允许最有效的查找,清除旧的缓存数据,并防止被击中的数据到期.
我查看了ASP.Net Cache,System.Runtime.MemoryCache,但我认为我需要一些更轻量级的东西来定制以获得正确的吞吐量.我也在看System.Collections.Concurrent作为替代和相关的白皮书.
有没有人对最佳方法有什么建议?
我需要In-Memory Cache在我的网站上提出申请,.NetFramework 4.5.2但出现了以下例外情况:
Unity.Exceptions.ResolutionFailedException:'解决依赖关系失败,类型='Tranship.UI.Areas.Portal.Controllers.SearchResultController',名称='(无)'。发生以下异常:正在解决。异常是:InvalidOperationException-当前类型Microsoft.Extensions.Caching.Memory.IMemoryCache是一个接口,无法构造。您是否缺少类型映射?
我正在使用Asp.net MVC(不是Core)并使用Microsoft.Extensions.Caching.Memory version 1.1.2
这是我的CS文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tranship.Business.Core;
using Tranship.Business.Interface;
using Tranship.DataAccess.UnitOfWork;
using Tranship.Domain.Context;
using Tranship.Domain.Model;
using Tranship.DomainService.Interface;
using Tranship.ViewModel.Model;
using Tranship.ViewModel.Mapper;
using Tranship.ViewModel.Parameter;
using Microsoft.Extensions.Caching.Memory;
namespace Tranship.DomainService.Core
{
public class ScheduleDomainService : IScheduleDomainService
{
private readonly IMemoryCache MemoryCache;
private readonly string key = "TranshipMemoryCache";
public BoundedContextUnitOfWork Context { get; set; }
public IScheduleBiz ScheduleBiz { get; set; }
public ScheduleDomainService(IMemoryCache memoryCache) …Run Code Online (Sandbox Code Playgroud) 我正在使用.net内存缓存与.NET 4.0和c#,我希望我的应用程序在删除项目时得到通知(所以我可以写它已被删除到日志文件或通知UI,该项目已移除).
无论如何要做到这一点.
我正在使用System.Runtime.Caching.MemoryCache而不是System.Web.Caching
Redis通常用作缓存,尽管它提供的不仅仅是内存缓存(例如,它支持持久性).
人们选择使用Redis而不是.NET的原因是什么MemoryCache?我想到了持久性和数据类型(键值对除外),但我确信使用额外的架构层(即Redis)必须有其他原因.
的扩展GetOrCreateAsync方法IMemoryCache:
public static async Task<TItem?> GetOrCreateAsync<TItem>(this IMemoryCache cache, object key, Func<ICacheEntry, Task<TItem>> factory)
{
if (!cache.TryGetValue(key, out object? result))
{
using ICacheEntry entry = cache.CreateEntry(key);
result = await factory(entry).ConfigureAwait(false);
entry.Value = result;
}
return (TItem?)result;
}
Run Code Online (Sandbox Code Playgroud)
为什么他们会回来TItem?而不是仅仅TItem?假设我的factory方法永远不会返回 null,那么可以安全地假设它永远不会返回null并使用 null-forgiving 运算符忽略它!吗?
public async Task<Foo> GetFooAsync()
=> (await cache.GetOrCreateAsync("Foo", async _ => new Foo()))!
Run Code Online (Sandbox Code Playgroud) 我有这样的缓存服务:
public interface ICacheService {
T Get<T>(string cacheID, Func<T> getItemCallback, int cacheMinutes = 5) where T : class;
}
public class MemoryCacheService : ICacheService {
public T Get<T>(string cacheId, Func<T> getItemCallback, int cacheMinutes = 5) where T : class {
T item = MemoryCache.Default.Get(cacheId) as T;
if (item == null) {
item = getItemCallback();
MemoryCache.Default.Add(cacheId, item,
new CacheItemPolicy {AbsoluteExpiration = DateTime.Now.AddMinutes(cacheMinutes)});
}
return item;
}
}
Run Code Online (Sandbox Code Playgroud)
并检索如下:
var result = _cache.Get("mylist", () => _database.Fetch<MyList>().AsQueryable(), 600);
Run Code Online (Sandbox Code Playgroud)
该列表很大,并且在每个击键类型提前下拉列表中经常访问.查询条件也是动态的,比如
if (this) result = …Run Code Online (Sandbox Code Playgroud) 我正在尝试写一个"简单"的 Generic Get<T>; 延期
System.Runtime.MemoryCache.
为什么"简单"?因为我通常在缓存之前知道对象的真实类型,所以当我从缓存中检索它时,我不会以不可预测的方式转换它.
例如:如果布尔"true"值存储在cacheKey"id"的缓存中,那么
Get<string>("id") == "true";
Get<int>("id") == 1; // any result > 0 is okay
Get<SomeUnpredictableType> == null; // just ignore these trouble conversions
Run Code Online (Sandbox Code Playgroud)
这是我不完整的实现:
public static T DoGet<T>(this MemoryCache cache, string key) {
object value = cache.Get(key);
if (value == null) {
return default(T);
}
if (value is T) {
return (T)value;
}
// TODO: (I'm not sure if following logic is okay or not)
// 1. if T and value …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个类来处理.net核心类库中的内存缓存.如果我不使用核心,那么我可以写
using System.Runtime.Caching;
using System.Collections.Concurrent;
namespace n{
public class MyCache
{
readonly MemoryCache _cache;
readonly Func<CacheItemPolicy> _cachePolicy;
static readonly ConcurrentDictionary<string, object> _theLock = new ConcurrentDictionary<string, object>();
public MyCache(){
_cache = MemoryCache.Default;
_cachePolicy = () => new CacheItemPolicy
{
SlidingExpiration = TimeSpan.FromMinutes(15),
RemovedCallback = x =>
{
object o;
_theLock.TryRemove(x.CacheItem.Key, out o);
}
};
}
public void Save(string idstring, object value){
lock (_locks.GetOrAdd(idstring, _ => new object()))
{
_cache.Add(idstring, value, _cachePolicy.Invoke());
}
....
}
}
}
Run Code Online (Sandbox Code Playgroud)
在.Net核心中,我找不到System.Runtime.Cache.在读取.net核心内存缓存后,我添加了参考Microsoft.Extensions.Caching.Memory(1.1.0)并尝试过
using System.Collections.Concurrent; …Run Code Online (Sandbox Code Playgroud) 我认为我的ASP.NET核心应用程序中的IMemoryCache的标准用法.
在startup.cs我有:
services.AddMemoryCache();
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我有:
private IMemoryCache memoryCache;
public RoleService(IMemoryCache memoryCache)
{
this.memoryCache = memoryCache;
}
Run Code Online (Sandbox Code Playgroud)
然而,当我进行调试时,我最终得到了多个内存缓存,每个缓存中包含不同的项目.我以为内存缓存会是单身?
更新了代码示例:
public List<FunctionRole> GetFunctionRoles()
{
var cacheKey = "RolesList";
var functionRoles = this.memoryCache.Get(cacheKey) as List<FunctionRole>;
if (functionRoles == null)
{
functionRoles = this.functionRoleDAL.ListData(orgId);
this.memoryCache.Set(cacheKey, functionRoles, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromDays(1)));
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在两个不同的浏览器中运行两个客户端,当我点击第二行时,我可以看到this.memoryCache包含不同的条目.
如何从 ASP.NET Core 正确清除 IMemoryCache?
我相信这个类缺少 Clear 方法,但无论如何如何处理它?在我的项目中,我将 DocumentRepository 的方法缓存 24 小时,从数据库中获取大量行。但有时我可以更改数据库,所以我想清除 IMemoryCache,因为它有垃圾数据。
memorycache ×10
c# ×8
caching ×7
.net ×2
asp.net-core ×2
.net-core ×1
asp.net ×1
asp.net-mvc ×1
generics ×1
hashtable ×1
linq ×1
redis ×1