用到的gem有:
gem 'activerecord-session_store', '~> 0.1.0'
gem 'redis', '~> 3.3.3'
gem 'redis-namespace', '~> 1.6.0'
gem 'redis-rack-cache'
gem 'redis-rails', '~> 4.0.0'
config/initializers/session_store.rb
# frozen_string_literal: true xxxxx::Application.config.session_store :redis_store, servers: [ { url: ENV['REDISTOGO_URL'], namespace: 'session' } ], key: '_xxxxx_session_id', expire_after: 60.minutes, domain: ENV['COOKIES_BASE_DOMAIN'], path: '/'
config/application.rb:
config.middleware.use WickedPdf::Middleware config.cache_store = :redis_store, "#{ENV["REDISTOGO_URL"]}/0/cache", { expires_in: 90.minutes }
打开terminal查看一下redis存的数据:
$ rails c
$ redis = Redis::Namespace.new("session", :redis => Redis.new) //备注: 这里的字符串session就是上面定义的namespace
打开浏览器找到cookies里存储的key对应的一长串的值,根据这个值在redis里获取数据
$ redis.get('xxx6eb71c172dxxxx6ff17da67c08xxx')
$ Marshal.load(redis.get("xxx6eb71c172dxxxx6ff17da67c08xxx")) 解决显示结果的编码问题