如何在.Net中缓存方法结果
使用 webmethods,使用“CacheDuration”属性缓存结果非常简单。是否有类似的“简单”方法来根据参数缓存非 webmethod 输出(或静态方法)?
我将不胜感激任何帮助。提前致谢!
Using webmethods, caching the results is pretty straight forward using "CacheDuration" attribute. Is there a similar "easy" way to cache non-webmethod outputs (or static methods) based on the parameters?
I would appreciate any help. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实现缓存的最简单方法是在类中使用字典或类似的数据结构将结果保存在内存中以供连续调用。
如果您想尝试自己实现一些东西,这只是一个基本示例。它不支持缓存“逐出”或随时重新加载数据。如果您需要更高级的功能,我建议您在 .NET 框架中查找缓存类或其他第三方库。
The simplest way to implement a cache is to use a Dictionary or similar data structure within your class to hold results in memory for successive calls.
This is just a basic example if you want to try to implement something on your own. It does not support cache "eviction" or re-loading data at any time. If you need more advanced features, I'd recommend looking around in the .NET framework for cache classes or other 3rd party libraries.
这个问题看起来类似于: 是否有缓存功能/C# 中的方法
您想要的正确术语是记忆化。维基百科提供了有关此主题的更多详细信息。不幸的是,没有参考支持它的 C# 库。有多种方法可以自行实现。但由于我没有 C# 经验,所以无法提供更多细节。可以使用的一些技术:AOP、注释、代理。
This question looks similar to: Is there anyway to cache function/method in C#
The right term for what you want is memoization. Wikipedia gives more details on this subjects. Unfortunately there is no reference to a C# library supporting it. There are various ways to implement it yourself. But since I am not experienced in C# I can not give any more detail. Some techniques that could be used: AOP, Annotations, Proxies.