【发布时间】:2014-05-06 16:27:03
【问题描述】:
我有一个 ruby on rails 应用程序,每分钟大约需要 10000 个请求。 其中一些请求执行对数据库表的写入。与数据库的最大连接数为 200。
我想知道什么更有效。在一次操作中写入缓存中的数组并在后台保存数据,还是将每个请求直接保存到数据库? 如果我将数据写入缓存中的数组,是否存在任何竞争条件或性能问题? 有没有更好的方法来优化性能并避免数据库瓶颈?
示例代码
#...
def self.add_data_message_to_queue(event_id, chat_item)
bucket_name = 'BUCKET_GET_CHAT_' + event_id.to_s
chat_queue = Rails.cache.fetch(bucket_name)
if chat_queue.blank?
chat_queue = []
end
chat_queue.push(chat_item)
Rails.cache.write(bucket_name, chat_queue, expires_in:Integer(30).days)
end
- 服务器:独角兽(高并发)
提前致谢
【问题讨论】:
标签: ruby-on-rails postgresql memcached