Class: DataMapper::Session::Abstract::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_datamapper/session/abstract/store.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options, id_generator) ⇒ Store

Returns a new instance of Store.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rack_datamapper/session/abstract/store.rb', line 8

def initialize(app, options, id_generator)
  @mutex = Mutex.new
  if cache = options.delete(:cache)
    @@cache = if RUBY_PLATFORM =~ /java/
                begin
                  # to avoid memory leaks use a hashmap which clears
                  # itself on severe memory shortage
                  require 'softhashmap'
                  m = Java.SoftHashMap.new
                  def m.delete(key)
                    remove(key)
                  end
                  m
                rescue
                  # fallback to non java Hash
                  {}
                end
              else
                {}
              end
    @@semaphore = Mutex.new
  else
    @@cache = nil unless cache.nil? && self.class.class_variable_defined?(:@@cache)
  end
  @@session_class = options.delete(:session_class) || Session unless (self.class.class_variable_defined?(:@@session_class) and @@session_class)
  @id_generator = id_generator
end

Instance Method Details

#get_session(env, sid) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rack_datamapper/session/abstract/store.rb', line 36

def get_session(env, sid)
  @mutex.lock if env['rack.multithread']
  if sid
    session = 
      if @@cache
        @@cache[sid] || @@cache[sid] = @@session_class.get(sid)
      else
        @@session_class.get(sid)
      end
  end

  unless sid and session
    env['rack.errors'].puts("Session '#{sid.inspect}' not found, initializing...") if $VERBOSE and not sid.nil?
    sid = @id_generator.call
    session = @@session_class.create(:session_id => sid)
    @@cache[sid] = session if @@cache
  end
  #session.instance_variable_set('@old', {}.merge(session))

  return [sid, session.data]
ensure
  @mutex.unlock if env['rack.multithread']
end

#set_session(env, sid, session_data, options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rack_datamapper/session/abstract/store.rb', line 60

def set_session(env, sid, session_data, options)
  @mutex.lock if env['rack.multithread']
  session = 
    if @@cache
      @@cache[sid] || @@cache[sid] = @@session_class.get(sid)
    else
      @@session_class.get(sid)
    end 
  return false if session.nil?
  if options[:renew] or options[:drop]
    @@cache.delete(sid) if @@cache
    session.destroy
    return false if options[:drop]
    sid = @id_generator.call
    session = @@session_class.create(:session_id => sid)
    @@cache[sid] = session if @@cache
  end
  #        old_session = new_session.instance_variable_get('@old') || {}
  #        session = merge_sessions session_id, old_session, new_session, session
  session.data = session_data
  if session_data.empty?
    @@cache.delete(sid) if @@cache
    session.destroy
    false
  elsif session.save
    session.session_id
  else
    false
  end
ensure
  @mutex.unlock if env['rack.multithread']
end