我在Microsoft.Extensions.Caching.Memory.MemoryCache中设置了具有滑动过期的缓存项.我想在每次缓存项到期时触发回调,但在我查询缓存中过期的缓存项之前,不会触发回调.
这是代码:
using System;
using Microsoft.Extensions.Caching.Memory;
namespace Memcache
{
public class Program
{
private static MemoryCache _cache;
private static int _cacheExpSecs;
public static void Main(string[] args)
{
_cache = new MemoryCache(new MemoryCacheOptions());
_cacheExpSecs = 2;
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromSeconds(_cacheExpSecs))
.RegisterPostEvictionCallback(callback: EvictionCallback);
_cache.Set(1, "One", cacheEntryOptions);
_cache.Set(2, "Two", cacheEntryOptions);
var autoEvent = new System.Threading.AutoResetEvent(false);
System.Threading.Timer timer = new System.Threading.Timer(checkCache, autoEvent, 1000, 6000);
Console.Read();
}
private static void checkCache(Object o)
{
if(_cache.Get(1)!=null)
{
Console.WriteLine(string.Format(@"checkCache: Cache with key {0} will be removed …Run Code Online (Sandbox Code Playgroud) 我最近将我的 API 项目更新为 ASP.NET Core 3。从那时起,[JsonIgnore]属性不起作用:
public class Diagnostico
{
[JsonIgnore]
public int TipoDiagnostico { get; set; }
[JsonIgnore]
public int Orden { get; set; }
[JsonIgnore]
public DateTime? FechaInicio { get; set; }
public string TipoCodificacion { get; set; }
public string Codigo { get; set; }
public string Descripcion { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
类的所有属性都被序列化。API 端点在 .NET Core 3 中,但所有逻辑都在 .NET Standard 2.1 中。我意识到序列化程序已从 更改Newtonsoft.Json为
System.Text.Json。此包在 .NET Standard 2.1 中不可用(它仅适用于 .NET Core),因此要[JsonIgnore] …
我正在尝试将一个Task存储在字典中以便以后执行,但没有成功:
Dictionary<string, Task<object>> router = new Dictionary<string, Task<object>();
router["xyz"] = functionToLaterExec
async Task<string> functionToLaterExec()
{
return await Task.FromResult("Success!");
}
Run Code Online (Sandbox Code Playgroud)
如果我将该函数声明为
string functionToExec()
Run Code Online (Sandbox Code Playgroud)
然后我可以这样做:
router["xyz"] = new Task<string>(functionToExec);
Run Code Online (Sandbox Code Playgroud)
但我需要该函数是异步的.并且不知道如何调用它.我试过了:
var t = router["xyz"];
var data = Encoding.UTF8.GetBytes(await t);
Run Code Online (Sandbox Code Playgroud)
没有成功.
谢谢.
asp.net-core ×2
c# ×2
.net ×1
caching ×1
dictionary ×1
dynamic ×1
json ×1
json.net ×1
memorycache ×1