简体   繁体   English

如何在休息服务中提高文件 IO 性能

[英]How to improve file IO performance in a rest service

The scenario I have at hand is from my spring boot rest service, read a word doc from resources folder and pass the byte array to the client我手头的场景来自我的 spring boot rest 服务,从资源文件夹中读取一个 word doc 并将字节数组传递给客户端

I read the word doc in memory using FileInputStream, convert input stream to a byte array using Apache Common IO IOUtils and place it in the response body of the rest service.我使用 FileInputStream 在内存中读取单词 doc,使用 Apache Common IO IOUtils 将输入流转换为字节数组,并将其放置在其余服务的响应正文中。

The problem here is that I always read the file in memeirh oer service request which is detrimental for there local memory of the process where service is running on.这里的问题是我总是在 memeirh 或服务请求中读取文件,这对运行服务的进程的本地内存有害。

I can't read the file line by line and return it to the service caller in that fashion as I need to return the byte array back to the caller all together我无法逐行读取文件并以这种方式将其返回给服务调用方,因为我需要将字节数组一起返回给调用方

Another problem I foresee is with how the file is read.我预见的另一个问题是文件的读取方式。 I want to be a non blocking IO instead of a blocking IO.我想成为一个非阻塞 IO 而不是阻塞 IO。

Wondering what would be an efficient way to solve this想知道解决这个问题的有效方法是什么

Do you actually need to read the file every time a request comes in.每次请求进来时,您是否真的需要读取文件。

Otherwise you could just read the file on server startup and then keep the file in memory stored in a Spring Bean.否则,您可以在服务器启动时读取文件,然后将文件保存在存储在 Spring Bean 中的内存中。 Then fetch it from there on every call?然后在每次通话时从那里获取它?

If you don't want to upload file every time, it's better to create the @Bean , doing that in init/postconstruct phase.如果您不想每次都上传文件,最好在 init/postconstruct 阶段创建@Bean You also can add some functionality to your retrieve() method, which checks and stores file modification time with invokation of File.lastModified() to decide whether you have to reload the content or not.您还可以向retrieve()方法添加一些功能,该方法通过调用File.lastModified()来检查和存储文件修改时间,以确定是否必须重新加载内容。

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

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