Permalink
Browse files

Merge pull request #27 from thousandsofthem/add-session-store

Added rails session store
  • Loading branch information...
2 parents 5b27651 + 84fe276 commit 7e7016553ee71d45036655a425d8f27d8056e32b @treeder treeder committed Nov 17, 2012
Showing with 120 additions and 0 deletions.
  1. +22 −0 README.md
  2. +93 −0 lib/action_dispatch/session/iron_cache.rb
  3. +5 −0 lib/iron_cache.rb
View
@@ -66,3 +66,25 @@ You can use IronCache as any other rails store. Put iron.json into your project'
Alternatively, you can supply project_id and token in code.
config.cache_store = :iron_cache_store, :project_id => 'XXX', :token => 'YYY'
+
+
+Using As Rails Session Store
+====================
+
+You can use IronCache as any other rails session store. Put iron.json into your project's config dir, add iron_cache to Gemfile and you are ready to go.
+
+`config/initializers/session_store.rb` :
+
+```ruby
+AppName::Application.config.session_store :iron_cache_store
+```
+
+Alternatively, you can supply project_id and token in code.
+
+```ruby
+AppName::Application.config.session_store :iron_cache,
+ project_id: 'XXX',
+ token: 'YYY',
+ namespace: 'other-cache-name',
+ expires_in: 7200
+```
@@ -0,0 +1,93 @@
+require 'iron_cache'
+require 'base64'
+require 'action_dispatch/middleware/session/abstract_store'
+
+module ActionDispatch
+ module Session
+ class IronCache < ActionDispatch::Session::AbstractStore
+
+ def initialize(app, options = {})
+ @options = options
+ super
+
+ @client = ::IronCache::Client.new(options)
+ end
+
+ def options
+ @options
+ end
+
+ def get_session(env, session_id)
+ item = nil
+ session_id ||= generate_sid
+
+ with_namespace(session_id, options) do |cache, k|
+ item = cache.get(k)
+ item = item.value unless item.nil?
+ end
+
+ session_data = deserialize_entry(item) rescue {}
+
+ [session_id, session_data]
+ end
+
+ def set_session(env, session_id, session, options)
+ with_namespace(session_id, options) do |cache, k|
+ cache.put(k, serialize_entry(session, options), options)
+ end
+
+ session_id
+ end
+
+ def destroy_session(env, session_id, options)
+ with_namespace(session_id, options) do |cache, k|
+ cache.delete(k)
+ end
+
+ generate_sid
+ end
+
+ private
+
+ def with_namespace(key, options)
+ options[:namespace] ||= 'rails_cache'
+
+ cache_name = options[:namespace]
+
+ yield(@client.cache(cache_name), escape_key(key))
+ end
+
+ def escape_key(key)
+ ekey = ::Base64.encode64(key)
+
+ if ekey.size > 250
+ ekey = "#{key[0, 213]}:md5:#{Digest::MD5.hexdigest(key)}"
+ end
+
+ ekey
+ end
+
+ def deserialize_entry(raw_value)
+ if raw_value
+ raw_value = ::Base64.decode64(raw_value) rescue raw_value
+ Marshal.load(raw_value) rescue raw_value
+ else
+ nil
+ end
+ end
+
+ def serialize_entry(entry, options)
+ if options[:raw]
+ if entry.respond_to?(:value)
+ entry.value.to_s
+ else
+ entry.to_s
+ end
+ else
+ ::Base64.encode64 Marshal.dump(entry)
+ end
+ end
+
+ end
+ end
+end
View
@@ -3,3 +3,8 @@
require_relative 'iron_cache/caches'
require_relative 'iron_cache/items'
require_relative 'iron_cache/client'
+
+# session store
+if defined? ActionDispatch
+ require_relative 'action_dispatch/session/iron_cache'
+end

0 comments on commit 7e70165

Please sign in to comment.