node.js - 上百(有限张)图片,需要在NodeJS中使用,如何最有效的利用内存
怪我咯
怪我咯 2017-04-17 16:08:48
0
1
690

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

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

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

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

缺点:应用占用内存大

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

优点:内存占用小

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

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

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

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all (1)
巴扎黑

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.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!