Class: Rack::Session::Dalli

Inherits:
Abstract::PersistedSecure
  • Object
show all
Defined in:
lib/rack/session/dalli.rb

Overview

Rack::Session::Dalli provides memcached based session management.

Defined Under Namespace

Classes: MissingSessionError

Constant Summary collapse

RACK_SESSION_PERSISTED =
'rack.session.persisted'
DEFAULT_DALLI_OPTIONS =

Don’t freeze this until we fix the specs/implementation rubocop:disable Style/MutableConstant

{
  namespace: 'rack:session'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Dalli

Brings in a new Rack::Session::Dalli middleware with the given :memcache_server. The server is either a hostname, or a host-with-port string in the form of “host_name:port”, or an array of such strings. For example:

use Rack::Session::Dalli,
  :memcache_server => "mc.example.com:1234"

If no :memcache_server option is specified, Rack::Session::Dalli will connect to localhost, port 11211 (the default memcached port). If :memcache_server is set to nil, Dalli::Client will look for ENV and use that value if it is available, or fall back to the same default behavior described above.

Rack::Session::Dalli accepts the same options as Dalli::Client, so it’s worth reviewing its documentation. Perhaps most importantly, if you don’t specify a :namespace option, Rack::Session::Dalli will default to using ‘rack:session’.

It is not recommended to set :expires_in. Instead, use :expire_after, which will control both the expiration of the client cookie as well as the expiration of the corresponding entry in memcached.

Rack::Session::Dalli also accepts a host of options that control how the sessions and session cookies are managed, including the aforementioned :expire_after option. Please see the documentation for Rack::Session::Abstract::Persisted for a detailed explanation of these options and their default values.

Finally, if your web application is multithreaded, the Rack::Session::Dalli middleware can become a source of contention. You can use a connection pool of Dalli clients by passing in the :pool_size and/or :pool_timeout options. For example:

use Rack::Session::Dalli,
  :memcache_server => "mc.example.com:1234",
  :pool_size => 10

You must include the connection_pool gem in your project if you wish to use pool support. Please see the documentation for ConnectionPool for more information about it and its default options (which would only be applicable if you supplied one of the two options, but not both).



68
69
70
71
72
73
74
75
# File 'lib/rack/session/dalli.rb', line 68

def initialize(app, options = {})
  # Parent uses DEFAULT_OPTIONS to build @default_options for Rack::Session
  super

  # Determine the default TTL for newly-created sessions
  @default_ttl = ttl(@default_options[:expire_after])
  @data = build_data_source(options)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



16
17
18
# File 'lib/rack/session/dalli.rb', line 16

def data
  @data
end

Instance Method Details

#call(*_args) ⇒ Object



77
78
79
80
81
# File 'lib/rack/session/dalli.rb', line 77

def call(*_args)
  super
rescue MissingSessionError
  [401, {}, ['Wrong session ID']]
end

#delete_session(_req, sid, options) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/rack/session/dalli.rb', line 112

def delete_session(_req, sid, options)
  with_dalli_client do |dc|
    key = memcached_key_from_sid(sid)
    dc.delete(key) if key
    generate_sid_with(dc) unless options[:drop]
  end
end

#find_session(req, sid) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rack/session/dalli.rb', line 83

def find_session(req, sid)
  with_dalli_client([nil, {}]) do |dc|
    existing_session = existing_session_for_sid(dc, sid)
    if existing_session.nil?
      sid = create_sid_with_empty_session(dc)
      existing_session = {}
    end

    update_session_persisted_data(req, { id: sid })
    return [sid, existing_session]
  end
end

#write_session(req, sid, session, options) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rack/session/dalli.rb', line 96

def write_session(req, sid, session, options)
  return false unless sid

  key = memcached_key_from_sid(sid)
  return false unless key

  with_dalli_client(false) do |dc|
    write_session_safely!(
      dc, sid, session_persisted_data(req),
      write_args: [memcached_key_from_sid(sid), session, ttl(options[:expire_after])]
    )

    sid
  end
end