Skip to content

Instantly share code, notes, and snippets.

@larzconwell
Created March 8, 2012 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larzconwell/2002893 to your computer and use it in GitHub Desktop.
Save larzconwell/2002893 to your computer and use it in GitHub Desktop.
Rails sessions with Redis, the output.
### Gemfile
gem "redis"
gem "redis-rails", "~> 3.2.1.rc"
### app/controllers/sessions_controller.rb
def create
user = User.where(email: params[:email]).first
# 'authenticate' comes from the 'has_secure_password' method in the User model
if user && user.authenticate(params[:password])
session[:user_id] = user.id # Set a session from the id
Time.zone = user.time_zone # Set the users time zone on sign in
redirect_to user, notice: "You have been signed in as #{user}."
else
flash.now[:error] = "There was an error, please check your email/password and try again."
render 'new'
end
end
### config/environments.rb
Hangout::Application.config.session_store :redis_store, servers: "redis://localhost:6379", expire_in: 7200
### Redis server logs
[16304] 08 Mar 14:44:50 * The server is now ready to accept connections on port 6379
[16304] 08 Mar 14:44:50 - 0 clients connected (0 slaves), 717496 bytes in use
# Redis-cli connected
[16304] 08 Mar 14:44:53 - Accepted 127.0.0.1:40680
[16304] 08 Mar 14:44:55 - 1 clients connected (0 slaves), 726024 bytes in use
[16304] 08 Mar 14:45:00 - 1 clients connected (0 slaves), 726088 bytes in use
[16304] 08 Mar 14:45:05 - 1 clients connected (0 slaves), 726088 bytes in use
[16304] 08 Mar 14:45:10 - 1 clients connected (0 slaves), 726088 bytes in use
# Rails server connected
[16304] 08 Mar 14:45:13 - Accepted 127.0.0.1:40687
[16304] 08 Mar 14:45:15 - DB 0: 1 keys (1 volatile) in 4 slots HT.
[16304] 08 Mar 14:45:15 - 2 clients connected (0 slaves), 735224 bytes in use
[16304] 08 Mar 14:45:20 - DB 0: 1 keys (1 volatile) in 4 slots HT.
[16304] 08 Mar 14:45:20 - 2 clients connected (0 slaves), 735224 bytes in use
[16304] 08 Mar 14:45:25 - DB 0: 1 keys (1 volatile) in 4 slots HT.
[16304] 08 Mar 14:45:25 - 2 clients connected (0 slaves), 735224 bytes in use
[16304] 08 Mar 14:45:30 - DB 0: 1 keys (1 volatile) in 4 slots HT.
[16304] 08 Mar 14:45:30 - 2 clients connected (0 slaves), 735288 bytes in use
[16304] 08 Mar 14:45:35 - DB 0: 1 keys (1 volatile) in 4 slots HT.
[16304] 08 Mar 14:45:35 - 2 clients connected (0 slaves), 735288 bytes in use
[16304] 08 Mar 14:45:41 - DB 0: 1 keys (1 volatile) in 4 slots HT.
### Redis-cli
# Before Rails connects
redis 127.0.0.1:6379> keys *
(empty list or set)
# After Rails connects
redis 127.0.0.1:6379> keys *
1) "5e5733bf51ea86eb32db3e96b4e65b9b"
# After Rails connects and before user session is created
redis 127.0.0.1:6379> get 5e5733bf51ea86eb32db3e96b4e65b9b
"\x04\b{\x06I\"\x10_csrf_token\x06:\x06EFI\"1/76X9vHqMYUQ5siYXVktw9c/jZ1Hy3CKlJPQ36C/W4E=\x06;\x00F"
# After user session is created from Rails
redis 127.0.0.1:6379> get 5e5733bf51ea86eb32db3e96b4e65b9b
"\x04\b{\bI\"\x10_csrf_token\x06:\x06EFI\"1/76X9vHqMYUQ5siYXVktw9c/jZ1Hy3CKlJPQ36C/W4E=\x06;\x00FI\"\x0cuser_id\x06;\x00Fi\x06I\"\nflash\x06;\x00Fo:%ActionDispatch::Flash::FlashHash\t:\n@usedo:\bSet\x06:\n@hash{\x06:\x0bnoticeT:\x0c@closedF:\r@flashes{\x06;\nI\"0You have been signed in as Admin_test User.\x06;\x00F:\t@now0"
# After user session is destroyed from Rails
redis 127.0.0.1:6379> get 5e5733bf51ea86eb32db3e96b4e65b9b
"\x04\b{\aI\"\x10_csrf_token\x06:\x06EFI\"1/76X9vHqMYUQ5siYXVktw9c/jZ1Hy3CKlJPQ36C/W4E=\x06;\x00FI\"\nflash\x06;\x00Fo:%ActionDispatch::Flash::FlashHash\t:\n@usedo:\bSet\x06:\n@hash{\x06:\x0bnoticeT:\x0c@closedF:\r@flashes{\x06;\nI\")You have been signed out, thank you.\x06;\x00F:\t@now0"
redis 127.0.0.1:6379>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment