We're having an application that uses CookieStore
to store the session's data. Among others we're using this functionality to store an object in the session to increase the performance of our site, e.g.
session[:data] = some_object
Usually it's fine, but sometimes the representation of some_object
is too large to be stored as a cookie. In this case we are completely happy not storing the object in the session - it just increases the site's performance a bit, and only affects a few of our users. However for this to work we would need to decide early whether to store the object or not, otherwise we'll get a ActionDispatch::Cookies::CookieOverflow
on the middleware level - too late to do anything about it.
Is there a way to get the size of the object in an easy way? We're thinking about converting the object to JSON and checking it's string's length but there might be better solutions.
In short we're interested in the implementation of the following stored_as_cookie_size
method:
if stored_as_cookie_size(some_object) < LIMIT
session[:data] = some_object
else
session[:data] = nil
end