在 Rails 5 中使用不同的 redis 存储进行会话和缓存 [英] Using different redis stores for session and caching in Rails 5

查看:65
本文介绍了在 Rails 5 中使用不同的 redis 存储进行会话和缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Ruby 和 Rails 的新手.我正在使用 Ruby on Rails 5.2 构建一个 Web 应用程序.我已将其配置为使用 redis-cache-store 来管理视图缓存:

I am a newbie to Ruby and Rails. I am building a web application using Ruby on Rails 5.2. I have configured it to use the redis-cache-store to manage the view caching:

config.cache_store = :redis_cache_store, { driver: :hiredis, namespace: "my-app", compress: true, url: ENV["REDIS_URL"] }

并且我已将会话存储配置为:

And I have configured my session storage as:

Rails.application.config.session_store :cache_store, {
  key: "sid",
  expire_after: 30.minutes
}

所以在这里我使用 :cache_store 作为我的会话存储.据我了解,这意味着视图缓存和会话数据的条目存储在同一个 Redis 数据库中.

So here I am using the :cache_store as my session store. As far as I understand, this means the entries for the view cache and the session data are stored in the same Redis database.

据我所知,如果使用 Rails 5.2,则不再需要 redis-rails gem,因为有对 redis 的内置支持 - https://github.com/redis-store/redis-rails#a-quick-note-about-rails-52.因此,我没有使用过那个 gem.

From what I understand, the redis-rails gem is no longer required if using Rails 5.2 as there is a built-in support for redis - https://github.com/redis-store/redis-rails#a-quick-note-about-rails-52. Hence, I have not used that gem.

有没有办法对会话使用不同的 redis 存储,对视图缓存使用不同的存储?

Is there a way to use a different redis store for sessions and different one for view caching?

或者我是在尝试做一些在 Rails-land 中不寻常的事情吗?

Or am I trying to do something which is unusual in Rails-land?

推荐答案

要单独配置会话存储以使用 Redis,您需要使用提供 Redis 会话存储的 gem:redis-store(注意 redis-activesupport 已被弃用,但 redis-actionpack 不是),或 redis-session-store.

To separately configure your session store to use Redis, you'll need to use a gem that provides a Redis session store: either redis-store (note redis-activesupport is deprecated, but redis-actionpack is not), or redis-session-store.

您不能在使用 :cache_store 会话存储时单独配置会话存储,因为它在实际缓存中存储会话...它不仅仅是缓存"-like"存储,因此它不能覆盖/表现与缓存本身不同.

You can't configure the session storage separately while using the :cache_store session store, because that stores sessions in the actual cache... it's not just "cache-like" storage, so it can't override / behave differently from the cache itself.

将会话内容存储在 cookie 以外的地方是有些不寻常的,现在会话 cookie 已加密且防篡改,但这种情况并不少见.如果你要去,Redis 是一个不错的选择.

Storing session contents somewhere other than a cookie is somewhat unusual, now that session cookies are encrypted as well as tamper-proof, but it's not rare. If you're going to, Redis is a good choice.

这篇关于在 Rails 5 中使用不同的 redis 存储进行会话和缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆