Rails4でsessionストアにredis-storeを使用する

Pocket

以下の2ファイルをいじればOK・・・のはずだった。

# Gemfile
gem 'redis-store'
gem 'redis-rails'
# config/initializers/session_store.rb
#RedisTest::Application.config.session_store :cookie_store, key: '_redis-test_session'
# ↓ 以下のように変更する
# 60秒有効
RedisTest::Application.config.session_store :redis_store, :servers => "redis://localhost:6379/1", :expire_in => 60

しかしbundle exec rails serverしようとすると以下の様なエラーが出ます。

/home/unicast/redis/redis-test/vendor/bundle/ruby/1.9.1/gems/redis-rails-0.0.0/lib/redis-rails/version.rb:1:in `<top (required)>': Redis is not a module (TypeError)
    from /home/unicast/redis/redis-test/vendor/bundle/ruby/1.9.1/gems/redis-rails-0.0.0/lib/redis-rails.rb:1:in `require'
    from /home/unicast/redis/redis-test/vendor/bundle/ruby/1.9.1/gems/redis-rails-0.0.0/lib/redis-rails.rb:1:in `<top (required)>'
    from /home/unicast/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `require'
    from /home/unicast/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `block (2 levels) in require'
    from /home/unicast/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/bundler-1.3.5/lib/bundler/runtime.rb:70:in `each'
    from /home/unicast/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/bundler-1.3.5/lib/bundler/runtime.rb:70:in `block in require'
    from /home/unicast/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/bundler-1.3.5/lib/bundler/runtime.rb:59:in `each'
    from /home/unicast/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/bundler-1.3.5/lib/bundler/runtime.rb:59:in `require'
    from /home/unicast/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/bundler-1.3.5/lib/bundler.rb:132:in `require'
    from /home/unicast/redis/redis-test/config/application.rb:7:in `<top (required)>'
    from /home/unicast/redis/redis-test/vendor/bundle/ruby/1.9.1/gems/railties-4.0.0/lib/rails/commands.rb:76:in `require'
    from /home/unicast/redis/redis-test/vendor/bundle/ruby/1.9.1/gems/railties-4.0.0/lib/rails/commands.rb:76:in `block in <top (required)>'
    from /home/unicast/redis/redis-test/vendor/bundle/ruby/1.9.1/gems/railties-4.0.0/lib/rails/commands.rb:73:in `tap'
    from /home/unicast/redis/redis-test/vendor/bundle/ruby/1.9.1/gems/railties-4.0.0/lib/rails/commands.rb:73:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

バージョンを明示的に指定しないと0.0.0になっちゃうよとのことです。バグじゃないのか・・・。
conflict with resque? Redis is not a module · Issue #118 · jodosha/redis-store

# Gemfile
gem 'redis-store'
gem 'redis-rails', '3.2.1'

こんどこそ。

$ bundle install
...
Bundler could not find compatible versions for gem "actionpack":
  In Gemfile:
    redis-rails (= 3.2.1) ruby depends on
      actionpack (= 3.2.1) ruby

    rails (= 4.0.0) ruby depends on
      actionpack (4.0.0)

バージョン関係のしがらみが多いなあ。

Is there any way to set up Redis session store with Rails 4? · Issue #160 · jodosha/redis-store を見ると結果的に

# Gemfile
gem 'redis-store', github: "bricker/redis-store"
gem 'redis-actionpack', github: "bricker/redis-store"
gem 'redis-activesupport', github: "bricker/redis-store"
gem 'redis-rack', github: "bricker/redis-store"

として bundle install して解決。

参考サイト

投稿者紹介

株式会社ユニキャスト
私たちは、テクノロジに魅せられた個性あふれるメンバーによって構成された茨城県日立市に本社を構えるベンチャー企業です。
”テクノロジを通して「驚き」と「感動」を創造し、人々の「夢」と「笑顔」を支えます。” の経営理念をモットーに明るい未来を描き、ワクワクする企画提案を続けて参ります。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください