我正在尝试实现基于 ObjectCache 的FileCache ( https://github.com/acarteas/FileCache )。我正在尝试检查缓存对象是否存在,或者在需要时添加它并返回。但是,当对象不存在时,不会执行委托并引发错误: [...] 中的类型“myNamespace.Controllers.ListController+<>c__DisplayClass0_0”未标记为可序列化。
我尝试过的(简化):
private string generateString(int? Id)
{
return "String";
}
public ActionResult GetSomething(int? Id)
{
var cacheFilePath = $"{AppDomain.CurrentDomain.BaseDirectory}{"\\cache"}";
var cache = new FileCache(cacheFilePath, false, TimeSpan.FromSeconds(30));
if (purgeCache)
cache.Remove($"CacheKey{Id}");
Func<string> createString = () => generateString(Id);
var myString = (string) cache.AddOrGetExisting($"CacheKey{Id}", createString, new CacheItemPolicy() { AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(30) });
return new JsonStringResult(myString);
}
好的,现在我尝试使用 Serializable 指定委托 createString,但这不起作用。有人能指出我正确的方向吗?
本质上我想要: - 运行一个返回 generateString(123) 的先前输出的语句;如果它不存在或过期,它应该重新生成它。
感谢您的任何提示!