Skip to content

Instantly share code, notes, and snippets.

@lawwantsin
Forked from bradpauly/custom_file_store.rb
Created October 31, 2015 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lawwantsin/de39ff7efe0975c1a2cd to your computer and use it in GitHub Desktop.
Save lawwantsin/de39ff7efe0975c1a2cd to your computer and use it in GitHub Desktop.
simple file store for rails sessions
module ActionDispatch
module Session
class CustomFileStore < ActionDispatch::Session::AbstractStore
def get_session(env, session_id)
session_data = {}
session_id ||= generate_sid
File.open(tmp_file(session_id),'r') do |f|
data = f.read
session_data = ::Marshal.load(data) unless data.empty?
end rescue nil
[session_id, session_data]
end
def set_session(env, session_id, session_data, options)
File.open(tmp_file(session_id), 'w+') do |f|
encoded = ::Marshal.dump(session_data)
f.write(encoded)
end
session_id
end
def destroy_session(env, session_id, options)
File.unlink tmp_file(session_id)
generate_sid
end
def tmp_file(session_id)
File.join(Rails.root, 'tmp', 'sessions', session_id)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment