建议使用Redis驱动程序在Goliath中使用?

时间:2011-11-11 00:34:22

标签: ruby redis eventmachine goliath

在EventMachine中建立Redis连接似乎有几种选择,我很难理解它们之间的核心差异。

我的目标是在Goliath

中实施Redis

我现在建立连接的方式是通过 em-synchrony

require 'em-synchrony'
require 'em-synchrony/em-redis'

config['redis'] = EventMachine::Synchrony::ConnectionPool.new(:size => 20) do 
  EventMachine::Protocols::Redis.connect(:host => 'localhost', :port => 6379)
end 

上述内容之间有什么区别,并使用 em-hiredis 之类的内容?

如果我使用Redis进行设置和基本密钥:值存储, em-redis 是我方案的最佳解决方案吗?

2 个答案:

答案 0 :(得分:2)

我们在歌利亚内部非常成功地使用了em-hiredis。以下是我们编码发布的示例:

配置/ example_api.rb

# These give us direct access to the redis connection from within the API
config['redisUri'] = 'redis://localhost:6379/0'
config['redisPub'] ||= EM::Hiredis.connect('')

example_api.rb

class ExampleApi < Goliath::API

  use Goliath::Rack::Params             # parse & merge query and body parameters
  use Goliath::Rack::Formatters::JSON   # JSON output formatter
  use Goliath::Rack::Render             # auto-negotiate response format

  def response(env)
    env.logger.debug "\n\n\nENV: #{env['PATH_INFO']}"
    env.logger.debug "REQUEST: Received"
    env.logger.debug "POST Action received: #{env.params} "

    #processing of requests from browser goes here

    resp =
      case env.params["action"]
      when 'SOME_ACTION'        then process_action(env)
      when 'ANOTHER_ACTION'     then process_another_action(env)
      else
        # skip
      end

    env.logger.debug "REQUEST: About to respond with: #{resp}"

    [200, {'Content-Type' => 'application/json', 'Access-Control-Allow-Origin' => "*"}, resp]
  end

  # process an action
  def process_action(env)
    # extract message data
    data = Hash.new
    data["user_id"], data["object_id"] = env.params['user_id'], env.params['object_id']

        publishData = { "action"   => 'SOME_ACTION_RECEIVED',
                        "data" => data }

        redisPub.publish("Channel_1", Yajl::Encoder.encode(publishData))

      end 
    end 
    return data
  end

  # process anothr action
  def process_another_action(env)
    # extract message data
    data = Hash.new
    data["user_id"], data["widget_id"] = env.params['user_id'], env.params['widget_id']

        publishData = { "action"   => 'SOME_OTHER_ACTION_RECEIVED',
                        "data" => data }
        redisPub.publish("Channel_1", Yajl::Encoder.encode(publishData))

      end 
    end 
    return data
  end
end

处理订阅留给读者练习。

答案 1 :(得分:1)

em-redis宝石可以修补em-redis宝石,使其与纤维一起使用,从而有效地让它在巨人中运行。

这是一个使用Goliath + Redis的项目,它可以指导您如何使所有这些工作:https://github.com/igrigorik/mneme

em-hiredis的例子,goliath做的是将你的请求包裹在光纤中,以便测试它是:

require 'rubygems'
require 'bundler/setup'

require 'em-hiredis'
require 'em-synchrony'

EM::run do
  Fiber.new do
    ## this is what you can use in goliath
    redis = EM::Hiredis.connect
    p EM::Synchrony.sync redis.keys('*')
    ## end of goliath block
  end.resume

end

和我使用的Gemfile:

source :rubygems

gem 'em-hiredis'
gem 'em-synchrony'

如果您运行此示例,您将获得屏幕上打印的redis数据库中已定义键的列表。 如果没有EM :: Synchrony.sync调用,您将获得延迟,但此处光纤将暂停,直到调用返回并获得结果。