1--- 2stage: none 3group: unassigned 4info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments 5--- 6 7# Accessing session data 8 9Session data in GitLab is stored in Redis and can be accessed in a variety of ways. 10 11During a web request, for example: 12 13- Rails provides access to the session from within controllers through [`ActionDispatch::Session`](https://guides.rubyonrails.org/action_controller_overview.html#session). 14- Outside of controllers, it is possible to access the session through `Gitlab::Session`. 15 16Outside of a web request it is still possible to access sessions stored in Redis. For example: 17 18- Session IDs and contents can be [looked up directly in Redis](#redis). 19- Data about the UserAgent associated with the session can be accessed through `ActiveSession`. 20 21When storing values in a session it is best to: 22 23- Use simple primitives and avoid storing objects to avoid marshaling complications. 24- Clean up after unneeded variables to keep memory usage in Redis down. 25 26## GitLab::Session 27 28Sometimes you might want to persist data in the session instead of another store like the database. `Gitlab::Session` lets you access this without passing the session around extensively. For example, you could access it from within a policy without having to pass the session through to each place permissions are checked from. 29 30The session has a hash-like interface, just like when using it from a controller. There is also `NamespacedSessionStore` for storing key-value data in a hash. 31 32```ruby 33# Lookup a value stored in the current session 34Gitlab::Session.current[:my_feature] 35 36# Modify the current session stored in redis 37Gitlab::Session.current[:my_feature] = value 38 39# Store key-value data namespaced under a key 40Gitlab::NamespacedSessionStore.new(:my_feature)[some_key] = value 41 42# Set the session for a block of code, such as for tests 43Gitlab::Session.with_session(my_feature: value) do 44 # Code that uses Session.current[:my_feature] 45end 46``` 47 48## Redis 49 50Session data can be accessed directly through Redis. This can let you check up on a browser session when debugging. 51 52```ruby 53# Get a list of sessions 54session_ids = Gitlab::Redis::Sessions.with do |redis| 55 redis.smembers("#{Gitlab::Redis::Sessions::USER_SESSIONS_LOOKUP_NAMESPACE}:#{user.id}") 56end 57 58# Retrieve a specific session 59session_data = Gitlab::Redis::Sessions.with { |redis| redis.get("#{Gitlab::Redis::Sessions::SESSION_NAMESPACE}:#{session_id}") } 60Marshal.load(session_data) 61``` 62 63## Getting device information with ActiveSession 64 65The [**Active Sessions** page on a user's profile](../user/profile/active_sessions.md) displays information about the device used to access each session. The methods used there to list sessions can also be useful for development. 66 67```ruby 68# Get list of sessions for a given user 69# Includes session_id and data from the UserAgent 70ActiveSession.list(user) 71``` 72