简体   繁体   English

Ruby:如何在代码中存储大型数组

[英]Ruby: How to store large arrays in code

I have a controller in my rails app. 我的rails应用程序中有一个控制器。 It's a vulnerability scanner in which the signatures are read using nokogiri from an xml file every time the code is executed. 这是一个漏洞扫描程序,每次执行代码时都会使用nokogiri从xml文件中读取签名。 This is clearly not the best way to do this. 这显然不是最好的方法。

Currently there are 4 arrays, each with ~600 elements, it's not much, but the code will be executed a few thousand times per day (lots of scans) so it all adds up, I have a small vps (2gb ram, 2 cores). 目前有4个阵列,每个有大约600个元素,但并不多,但代码每天会执行几千次(大量扫描)所以这一切都加起来,我有一个小的vps(2gb内存,2个内核) )。 Storing them in the code makes it look messy and I'm not sure if storing it in the code is considered best practice or not. 将它们存储在代码中使它看起来很混乱,我不确定将它存储在代码中是否被视为最佳实践。

What is the best way to go about doing this? 这样做的最佳方法是什么? Is there a way to cache this in the memory so that it doesn't read the file every time? 有没有办法在内存中缓存它,以便它不会每次都读取文件? How can I optimize this process? 我该如何优化此流程?

Isn't this what memcached does? 这不是memcached的作用吗?

Also, for a Phusion Passenger server, anything you load while config.ru is running will exist pre-fork. 此外,对于Phusion Passenger服务器,在config.ru运行时加载的任何内容都将存在于pre-fork之前。 This will then be available with essentially no overhead to every controller. 然后,这将基本上没有每个控制器的开销。

Memcached could help you here. Memcached可以帮到你。 Have a look at this . 看看这个

I would create a local service with all necessary data already loaded on memory, just waiting for function call. 我会创建一个本地服务,所有必要的数据已经加载到内存中,只是等待函数调用。

This can be easily implemented with EventMachine Ruby library. 使用EventMachine Ruby库可以轻松实现这一点

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

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