【问题标题】:redis fails to connect in rails (ECONNREFUSED)redis 无法在 rails 中连接(ECONNREFUSED)
【发布时间】:2016-06-04 09:21:44
【问题描述】:

学习在 Rails 中执行自动填充建议的不同方法。现在,尝试基于 Railscast 399 的 Redis,rake 任务停止工作。

控制器:

class VenueSuggestionsController < ApplicationController
  def create
    VenueSuggestion.create(params[:venue_suggestion])
  end

  def index
    render json: VenueSuggestion.terms_for(params[:term])
  end

  private
        def venue_suggestion_params
            params.require(:venue_suggestion).permit(:term, :popularity, venues_attributes: [ :id, :name, :address ])
        end
end

型号

class VenueSuggestion
  def self.terms_for(prefix)
    $redis.zrevrange "venue-suggestions:#{prefix.downcase}", 0, 9
  end

  def self.index_venues
    Venue.find_each do |venue|
      index_term(venue.name)
      index_term(venue.address)
      venue.name.split.each { |t| index_term(t) }
    end
  end

  def self.index_term(term)
    1.upto(term.length-1) do |n|
      prefix = term[0, n]
      $redis.zincrby "venue-suggestions:#{prefix.downcase}", 1, term.downcase
    end
  end
end

耙任务:

namespace :venue_suggestions do
  desc "Generate venue suggestions for event form"
  task :index => :environment do
    VenueSuggestion.index_venues
  end
end

config/initializers/redis.rb

$redis = Redis.new

然后尝试运行它:

$ rake venue_suggestions:index --trace
** Invoke venue_suggestions:index (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute venue_suggestions:index
rake aborted!
Redis::CannotConnectError: Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)

【问题讨论】:

  • 能否也包括 $redis 的定义位置?
  • 此错误通常仅在您的 redis-server 未运行或您在不同端口上运行时发生。
  • 好的,如果我在单独的窗口中运行 redis-server 就可以了,但这是唯一的方法吗?

标签: ruby-on-rails redis


【解决方案1】:

也许,您要做的是在后台运行您的 redis-server。

简单的方法(Tm)

redis-server &

正确的方式(Tm)

# cd into your redis directory(path for mine on OSX below)
cd /usr/local/etc/redis.conf
vim redis.conf
daemonize yes # find this line and change 'no' to 'yes'

然后重启你的redis服务器。

此外,以下是特定于 OSX 的有用说明(如果这是您的平台): https://medium.com/@petehouston/install-and-config-redis-on-mac-os-x-via-homebrew-eb8df9a4f298#.mc9tqg22p

【讨论】:

    猜你喜欢
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 2021-01-13
    • 2020-12-25
    • 1970-01-01
    • 2020-06-13
    • 2013-12-02
    相关资源
    最近更新 更多