开发者

Cache Wrapper with expressions

I dont know if is possible.

I want a class to encapsulate all Cache of my site. I thinking about the best way to do this to avoid conflict with keys.

My first idea is something like this:

    public static TResult Cachear<TResult>(this Cache cache, Expression<Func<TResult>> funcao)
    {
        string chave = funcao.ToString();

        if (!(cache[chave] is TResult))
        {
            cache[cha开发者_运维知识库ve] = funcao.Compile()();
        }

        return (TResult)cache[chave];
    }

Is the best way? Ty


Expression.ToString() is rather expensive.

The other problem is that Expression's are always freshly created objects, so they will never have the same reference, so using that as a key is problematic.

One last issue (that does not affect you (yet)), is considering all possible parameter combination's.

The only thing I can suggest is to forget about Expression's, and just use Func<R> directly.


I did a class that do this. At codeplex: http://fujiy.codeplex.com/SourceControl/changeset/view/5d9d4a292597#Misc/Fujiy.Util/Caching/CacheHelper.cs

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜