0

我对 Rack 很陌生,我正在玩它,我很想在我所有的机架应用程序中分享一些会话或哈希表,

假设我有以下 config.ru

require "rack"
require "./my_app.rb"
require "./auth.rb"

use Rack::Session::Pool, :domain => 'example.com', :expire_after => 60 * 60 * 24
use Auth
run MyApp.new

我希望Auth检查内存中的user_id,如果是第一次请求,则从db中获取数据,下次具有相同id的请求不会触发User.find,我不介意任何类型的实现像共享哈希或任何东西,但对于这个例子,我使用了会话 [],我非常感谢它给我任何关于如何使事情工作的提示,我不是在这里寻找真正的身份验证,而不是在这么多的人之间共享一个密钥红宝石工艺。

假设我将在 nginx 下运行 10 个瘦机架实例,所以我希望与所有进程共享会话/哈希。

And auth.rb : 

class Auth

  def initialize(app)
    @app = app
  end

  def call(env)     
    @request = Rack::Request.new(env)
    # here I want to find a way to search for key user_id and if I find it do nothing otherwise to set the value for the key 
    session[:user_id] ||= User.find(request.params[:user_id])
    @app.call(env)
  end
end
4

0 回答 0