forked from fcheung/smart_session_store
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rails 2.3 compat (retains backwards compatibility with 2.2 and prior)
- Loading branch information
Showing
25 changed files
with
400 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
require 'smart_session_store' | ||
version ||= Rails.version.split('.') | ||
|
||
if version[0].to_i == 2 && version[1].to_i < 3 #version prior to 2.3 use the legacy store | ||
require 'legacy_smart_session_sore' | ||
SmartSessionStore = LegacySmartSessionStore | ||
else | ||
require 'smart_session_store' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
require 'active_record' | ||
require 'cgi' | ||
require 'cgi/session' | ||
require 'base64' | ||
require 'pp' | ||
# +SmartSessionStore+ is a session store that strives to correctly handle session storage in the face of multiple | ||
# concurrent actions accessing the session. It is derived from Stephen Kaes' +SqlSessionStore+, a stripped down, | ||
# optimized for speed version of class +ActiveRecordStore+. | ||
# | ||
# LegacySmartSessionStore implements the session store interface used by versions of rails prior to 2.3 | ||
class LegacySmartSessionStore | ||
include SessionSmarts | ||
# The class to be used for creating, retrieving and updating sessions. | ||
# Defaults to SmartSessionStore::Session, which is derived from +ActiveRecord::Base+. | ||
# | ||
# In order to achieve acceptable performance you should implement | ||
# your own session class, similar to the one provided for Myqsl. | ||
# | ||
# Only functions +find_session+, +create_session+, | ||
# +update_session+ and +destroy+ are required. See file +mysql_session.rb+. | ||
|
||
cattr_accessor :session_class | ||
@@session_class = SqlSession | ||
|
||
# Create a new SmartSessionStore instance. | ||
# | ||
# +session+ is the session for which this instance is being created. | ||
# | ||
# +option+ is currently ignored as no options are recognized. | ||
|
||
def initialize(session, option=nil) | ||
if @session = @@session_class.find_session(session.session_id) | ||
self.data = unmarshalize(@session.data) | ||
else | ||
@session = @@session_class.create_session(session.session_id, marshalize({})) | ||
self.data = {} | ||
end | ||
end | ||
|
||
# Update the database and disassociate the session object | ||
def close | ||
if @session | ||
finalize_session | ||
@session = nil | ||
end | ||
end | ||
|
||
# Delete the current session, disassociate and destroy session object | ||
def delete | ||
if @session | ||
@session.destroy | ||
@session = nil | ||
end | ||
end | ||
|
||
# Restore session data from the session object | ||
def restore | ||
if @session | ||
self.data = unmarshalize(@session.data) | ||
end | ||
end | ||
|
||
# Save session data in the session object | ||
def update | ||
if @session | ||
finalize_session | ||
end | ||
end | ||
|
||
private | ||
|
||
def data= data | ||
@data = data | ||
end | ||
|
||
def finalize_session | ||
@data, @session = save_session(@session, @data || {}) | ||
end | ||
end | ||
|
||
__END__ | ||
|
||
# This software is released under the MIT license | ||
# Copyright (c) 2007 Frederick Cheung | ||
# Copyright (c) 2005,2006 Stefan Kaes | ||
|
||
# Permission is hereby granted, free of charge, to any person obtaining | ||
# a copy of this software and associated documentation files (the | ||
# "Software"), to deal in the Software without restriction, including | ||
# without limitation the rights to use, copy, modify, merge, publish, | ||
# distribute, sublicense, and/or sell copies of the Software, and to | ||
# permit persons to whom the Software is furnished to do so, subject to | ||
# the following conditions: | ||
|
||
# The above copyright notice and this permission notice shall be | ||
# included in all copies or substantial portions of the Software. | ||
|
||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module SessionSmarts | ||
def marshalize(data) | ||
Base64.encode64(Marshal.dump(data)) | ||
end | ||
|
||
def unmarshalize(data) | ||
Marshal.load(Base64.decode64(data)) | ||
end | ||
|
||
def save_session(session, data) | ||
original_data = unmarshalize(session.data) | ||
original_marshalled_data = session.data | ||
|
||
deleted_keys = original_data.keys - data.keys | ||
changed_keys = [] | ||
data.each {|k,v| changed_keys << k if Marshal.dump( original_data[k]) != Marshal.dump( v)} | ||
|
||
return nil if changed_keys.empty? && deleted_keys.empty? | ||
|
||
SqlSession.transaction do | ||
fresh_session = session_class.find_session(session.session_id, true) | ||
if fresh_session && fresh_session.data != original_marshalled_data && fresh_data = unmarshalize(fresh_session.data) | ||
deleted_keys.each {|k| fresh_data.delete k} | ||
changed_keys.each {|k| fresh_data[k] = data[k]} | ||
data = fresh_data | ||
session = fresh_session | ||
end | ||
session.update_session(marshalize(data)) | ||
end | ||
return data, session | ||
end | ||
end | ||
|
||
# This software is released under the MIT license | ||
# Copyright (c) 2007-2009 Frederick Cheung | ||
|
||
# Permission is hereby granted, free of charge, to any person obtaining | ||
# a copy of this software and associated documentation files (the | ||
# "Software"), to deal in the Software without restriction, including | ||
# without limitation the rights to use, copy, modify, merge, publish, | ||
# distribute, sublicense, and/or sell copies of the Software, and to | ||
# permit persons to whom the Software is furnished to do so, subject to | ||
# the following conditions: | ||
|
||
# The above copyright notice and this permission notice shall be | ||
# included in all copies or substantial portions of the Software. | ||
|
||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ mysql: | |
:host: localhost | ||
:username: smart_session | ||
:password: | ||
:database: smart_session | ||
:database: smart_session |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.