如何从我的漂亮缓存扩展的lambda表达式中获取参数值?
首先,这个问题可能值得一看: 如何在 ASP.NET MVC 中缓存对象?
有一些伪代码几乎可以满足我的要求:
public class CacheExtensions
{
public static T GetOrStore<T>(this Cache cache, string key, Func<T> generator)
{
var result = cache[key];
if(result == null)
{
result = generator();
cache[key] = result;
}
return (T)result;
}
}
但是,我真正想做的是从生成器自动生成“密钥”。我想我需要将方法签名更改为:
public static T GetOrStore<T>(this Cache cache,
System.Linq.Expressions.Expression<Func<T>> generator)
我想使用方法名称,但也想使用任何参数及其值来生成密钥。我可以从表达式中获取方法主体和参数名称(某种程度),但我不知道如何获取参数值...?
或者我以错误的方式处理这个问题?任何想法都非常感激。
First of all it might be worth looking at this question:
How can I cache objects in ASP.NET MVC?
There some pseudo code that almost does what i want:
public class CacheExtensions
{
public static T GetOrStore<T>(this Cache cache, string key, Func<T> generator)
{
var result = cache[key];
if(result == null)
{
result = generator();
cache[key] = result;
}
return (T)result;
}
}
However, what I'd really like to do, is auto-generate the "key" from the generator. I figure i need to change the method signature to:
public static T GetOrStore<T>(this Cache cache,
System.Linq.Expressions.Expression<Func<T>> generator)
I want to use the method name, but also any parameters and their values to generate the key. I can get the method body from the expression, and the paramter names (sort of), but I have no idea how to get the paramter values...?
Or am I going about this the wrong way? Any ideas much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我是这样做的:
使用这个助手来评估表达式树的节点:
Here's how I did it:
with this helper to evaluate nodes of an expression tree:
当调用生成集合的函数时,我想缓存,我将所有函数的参数和函数名称传递给缓存函数,该函数从中创建一个键。
我的所有类都实现了一个具有 ID 字段的接口,因此我可以在缓存键中使用它。
我确信有更好的方法,但不知怎的,我有时也得睡觉。
我还传递了 1 个或多个关键字,可用于使相关集合无效。
When calling a function that produces a collection i want to cache i pass all my function's parameters and function name to the cache function which creates a key from it.
All my classes implement an interface that has and ID field so i can use it in my cache keys.
I'm sure there's a nicer way but somehow i gotta sleep at times too.
I also pass 1 or more keywords that i can use to invalidate related collections.