简体   繁体   English

在C#中缓存流

[英]Caching stream in c#

I have a bunch of files that are being read by another bunch of threads. 我有一堆文件正在被另一堆线程读取。 Some parts of files get read multiple times using multiple file opens. 使用多个文件打开可以多次读取文件的某些部分。 I'd like to have a sort of caching file stream. 我想要一种缓存文件流。

If part of the file was read before, all subsequent reads would come from cache, otherwise file part would be read and content added to cache. 如果以前读取过文件的一部分,则所有后续读取都将从缓存中进行,否则将读取文件的一部分并将内容添加到缓存中。

Files do not change, so there is no need to invalidate cache. 文件不会更改,因此无需使缓存无效。

Is anyone aware of such class being available somewhere or similar mechanism that would help with such problem? 有没有人知道这样的类可以在某个地方使用或类似的机制来解决这个问题?

If the file size is small you can use shared MemoryStream object to cache the file streams. 如果文件很小,则可以使用共享的MemoryStream对象来缓存文件流。 But am not sure about caching the part of a file and identifying the part of a file from cache. 但是不确定是否要缓存文件的一部分并从缓存中识别文件的一部分。

You can cache the whole file in a memorystream and you can use it whenever needed. 您可以将整个文件缓存在一个内存流中,并且可以在需要时使用它。 And you can identify this using the filename as a key. 您可以使用文件名作为密钥来识别它。 For this you have to maintain a list or dictionary kind of structure to store filename (key) - binarystream(value) pairs. 为此,您必须维护列表或字典类型的结构来存储文件名(键)-二进制流(值)对。

these below links discussed the same topics. 以下这些链接讨论了相同的主题。 have a look 看一看

hope this helps 希望这可以帮助

Cheers 干杯

Ramesh Vel 拉梅什·维尔

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

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