class Rack::Session::Redic

Rack::Session::Redic provides simple cookie based session management. Session data is stored in Redis via the Redic gem. The corresponding session key is maintained in the cookie.

Options include:

Any other options will get passed to Rack::Session::Abstract::Persisted.

Constants

DELETE

Redis commands.

EX
EXISTS
GET
HASH
REDIS_URL

Assorted.

SET
ZERO

Attributes

storage[R]

Access the storage interface directly. Needed for testing.

@return [Redic]

Public Class Methods

new(app, options = HASH) click to toggle source
Calls superclass method
# File lib/rack/session/redic.rb, line 46
def initialize(app, options = HASH)
  super(app, options)

  @expires = options[:expire_after]
  @marshaller = options.fetch(:marshaller) { Marshal }
  @storage = ::Redic.new(options.fetch(:url) { ENV.fetch(REDIS_URL) })
end

Public Instance Methods

delete_session(_req, session_id, options) click to toggle source

Kill the session.

# File lib/rack/session/redic.rb, line 88
def delete_session(_req, session_id, options)
  @storage.call(DELETE, session_id)

  generate_sid unless options[:drop]
end
find_session(_req, session_id) click to toggle source

Find the session (or generate a blank one).

# File lib/rack/session/redic.rb, line 69
def find_session(_req, session_id)
  unless session_id && (session = deserialize(@storage.call(GET, session_id)))
    session_id, session = generate_sid, {} # rubocop:disable Style/ParallelAssignment
  end

  [session_id, session]
end
generate_sid() click to toggle source

Generate a session ID that doesn't already exist.

Based on Rack::Session::Abstract::Persisted#generate_sid and Rack::Session::Memcache#generate_sid but without the conditional check. We always generate the session ID from SecureRandom#hex.

@return [String]

# File lib/rack/session/redic.rb, line 61
def generate_sid
  loop do
    session_id = SecureRandom.hex(@sid_length)
    break session_id unless @storage.call(EXISTS, session_id) != ZERO
  end
end
write_session(_req, session_id, session_data, _options) click to toggle source

Write the session.

# File lib/rack/session/redic.rb, line 78
def write_session(_req, session_id, session_data, _options)
  arguments = [SET, session_id, serialize(session_data)]
  arguments.push(EX, @expires) if @expires

  @storage.call(*arguments)

  session_id
end

Private Instance Methods

deserialize(string) click to toggle source

Deserialize a string back into an object.

@param string [String] @return [Object, nil]

Returns the object as loaded by the marshaller, or nil.
# File lib/rack/session/redic.rb, line 110
def deserialize(string)
  @marshaller.load(string) if string

# In the case that loading fails, return a nil.
rescue # rubocop:disable Style/RescueStandardError
  nil
end
serialize(object) click to toggle source

Serialize an object using our marshaller.

@param object [Object] @return [String]

The object as serialized by the marshaller.
# File lib/rack/session/redic.rb, line 101
def serialize(object)
  @marshaller.dump(object)
end