即使使用memcached服务器,Rails 4中的Cookie溢出也是如此

时间:2013-06-24 20:37:10

标签: ruby-on-rails ruby-on-rails-4

我正在将一个Rails 3应用程序转换为Rails 4,后者使用active_record_store来存储对于常规cookie来说太大的会话信息,Rails 4已经弃用了active_record_store。我的Rails 4应用程序正在引发错误......

ActionDispatch::Cookies::CookieOverflow (ActionDispatch::Cookies::CookieOverflow):

导致我在Rails 3中切换到active_record_store。如果我的会话信息超出允许限制,我可以在Rails 4中做什么?

更新。我在本地计算机上启动了memcache服务器,并在开发文件

中进行了配置
config.cache_store = :mem_cache_store, 'localhost', '127.0.0.1:11211',
{:namespace => 'myapp123'}

在控制台中,当我执行Rails.cache时,会显示此信息(后跟大量数据)

=>真

>> Rails.cache
=> #<ActiveSupport::Cache::MemCacheStore:0x007f80ebe10500 @options={}, @data=#<Dalli::Client:0x007f80ebe0fd08 @servers=["localhost:11211"]

但是,当我运行应用程序时,我遇到了同样的错误

ActionDispatch::Cookies::CookieOverflow (ActionDispatch::Cookies::CookieOverflow):
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/cookies.rb:455:in `[]='
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/session/cookie_store.rb:109:in `set_cookie'
  rack (1.5.2) lib/rack/session/abstract/id.rb:350:in `commit_session'
  rack (1.5.2) lib/rack/session/abstract/id.rb:226:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/cookies.rb:486:in `call'
  activerecord (4.0.0.rc2) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.0.0.rc2) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
  activerecord (4.0.0.rc2) lib/active_record/migration.rb:369:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.0.0.rc2) lib/active_support/callbacks.rb:373:in `_run__1963944942928963409__call__callbacks'
  activesupport (4.0.0.rc2) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/reloader.rb:64:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.0.rc2) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.0.rc2) lib/rails/rack/logger.rb:21:in `block in call'
  activesupport (4.0.0.rc2) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.0.rc2) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.0.rc2) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.0.rc2) lib/rails/rack/logger.rb:21:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.0.rc2) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/static.rb:64:in `call'
  railties (4.0.0.rc2) lib/rails/engine.rb:511:in `call'
  railties (4.0.0.rc2) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /Users/mm/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /Users/mm/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /Users/mm/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

2 个答案:

答案 0 :(得分:1)

如果您使用的是Cookie存储,则只能在会话中存储4kb。你有两个选择。

  1. 查看减少会话中存储的信息。如果要在会话中存储模型对象,请将其更改为仅存储id。

  2. 使用mem_cache_store

    config.action_controller.session_store =:mem_cache_store

  3. 您还需要设置mem缓存服务器并按如下所示进行配置:

    config.cache_store = :mem_cache_store, 'localhost', '127.0.0.1:11211',
    {:namespace => 'myapp123'}
    

答案 1 :(得分:1)

你能告诉我config / initializers / session_store.rb的内容好像你还在那里使用cookie存储。

定义缓存存储时,仍然需要更改session_store

config.session_store = :mem_cache_store