3

似乎有几个选项可以建立 Redis 连接以在 EventMachine 中使用,我很难理解它们之间的核心差异。

我的目标是在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是我的方案的最佳解决方案吗?

4

2 回答 2

2

我们在 Goliath 内部非常成功地使用了 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

处理订阅留给读者作为练习。

于 2012-05-18T03:55:19.397 回答
1

em-synchrony 所做的是修补 em-redis gem 以允许将其与纤维一起使用,从而有效地使其在 goliath 中运行。

这是一个使用 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 调用,您将得到一个可延迟的,但在这里光纤被暂停,直到调用返回并且您得到结果。

于 2011-12-06T10:52:02.680 回答