Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added rails session store #27

Merged
merged 3 commits into from

2 participants

Alexander Shapiotko Travis Reeder
Alexander Shapiotko

Added ability to use gem as rails session store

Travis Reeder treeder merged commit 7e70165 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
22 README.md
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
+```
93 lib/action_dispatch/session/iron_cache.rb
View
@@ -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
5 lib/iron_cache.rb
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
Something went wrong with that request. Please try again.