Sha256: 7c8d64ba083332fd8706b09b2d00309adb397c5b9566cd3a72462ef7fdc715aa

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

#!/usr/bin/env ruby

require 'nitro/session'
require 'fileutils'
require 'tmpdir'

module Nitro
  
  # A Session manager that persists sessions on disk.
  #
  # TODO: safe locking of files, because Nitro can be multiprocess
  
  class FileSessionStore
    setting :path, :default => "#{Dir.tmpdir}/nitro_session_#{Session.cookie_name}", :doc => 'The directory to store file session'

    def initialize
      @path = FileSessionStore.path
      FileUtils.mkdir_p(@path)
    end

    def []=(k,v)
      fn = File.join(@path, k.to_s)
      encode_file(fn, v)
    end

    def [](k)
      fn = File.join(@path, k.to_s)
      return nil unless File.exists?(fn)
      decode_file(fn)
    end

    def gc!
      now = Time.now
      all.each do |fn|
        expire_time = File.stat(fn).atime + Session.keepalive
        File.delete(fn) if now > expire_time
      end
    end

    def all
      Dir.glob( File.join(@path, '*' ) )
    end

    private

    def decode_file(fn)
      Marshal.load( File.read(fn) )
    end

    def encode_file(fn, value)
      File.open(fn, "w") { |f| f.write(Marshal.dump(value)) }
    end

  end

  Session.store = FileSessionStore.new
end

# * Guillaume Pierronnet <guillaume.pierronnet@gmail.com>

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nitro-0.26.0 lib/nitro/session/file.rb
nitro-0.27.0 lib/nitro/session/file.rb