简体   繁体   中英

Ruby: How to store large arrays in code

I have a controller in my rails app. It's a vulnerability scanner in which the signatures are read using nokogiri from an xml file every time the code is executed. 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). 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?

Also, for a Phusion Passenger server, anything you load while config.ru is running will exist pre-fork. This will then be available with essentially no overhead to every controller.

Memcached could help you here. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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