gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:01:44 24 4
gpt4 key购买 nike

在 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 是适合我的场景的最佳解决方案吗?

最佳答案

我们在 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

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

关于ruby - 建议在 Goliath 中使用的 Redis 驱动程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8088314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com