Skip to content

Instantly share code, notes, and snippets.

@bradpauly
Created August 12, 2012 22:37
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bradpauly/3335006 to your computer and use it in GitHub Desktop.
Save bradpauly/3335006 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
@bradpauly
Copy link
Author

Wrote a simple file store while I was researching rails sessions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment