如何检查rails中的会话大小? [英] How to check size of session in rails?

查看:28
本文介绍了如何检查rails中的会话大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望 session.sizesession.lengthsession.count 能够工作,但它们都返回未定义的方法".为什么,session 不是一个哈希值吗?

I would expect session.size, session.length or session.count to work, but they all return "undefined method". Why, isn't session a hash?

那是好奇心.实际问题:有没有办法以 KB 为单位检查会话的大小?目前我的会话存储在 cookie 中,它有 4 KB 的限制,当超过时,它只会停止将新数据放入那里.很高兴知道何时发生,重置会话或删除旧数据.

That was curiosity. The actual question: is there a way to check the size of session in KB? Currently my sessions are stored in cookies and it has a 4 KB limit, and when it exceeds, it simply stops putting new data in there. It would be nice to know when in happens, to reset the session or remove old data.

推荐答案

以下是 rails 获取会话大小的方式:

Following is how rails get the session size:

首先获取session数据,见(gems/rack/lib/rack/session/abstract/id.rb:352 commit_session)

Firstly, get the session data, see (gems/rack/lib/rack/session/abstract/id.rb:352 commit_session)

data = session.to_hash.delete_if { |k,v| v.nil? }

然后它会加密数据,见(gems/actionpack/lib/action_dispatch/middleware/cookies.rb:640):

Then it will encrypt the data, see (gems/actionpack/lib/action_dispatch/middleware/cookies.rb:640):

data = @encryptor.encrypt_and_sign(serialize(name, data))

最后,获取字节:

data.bytesize

您可以直接获取会话data,但很难手动对其进行加密.我一般会在gem代码处加一个日志来检查数据:

You can directly get the session data but it's hard to encrypt it manually. I usually add a log at the gem code to check the data:

options[:value] = @encryptor.encrypt_and_sign(serialize(name, options[:value]))

puts options[:value].bytesize

raise CookieOverflow if options[:value].bytesize > MAX_COOKIE_SIZE

这篇关于如何检查rails中的会话大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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