如何缓存从文件中读取的内容 .NET 2.0

发布于 2024-08-09 14:08:42 字数 162 浏览 6 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空袭的梦i 2024-08-16 14:08:42

将缓存与文件依赖一起使用

当缓存未命中时,您将读取您的值,并将它们转换为某些对象类型的列表并缓存它们。好处:

  • 为所有用户统一存储,因为自从您使用HttpModule以来,他们都具有这些价​​值
  • 自动内存利用率管理 - 缓存快速且可靠当服务器内存不足时可能会被丢弃
  • 文件依赖缓存在您更改文件时非常有用,并且您的缓存将自动失效并在新更改的文件的第一个请求中重新读取,这样您就不会必须重新启动应用程序或执行一些其他技巧来刷新缓存值
  • 快速数据读取 - 如果您还在文件读取上转换 CSV 数据,您将获得大量时间,因为您只会解析一次,因此所有后续操作与仅存储 CSV 原始数据并解析读取的每个数据相比,数据读取所需的时间要少得多

附加说明
还应该注意的是,操作系统也会缓存文件。因此,如果您经常使用某些文件,并且在相对较短的时间范围内完成后续读取,则访问速度会更快。 相对,因为这取决于服务器负载、内存使用情况等。这只是我的想法。但它仍然比所描述的缓存慢得多。

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:

  • unified storage for all users, because they all these values since you're using HttpModule
  • automatic memory utilisation management - cache is fast and may get discarded when server is low on memory
  • file dependency cache is great when you change the file and your cache will automatically get invalidated and read anew in the first request from the newly changed file so you don't have to restart you application or do some other tricks to refresh cached values
  • fast data reads - if you also convert CSV data on file read you gain a lot of time because you will only parse it once so all subsequent data reads will take much less time than if you stored CSV raw data only and parse on every data read

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.

驱逐舰岛风号 2024-08-16 14:08:42

假设所有用户都将以相同的方式重定向,应用程序存储是放置此内容的适当位置。会话存储存储当前用户上下文的信息,并且仅供该用户共享。应用程序存储功能保留信息,以便可以在所有用户和访客之间全局共享。

如果此信息相当大(> 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])

差↓一点笑了 2024-08-16 14:08:42

我们通过将规则(或类似规则)存储在应用程序状态中来做到这一点。

我们在 Session_Start 中加载值(进行测试以确保我们只执行一次) - 我在 Application_Start 中有一条评论指出:“移至会话启动,因为它似乎工作得更好”

这样的事情:

Application.Lock();
// Only load the rules if we haven't already
if (Application["RulesCache"] == null)
{
    List<Rule> rules = LoadRules();
    Application["RulesCache"] == rules;
}
Application.Unlock();

我们实际上是比这更复杂一点——但基本前提是有效的。

实际上,您希望将负载封装到单独的例程中,并可能提供一种无需重新启动应用程序即可更新缓存的机制

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:

Application.Lock();
// Only load the rules if we haven't already
if (Application["RulesCache"] == null)
{
    List<Rule> rules = LoadRules();
    Application["RulesCache"] == rules;
}
Application.Unlock();

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

浪漫之都 2024-08-16 14:08:42

有两种方法。创建一个数组变量并将其设置为静态。

public class RulesTable
{
    private static readonly RuleCollection _rules;

    public RuleCollection Rules { get; }
}

在这种情况下,您必须创建一个类似 RuleCollection 的类并使其线程安全。

或者您可以使用简单的缓存

System.Web.HttpContext.Current.Cache.Insert("Rules", RulesArray, new System.Web.Caching.CacheDependency("CSV Filename"));

There are two ways. Create an Array variable and set it to static.

public class RulesTable
{
    private static readonly RuleCollection _rules;

    public RuleCollection Rules { get; }
}

In this case, you have to create a class like RuleCollection and make it threadsafe.

Or you can use a simple Cache.

System.Web.HttpContext.Current.Cache.Insert("Rules", RulesArray, new System.Web.Caching.CacheDependency("CSV Filename"));
蓝海似她心 2024-08-16 14:08:42

字典怎么样?你可以使用 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文