Redis和Rails生产错误:ActionView :: Template :: Error(ERR无效的DB索引)

时间:2014-05-19 00:10:50

标签: ruby-on-rails database caching redis

当我在开发时,我加载页面,它工作正常。没有错误。我在登台时加载页面,然后得到404.我要做的是缓存页面的一部分以提高性能。

这是我在papertrail中发现的错误:

Processing by InstructorsController#index as HTML 
app/web.1:  Read fragment views/staging.example.com/instructors?expires_in=86400/d373c7125bca0fa7485862a5e9f72b99 (10.2ms) 
app/web.1:  Completed 500 Internal Server Error in 238ms 
app/web.1:  cache: [GET /instructors] miss 
app/web.1:  Started GET "/instructors" for 12.34.567.89 at 2014-05-18 23:46:47 +0000 
app/web.1:    Cache digest for app/views/instructors/index.html.erb: d373c7125bca0fa7485862a5e9f72b99 
app/web.1:    Rendered instructors/index.html.erb within layouts/application (14.2ms) 
app/web.1:  ActionView::Template::Error (ERR invalid DB index): 
app/web.1:      1: <% cache expires_in: 1.day do %> 
app/web.1:      2:   <% set_meta_tags keywords: @instructors.keywords %> 
app/web.1:      3:  
app/web.1:      4:   <section id="instructors" class="panel panel-default"> 
app/web.1:    app/views/instructors/index.html.erb:1:in `_app_views_instructors_index_html_erb__1873438065652455666_70314031777140' 
heroku/router:  at=info method=GET path=/instructors host=staging.example.com request_id=c30153a3-bf5d-4e87-ab4f-45e1148db83b fwd="12.34.567.89" dyno=web.1 connect=1ms service=260ms status=500 bytes=1789

现在你知道出现了什么“出现”。为了给你更多的阅读,这就是New Relic为我们所做的:

Error message
ActionView::Template::Error: ERR invalid DB index

Stack trace
…bundle/ruby/2.1.0/gems/redis-3.0.7/lib/redis/client.rb:  97:in `call'
…bundle/ruby/2.1.0/gems/redis-3.0.7/lib/redis/client.rb:  81:in `block in connect'
…bundle/ruby/2.1.0/gems/redis-3.0.7/lib/redis/client.rb: 257:in `with_reconnect'
…bundle/ruby/2.1.0/gems/redis-3.0.7/lib/redis/client.rb:  78:in `connect'
…bundle/ruby/2.1.0/gems/redis-3.0.7/lib/redis/client.rb: 304:in `ensure_connected'
…bundle/ruby/2.1.0/gems/redis-3.0.7/lib/redis/client.rb: 191:in `block in process'
…bundle/ruby/2.1.0/gems/redis-3.0.7/lib/redis/client.rb: 270:in `logging'
…bundle/ruby/2.1.0/gems/redis-3.0.7/lib/redis/client.rb: 190:in `process'
…bundle/ruby/2.1.0/gems/redis-3.0.7/lib/redis/client.rb:  96:in `call'
…vendor/bundle/ruby/2.1.0/gems/redis-3.0.7/lib/redis.rb: 784:in `block in get'
…vendor/bundle/ruby/2.1.0/gems/redis-3.0.7/lib/redis.rb:  37:in `block in synchronize'
       /app/vendor/ruby-2.1.1/lib/ruby/2.1.0/monitor.rb: 211:in `mon_synchronize'
…vendor/bundle/ruby/2.1.0/gems/redis-3.0.7/lib/redis.rb:  37:in `synchronize'
…vendor/bundle/ruby/2.1.0/gems/redis-3.0.7/lib/redis.rb: 783:in `get'
….0/gems/redis-store-1.1.4/lib/redis/store/interface.rb:   5:in `get'
…/gems/redis-store-1.1.4/lib/redis/store/marshalling.rb:  17:in `get'

现在您知道出了什么问题,以下是所有redis信息:

# redis.conf
daemonize no
databases 128

# config/environments/production.rb
config.action_controller.perform_caching = true
config.cache_store                       = :redis_store, "#{ENV['REDISCLOUD_URL']}/1"
config.action_dispatch.rack_cache        = {
  metastore:   "#{ENV['REDISCLOUD_URL']}/1/metastore",
  entitystore: "#{ENV['REDISCLOUD_URL']}/1/entitystore"
}

# config.ru
require 'rack'
require 'rack/cache'
require 'redis-rack-cache'

use Rack::Cache,
    metastore:   "#{ENV['REDISCLOUD_URL']}/0/metastore",
    entitystore: "#{ENV['REDISCLOUD_URL']}/0/entitystore"

# Gemfile
source 'https://rubygems.org'
ruby '2.1.2'

gem 'rails', '4.1.1'
gem 'mysql2'
gem 'thin'
gem 'redis-store'
gem 'redis-rails'
gem 'redis-rack-cache'
gem 'redis-namespace'

# app/controllers/instructors_controller.rb
class InstructorsController < ApplicationController
  def index
    @instructors = Instructor.all
  end

  def show
    @instructor = Instructor.find(params[:id])
  end
end

# db/schema.rb
# there are no indexes to be found for the instructors.
create_table "instructors", force: true do |t|
  t.string   "first_name"
  t.string   "last_name"
  t.string   "handle"
  t.string   "google_plus"
  t.text     "bio"
  t.string   "avatar"
  t.datetime "created_at"
  t.datetime "updated_at"
end

# Procfile
web: bundle exec thin start -p $PORT
redis: redis-server redis.conf

请记住,为了简洁起见,我一直保持简短,以便您快速浏览。我几乎总是在线,所以如果你需要更多信息,请发表评论。我不介意。

1 个答案:

答案 0 :(得分:4)

如果我没弄错的话,看起来您的生产环境使用的是shared / 1数据库,Redis Cloud不支持这种数据库 - 虽然不是语法错误。相反,您应该只使用/ 0数据库,因为所有Redis Cloud的数据库都是专​​用的 - 有关该主题的更多信息,请参阅http://redislabs.com/blog/benchmark-shared-vs-dedicated-redis-instances