用于 C# 的磁盘支持字典/缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/408401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disk backed dictionary/cache for c#
提问by BCS
I'm looking for a drop in solution for caching large-ish amounts of data.
我正在寻找一种用于缓存大量数据的解决方案。
related questions but for different languages:
相关问题,但针对不同语言:
Close question in different terms:
用不同的术语结束问题:
I don't need (or want to pay anything for) persistence, transactions, thread safety or the like and want something that is not much more complex to use than a List<> or Dictionary<>.
我不需要(或不想为此支付任何费用)持久性、事务、线程安全等,并且想要使用起来并不比 List<> 或 Dictionary<> 复杂得多的东西。
If I have to write code, I'll just save everything off as files in the temp directory:
如果我必须编写代码,我会将所有内容保存为临时目录中的文件:
string Get(int i)
{
File.ReadAllText(Path.Combine(root,i.ToString());
}
In my cases in index will be an int
(and they should be consecutive or close enough) and the data will be a string
so I can get away with treating both a PODand would rather go ultra-light and do exactly that.
在我的情况下,索引将是一个int
(并且它们应该是连续的或足够接近)并且数据将是一个,string
这样我就可以摆脱对POD 的处理,而宁愿变得超轻并做到这一点。
The usage is that I have a sequence of 3k files (as in file #1 to #3000) totaling 650MB and need to do a diff for each step in the sequence. I expect that to total about the same or a little more and I don't want to keep all that in memory (larger cases may come along where I just can't).
用法是我有一个总共 650MB 的 3k 文件序列(如文件 #1 到 #3000),需要为序列中的每个步骤做一个差异。我希望总数大致相同或更多,我不想将所有这些都保留在内存中(更大的情况可能会出现在我无法做到的地方)。
A number of people have suggested different solutions for my problem. However none seem to be targeted at my little niche. The reasons that I'm looking at disk backed caching is because I'm expecting that my current use will use up 1/3 to 1/2 of my available address space. I'm worried that larger cases will just flat run out of space. I'm not worried about treading, persistence or replication. What I'm looking for is a minimal solution using a minimum of code, a minimal usage foot print, minimal in memory overhead and minimum complexity.
许多人为我的问题提出了不同的解决方案。然而,似乎没有一个是针对我的小利基的。我查看磁盘支持缓存的原因是因为我预计我当前的使用将使用我可用地址空间的 1/3 到 1/2。我担心较大的箱子会耗尽空间。我不担心踩踏、坚持或复制。我正在寻找的是使用最少代码、最少使用量、最少内存开销和最少复杂性的最少解决方案。
I'm starting to think I'm being overly optimistic.
我开始觉得我过于乐观了。
回答by leora
you can use the MS application blockwith disk based cache solution
您可以将MS 应用程序块与基于磁盘的缓存解决方案一起使用
回答by Saif Khan
回答by Daniel Paull
Disclaimer - I am about to point you at a product that I am involved in.
免责声明 - 我将向您介绍我参与的产品。
I'm still working on the web site side of things, so there is not a lot of info, but Serial Killerwould be a good fit for this. I have examples that use .Net serialization (can supply examples), so writing a persistent map cache for .Net serializable objects would be trivial.
我仍然在网站方面工作,所以没有很多信息,但连环杀手很适合这个。我有使用 .Net 序列化的示例(可以提供示例),因此为 .Net 可序列化对象编写持久映射缓存将是微不足道的。
Enough shameless self promotion - if interested, use the contact link on the website.
足够无耻的自我宣传 - 如果有兴趣,请使用网站上的联系链接。
回答by Scott Wisniewski
What you really want is a B-Tree. That's the primary data structure that a database uses. It's designed to enable you to efficiently swap portions of a data structure to and from disk as needed.
你真正想要的是一个 B 树。这是数据库使用的主要数据结构。它旨在使您能够根据需要有效地将数据结构的部分交换到磁盘或从磁盘交换。
I don't know of any widely used, high quality standalone B-Tree implementations for C#.
我不知道有任何广泛使用的、高质量的 C# 独立 B 树实现。
However, an easy way to get one would be to use a Sql Compact database. The Sql Compact engine will run in-process, so you don't need a seperate service running. It will give you a b-tree, but without all the headaches. You can just use SQL to access the data.
但是,获得它的一种简单方法是使用 Sql Compact 数据库。Sql Compact 引擎将在进程内运行,因此您不需要运行单独的服务。它会给你一个 b 树,但没有所有的头痛。您可以仅使用 SQL 来访问数据。
回答by Timur Fanshteyn
I've partially poprted EhCache Java application to .NET The distributed caching is not yet implemented, but on a single node, all original UnitTests pass. Full OpenSource:
我已经将 EhCache Java 应用程序部分弹出到 .NET 分布式缓存尚未实现,但在单个节点上,所有原始单元测试都通过了。完全开源:
http://sourceforge.net/projects/thecache/
http://sourceforge.net/projects/thecache/
I can create a binary drop if you need it (only sourcecode is availble now)
如果您需要,我可以创建一个二进制 drop(现在只有源代码可用)
回答by Mauricio Scheffer
I'd take the embedded DB route (SQLite, Firebird), but here are some other options:
我会采用嵌入式数据库路线(SQLite、Firebird),但这里有一些其他选择:
- Berkeley DB: not your standard SQL embedded DB, but seems to be the right choice for this kind of task, although not easily usable from .net
- db4o: an OODB, very simple interface
- Berkeley DB:不是您的标准 SQL 嵌入式 DB,但似乎是此类任务的正确选择,尽管不容易从 .net 中使用
- db4o: OODB,非常简单的界面
回答by casperOne
I recommend the Caching Application block in the Enterprise Library from MS. That was recommended as well, but the link points to an article on the Data Access portion of the Enterprise Library.
我推荐 MS 企业库中的缓存应用程序块。这也是推荐的,但链接指向一篇关于企业库数据访问部分的文章。
Here is the link to the Caching Application Block:
这是缓存应用程序块的链接:
http://msdn.microsoft.com/en-us/library/cc309502.aspx
http://msdn.microsoft.com/en-us/library/cc309502.aspx
And specifically, you will want to create a new backing store (if one that persists to disk is not there):
具体来说,您需要创建一个新的后备存储(如果不存在持久存储到磁盘的存储):
回答by Daniel Paull
Given your recent edits to the question, I suggest that you implement the solution noted in your question as you are very unlikely to find such a naive solution wrapped up in a library for you to reuse.
鉴于您最近对该问题的编辑,我建议您实施问题中提到的解决方案,因为您不太可能找到这样一个包含在库中供您重用的幼稚解决方案。
回答by Sam Saffron
This is very similar to my question
这与我的问题非常相似
Looking for a simple standalone persistant dictionary implementation in C#
I don't think a library that exactly fits what you want exists, maybe its time for a new project on github.
我认为不存在完全适合您想要的库,也许是时候在 github 上创建一个新项目了。
回答by Luke Quinane
Here is a B-Tree implementation for .net: http://bplusdotnet.sourceforge.net/
这是 .net 的 B 树实现:http: //bplusdotnet.sourceforge.net/