Home  >  Q&A  >  body text

node.js - 上百(有限张)图片,需要在NodeJS中使用,如何最有效的利用内存

需求场景(后端生成图片):

应用在几百张图片中随机选取一张图片作为模板,在图片上绘制一些文字,并生成一张图片,传入CDN中,返回用户CDN的图片地址。

方案1:一股脑全部加载到内存

优点:不用判断缓存,速度也最快

缺点:应用占用内存大

方案2:使用到的时候再读取,并设置失效时间

优点:内存占用小

缺点:写不好,容易内存泄露,读文件导致速度变慢

目前采用了方案1,使用了node-canvas

各位有没有更好的建议,或者方案?

怪我咯怪我咯2766 days ago742

reply all(1)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 16:10:48

    For scalability considerations, there may be more pictures in the future, or even unlimited pictures. If they are all stored in the server memory, it will definitely not be suitable; if your service is deployed on Alibaba Cloud, buy oss and use the intranet. When you want to use pictures, get them from OSS and store them back in OSS. If they are not on Alibaba Cloud, you can also buy the corresponding cloud storage on other cloud services. Be careful not to purchase cloud services separately, which will require you to go outside the network. Get the picture. If it is your own server, store it in a database, such as mongo. The above storage is all binary of the stored files. In fact, there is no reading of files or memory leaks. Just write the code well.

    reply
    0
  • Cancelreply