Class: Rack::Session::Couchbase
- Inherits:
-
Abstract::ID
- Object
- Abstract::ID
- Rack::Session::Couchbase
- Defined in:
- lib/rack/session/couchbase.rb
Overview
This is Couchbase-powered session store for rack applications
To use it just load it as usual middleware in your ‘config.ru` file
require 'rack/session/couchbase'
use Rack::Session::Couchbase
You can also pass additional options:
require 'rack/session/couchbase'
use Rack::Session::Couchbase, :expire_after => 5.minutes,
:couchbase => {:bucket => "sessions", :default_format => :document}
By default sessions will be serialized using Marshal class. But you can store them as JSON (+:default_format => :document+), to allow analyse them using Map/Reduce. In this case you should care about serialization of all custom objects like ActionDispatch::Flash::FlashHash
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_OPTIONS =
Abstract::ID::DEFAULT_OPTIONS.merge( :couchbase => {:quiet => true, :default_format => :marshal, :key_prefix => 'rack:session:'})
Instance Attribute Summary collapse
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
Instance Method Summary collapse
- #destroy_session(env, session_id, options) ⇒ Object
- #generate_sid ⇒ Object
- #get_session(env, sid) ⇒ Object
-
#initialize(app, options = {}) ⇒ Couchbase
constructor
A new instance of Couchbase.
- #set_session(env, session_id, new_session, options) ⇒ Object
- #with_lock(env, default = nil) ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Couchbase
Returns a new instance of Couchbase.
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rack/session/couchbase.rb', line 51 def initialize(app, = {}) # Support old :expires option [:expire_after] ||= [:expires] super @default_options[:couchbase][:default_ttl] ||= [:expire_after] @default_options[:couchbase][:key_prefix] ||= [:namespace] @namespace = @default_options[:couchbase][:key_prefix] @mutex = Mutex.new @pool = ::Couchbase.connect(@default_options[:couchbase]) end |
Instance Attribute Details
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
45 46 47 |
# File 'lib/rack/session/couchbase.rb', line 45 def mutex @mutex end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
45 46 47 |
# File 'lib/rack/session/couchbase.rb', line 45 def pool @pool end |
Instance Method Details
#destroy_session(env, session_id, options) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/rack/session/couchbase.rb', line 87 def destroy_session(env, session_id, ) with_lock(env) do @pool.delete(session_id) generate_sid unless [:drop] end end |
#generate_sid ⇒ Object
63 64 65 66 67 68 |
# File 'lib/rack/session/couchbase.rb', line 63 def generate_sid while true sid = super break sid unless @pool.get(sid) end end |
#get_session(env, sid) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/rack/session/couchbase.rb', line 70 def get_session(env, sid) with_lock(env, [nil, {}]) do unless sid and session = @pool.get(sid) sid, session = generate_sid, {} @pool.set(sid, session) end [sid, session] end end |
#set_session(env, session_id, new_session, options) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/rack/session/couchbase.rb', line 80 def set_session(env, session_id, new_session, ) with_lock(env, false) do @pool.set(session_id, new_session, ) session_id end end |
#with_lock(env, default = nil) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rack/session/couchbase.rb', line 94 def with_lock(env, default = nil) @mutex.lock if env['rack.multithread'] yield rescue ::Couchbase::Error::Connect, ::Couchbase::Error::Timeout if $VERBOSE warn "#{self} is unable to find Couchbase server." warn $!.inspect end default ensure @mutex.unlock if @mutex.locked? end |