简体   繁体   English

缓存在C#中存储字典的最佳方法

[英]Best way to cache store a dictionary in c#

I am writing out a navigation structure of categories. 我正在编写类别的导航结构。 Each category has an ID, that is located in a 3rd party search tool. 每个类别都有一个ID,该ID位于第3方搜索工具中。 At either app start, or at various intervals, I need to get the caetegories and their id's, and store them in a dictionary (or some other way) so that I can access them for link rewriting from id to name. 无论是在应用启动时,还是在不同的时间间隔中,我都需要获取分类及其ID,并将它们存储在字典中(或以其他方式),以便我可以访问它们以进行从ID到名称的链接重写。

Store the Dictionary in a XML file using a format that looks something like this and read it when you need to do your re-writing 使用类似于以下格式的数据将字典存储在XML文件中,并在需要重写时将其读取

<nodes>
<node id="1">Some Nav Data</node>
</nodes>

You then have 2 options , write a separate process that will update the file at specific intervals or your current process can update the file after a set interval. 然后,您有2个选项,编写一个单独的进程,该进程将以特定的间隔更新文件,或者您当前的进程可以在设置的间隔后更新文件。

If you feel that loading and parsing XML is overkill think about serializing a class to disk that contains a simple hash table or dictionary. 如果您认为加载和解析XML是过分的,请考虑将类序列化到包含简单哈希表或字典的磁盘。

How many settins are you talking about? 您在谈论几个定调子? Why do you need a cache rather than just using an in-memory dictionary with a background thread to reload it periodically? 为什么需要缓存,而不仅仅是使用带有后台线程的内存字典来定期重新加载它?

If you need a cache, you could look at the caching application block in EntLib. 如果需要缓存,则可以查看EntLib中的缓存应用程序块 You can set an expiry interval and then handle an even on expiry to repopulate the cache. 您可以设置一个有效期限,然后处理一个有效期限,以重新填充缓存。 But frankly that sounds overblown for your requirements. 但是坦率地说,这听起来太夸张了。

Caching is a super big topic! 缓存是一个很大的话题! You could use a local cache if you are in a web project. 如果您在Web项目中,则可以使用本地缓存。 You could use HttpContext.Current.Cache if you are in a windows environment (though you don't really need cache in a windows app). 如果您在Windows环境中,则可以使用HttpContext.Current.Cache(尽管您实际上并不需要Windows应用程序中的缓存)。 If you are spanning more than one web server then you might look to the power of MemCached or MemCached Win32. 如果跨多个Web服务器,则可能需要使用MemCached或MemCached Win32的强大功能。 If you don't mind using new stuff you could also take a look at Velocity (which is MS's answer to MemCache's superiority for the past several years in the caching arena). 如果您不介意使用新的东西,您还可以看看Velocity(这是MS过去几年在缓存领域对MemCache优越性的回答)。

Here are some SO posts that might interest you: 以下是一些您可能会感兴趣的帖子:

System.Web.Caching vs. Enterprise Library Caching Block System.Web.Caching与企业库缓存块

Running a Asp.net website with MS SQL server - When should i worry about scalability? 使用MS SQL Server运行Asp.net网站-我何时应该担心可扩展性?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM