如何缓存从文件中读取的内容 .NET 2.0
我有一个 httpmodule,它从 csv 文件读取重定向规则。
但是,由于它是一个 httpmodule 并且它会检查每个请求,因此我显然不想每次都读取该文件。
有哪些策略可以缓存规则(在数组中),以便我不必在每个请求时读取文件?
更新:使用.NET 2.0
I have an httpmodule that reads from a csv file for redirection rules.
However, since it's an httpmodule and it is checking on every request, I obviously don't want to read the file every time.
What are some strategies to cache the rules (in an in Array) so that I do not have to read the file on every request?
Update: Using .NET 2.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将缓存与文件依赖一起使用
当缓存未命中时,您将读取您的值,并将它们转换为某些对象类型的列表并缓存它们。好处:
HttpModule
以来,他们都具有这些价值附加说明
还应该注意的是,操作系统也会缓存文件。因此,如果您经常使用某些文件,并且在相对较短的时间范围内完成后续读取,则访问速度会更快。 相对,因为这取决于服务器负载、内存使用情况等。这只是我的想法。但它仍然比所描述的缓存慢得多。
Use Cache with file dependency
On cache miss you will read your values and also transform them into a list of certain object types and cache them. The benefits:
HttpModule
Additional note
It should be noted as well that OS caches files as well. So if you frequently use certain files access will be quicker on consequent reads if they are done in a relative short time-frame. Relative because this depends on server load, memory usage etc. Just from the top of my head. But it would still be much slower than caching described.
假设所有用户都将以相同的方式重定向,应用程序存储是放置此内容的适当位置。会话存储存储当前用户上下文的信息,并且仅供该用户共享。应用程序存储功能保留信息,以便可以在所有用户和访客之间全局共享。
如果此信息相当大(> 60mb+ 或 1000+ 个对象),您可能需要考虑一个严肃的企业级缓存组件(类似于 JCS/EhCache [但适用于 .net])
The Application storage is the appropriate place to put this assuming that all users will be redirected in the same manner. The Session storage stores information with the context of the current user and is only share for that user. The Application storage functionality keeps the information so that it can be globally shared amongst All users, and guests.
If this information is quite large (>60mb+ or 1000s+ of objects) you might want to consider a serious enterprise level caching component (similar to JCS/EhCache [but for .net])
我们通过将规则(或类似规则)存储在应用程序状态中来做到这一点。
我们在 Session_Start 中加载值(进行测试以确保我们只执行一次) - 我在 Application_Start 中有一条评论指出:“移至会话启动,因为它似乎工作得更好”
这样的事情:
我们实际上是比这更复杂一点——但基本前提是有效的。
实际上,您希望将负载封装到单独的例程中,并可能提供一种无需重新启动应用程序即可更新缓存的机制
We do this by storing the rules (or similar) in the Application state.
We load the values in Session_Start (with a test to ensure that we only do it once) - I have a comment in Application_Start that states: "moved to session start because it seems to work better"
Something like this:
We're actually a little more sophisticated than this - but the basic premise is valid.
Pragmatically, you'd want to encapsulate the load into a separate routine and probably provide a mechanism to update the cache without having to restart the application
有两种方法。创建一个数组变量并将其设置为静态。
在这种情况下,您必须创建一个类似
RuleCollection
的类并使其线程安全。或者您可以使用简单的
缓存
。There are two ways. Create an Array variable and set it to static.
In this case, you have to create a class like
RuleCollection
and make it threadsafe.Or you can use a simple
Cache
.字典怎么样?你可以使用 dict[rule] 来获取你想要的那个。或者比这更复杂?将 csv 转换为 XML 并使用 XQueryXPath 搜索?
How about a Dictionary? You can just grab the one you want using dict[rule]. Or is it more complicated than that? Convert the csv to XML and use XQueryXPath searches?