Class: Rack::Session::RethinkDB

Inherits:
Abstract::ID
  • Object
show all
Defined in:
lib/rack/session/rethinkdb.rb

Constant Summary collapse

DEFAULT_OPTIONS =
Abstract::ID::DEFAULT_OPTIONS.merge \
port: 28_015, table: 'sessions'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RethinkDB.

Parameters:

  • options (Hash<Symbol,Object>) (defaults to: {})

Options Hash (options):

  • :host (String)

    hostname or IP for RethinkDB server (required)

  • :port (Integer)

    port number for the RethinkDB server (default: 28015)

  • :db (String)

    database name (required)

  • :table (String)

    table name to store sessions in (default: ‘sessions’)

See Also:

  • Rack::Session#initialize


25
26
27
28
29
30
31
32
33
# File 'lib/rack/session/rethinkdb.rb', line 25

def initialize(app, options = {})
  super
  @host  = options[:host]
  @port  = @default_options[:port]
  @db    = @default_options[:db]
  @table = @default_options[:table]

  @mutex = Mutex.new
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



13
14
15
# File 'lib/rack/session/rethinkdb.rb', line 13

def db
  @db
end

#hostObject (readonly)

Returns the value of attribute host.



13
14
15
# File 'lib/rack/session/rethinkdb.rb', line 13

def host
  @host
end

#mutexObject (readonly)

Returns the value of attribute mutex.



13
14
15
# File 'lib/rack/session/rethinkdb.rb', line 13

def mutex
  @mutex
end

#poolObject (readonly)

Returns the value of attribute pool.



13
14
15
# File 'lib/rack/session/rethinkdb.rb', line 13

def pool
  @pool
end

#portObject (readonly)

Returns the value of attribute port.



13
14
15
# File 'lib/rack/session/rethinkdb.rb', line 13

def port
  @port
end

#tableObject (readonly)

Returns the value of attribute table.



13
14
15
# File 'lib/rack/session/rethinkdb.rb', line 13

def table
  @table
end

Instance Method Details

#destroy_session(env, session_id, options) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/rack/session/rethinkdb.rb', line 60

def destroy_session(env, session_id, options)
  with_lock(env) do
    # @pool.del(session_id)
    ::RethinkDB::RQL.new.db(db).table(table).get(session_id).delete
      .run(connection)
    generate_sid unless options[:drop]
  end
end

#generate_sidObject



35
36
37
38
39
40
# File 'lib/rack/session/rethinkdb.rb', line 35

def generate_sid
  loop do
    sid = super
    break sid unless _exists?(sid)
  end
end

#get_session(env, sid) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/rack/session/rethinkdb.rb', line 42

def get_session(env, sid)
  with_lock(env, [nil, {}]) do
    unless sid && (session = _get(sid))
      sid, session = generate_sid, {}
      _put(sid, session)
    end

    [sid, session]
  end
end

#set_session(env, session_id, new_session, _options) ⇒ Object



53
54
55
56
57
58
# File 'lib/rack/session/rethinkdb.rb', line 53

def set_session(env, session_id, new_session, _options)
  with_lock(env, false) do
    _put(session_id, new_session)
    session_id
  end
end

#with_lock(env, default = nil) ⇒ Object



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

def with_lock(env, default = nil)
  mutex.lock if env['rack.multithread']
  yield
rescue
  default
ensure
  mutex.unlock if mutex.locked?
end