Sha256: 2c34565f289314266cca9043232b461603bd8be2484fa3f53e23dc241ba50692

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

require 'rack/session/abstract/id'
require 'dalli'

module Rack
  module Session
    class Dalli < Abstract::ID
      attr_reader :pool

      DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge \
        :namespace => 'rack:session',
        :memcache_server => 'localhost:11211'

      def initialize(app, options={})
        super
        mserv = @default_options[:memcache_server]
        mopts = @default_options.reject{|k,v| !DEFAULT_OPTIONS.include? k }
        @pool = options[:cache] || Dalli::Client.new(mserv, mopts)
      end

      def generate_sid
        loop do
          sid = super
          break sid unless @pool.get(sid)
        end
      end

      def get_session(env, sid)
        unless sid and session = @pool.get(sid)
          sid, session = generate_sid, {}
          unless @pool.add(sid, session)
            raise "Session collision on '#{sid.inspect}'"
          end
        end
        [sid, session]
      end

      def set_session(env, session_id, new_session, options)
        expiry = options[:expire_after]
        expiry = expiry.nil? ? 0 : expiry + 1

        @pool.set session_id, new_session, expiry
        session_id
      end

      def destroy_session(env, session_id, options)
        @pool.delete(session_id)
        generate_sid unless options[:drop]
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dalli-1.1.4 lib/rack/session/dalli.rb