iron_cache_rails 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ .DS_Store
2
+ pkg/
3
+ doc
4
+ .idea/
5
+ log
6
+ test/config.yml
7
+ *.sublime*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ iron_cache_rails (0.1.0)
5
+ iron_cache (>= 1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ iron_cache (1.3.0)
11
+ iron_core (>= 0.4.2)
12
+ iron_core (0.4.3)
13
+ rest (>= 2.0.2)
14
+ mime-types (1.19)
15
+ minitest (4.1.0)
16
+ net-http-persistent (2.8)
17
+ rake (0.9.2.2)
18
+ rest (2.1.0)
19
+ net-http-persistent
20
+ rest-client (>= 0.3.0)
21
+ rest-client (1.6.7)
22
+ mime-types (>= 1.16)
23
+ test-unit (2.5.2)
24
+ uber_config (1.0.5)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ iron_cache_rails!
31
+ minitest
32
+ rake
33
+ test-unit
34
+ uber_config
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ IronCache Rails Support Client
2
+ -------------
3
+
4
+
5
+ Using As Rails Store
6
+ ====================
7
+
8
+ You can use IronCache as any other rails store. Put iron.json into your project's config dir, add iron_cache to Gemfile and you are ready to go.
9
+
10
+ config.cache_store = :iron_cache_store
11
+
12
+ Alternatively, you can supply project_id and token in code.
13
+
14
+ config.cache_store = :iron_cache_store, :project_id => 'XXX', :token => 'YYY'
15
+
16
+
17
+ Using As Rails Session Store
18
+ ====================
19
+
20
+ 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.
21
+
22
+ `config/initializers/session_store.rb` :
23
+
24
+ ```ruby
25
+ AppName::Application.config.session_store :iron_cache_store
26
+ ```
27
+
28
+ Alternatively, you can supply project_id and token in code.
29
+
30
+ ```ruby
31
+ AppName::Application.config.session_store :iron_cache,
32
+ project_id: 'XXX',
33
+ token: 'YYY',
34
+ namespace: 'other-cache-name',
35
+ expires_in: 7200
36
+ ```
37
+
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/test_*.rb'
8
+ test.verbose = true
9
+ end
10
+
11
+ task :default => :test
12
+
13
+ require 'rdoc/task'
14
+ Rake::RDocTask.new do |rdoc|
15
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
16
+
17
+ rdoc.rdoc_dir = 'doc'
18
+ rdoc.title = "iron_cache_rails #{version}"
19
+ rdoc.rdoc_files.include('README*')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path('../lib/iron_cache_rails/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Travis Reeder"]
5
+ gem.email = ["treeder@gmail.com"]
6
+ gem.description = "Rails cache store and session store using IronCache by www.iron.io"
7
+ gem.summary = gem.description
8
+ gem.homepage = "https://github.com/iron-io/iron_cache_rails"
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.name = "iron_cache_rails"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = IronCache::VERSION
16
+
17
+ gem.required_rubygems_version = ">= 1.3.6"
18
+ gem.required_ruby_version = Gem::Requirement.new(">= 1.9")
19
+ gem.add_runtime_dependency "iron_cache", ">= 1"
20
+
21
+ gem.add_development_dependency "test-unit"
22
+ gem.add_development_dependency "minitest"
23
+ gem.add_development_dependency "rake"
24
+ gem.add_development_dependency "uber_config"
25
+
26
+
27
+ end
28
+
@@ -0,0 +1,93 @@
1
+ require 'iron_cache'
2
+ require 'base64'
3
+ require 'action_dispatch/middleware/session/abstract_store'
4
+
5
+ module ActionDispatch
6
+ module Session
7
+ class IronCache < ActionDispatch::Session::AbstractStore
8
+
9
+ def initialize(app, options = {})
10
+ @options = options
11
+ super
12
+
13
+ @client = ::IronCache::Client.new(options)
14
+ end
15
+
16
+ def options
17
+ @options
18
+ end
19
+
20
+ def get_session(env, session_id)
21
+ item = nil
22
+ session_id ||= generate_sid
23
+
24
+ with_namespace(session_id, options) do |cache, k|
25
+ item = cache.get(k)
26
+ item = item.value unless item.nil?
27
+ end
28
+
29
+ session_data = deserialize_entry(item) rescue {}
30
+
31
+ [session_id, session_data]
32
+ end
33
+
34
+ def set_session(env, session_id, session, options)
35
+ with_namespace(session_id, options) do |cache, k|
36
+ cache.put(k, serialize_entry(session, options), options)
37
+ end
38
+
39
+ session_id
40
+ end
41
+
42
+ def destroy_session(env, session_id, options)
43
+ with_namespace(session_id, options) do |cache, k|
44
+ cache.delete(k)
45
+ end
46
+
47
+ generate_sid
48
+ end
49
+
50
+ private
51
+
52
+ def with_namespace(key, options)
53
+ options[:namespace] ||= 'rails_cache'
54
+
55
+ cache_name = options[:namespace]
56
+
57
+ yield(@client.cache(cache_name), escape_key(key))
58
+ end
59
+
60
+ def escape_key(key)
61
+ ekey = ::Base64.encode64(key)
62
+
63
+ if ekey.size > 250
64
+ ekey = "#{key[0, 213]}:md5:#{Digest::MD5.hexdigest(key)}"
65
+ end
66
+
67
+ ekey
68
+ end
69
+
70
+ def deserialize_entry(raw_value)
71
+ if raw_value
72
+ raw_value = ::Base64.decode64(raw_value) rescue raw_value
73
+ Marshal.load(raw_value) rescue raw_value
74
+ else
75
+ nil
76
+ end
77
+ end
78
+
79
+ def serialize_entry(entry, options)
80
+ if options[:raw]
81
+ if entry.respond_to?(:value)
82
+ entry.value.to_s
83
+ else
84
+ entry.to_s
85
+ end
86
+ else
87
+ ::Base64.encode64 Marshal.dump(entry)
88
+ end
89
+ end
90
+
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,102 @@
1
+ require 'iron_cache'
2
+ require 'base64'
3
+
4
+ module ActiveSupport
5
+ module Cache
6
+ class IronCacheStore < ActiveSupport::Cache::Store
7
+
8
+ def initialize(options = nil)
9
+ super(options)
10
+
11
+ @client = IronCache::Client.new(@options)
12
+
13
+ extend ActiveSupport::Cache::Strategy::LocalCache
14
+ end
15
+
16
+ def increment(key, amount = 1, options = nil)
17
+ with_namespace(key, options) do |cache, k|
18
+ cache.increment(k, amount)
19
+ end
20
+ end
21
+
22
+ def decrement(key, amount = 1, options = nil)
23
+ with_namespace(key, options) do |cache, k|
24
+ cache.increment(k, -amount)
25
+ end
26
+ end
27
+
28
+ protected
29
+
30
+ def read_entry(key, options)
31
+ item = nil
32
+
33
+ with_namespace(key, options) do |cache, k|
34
+ item = cache.get(k)
35
+ item = item.value unless item.nil?
36
+ end
37
+
38
+ deserialize_entry(item)
39
+ end
40
+
41
+ def write_entry(key, entry, options)
42
+ with_namespace(key, options) do |cache, k|
43
+ cache.put(k, serialize_entry(entry, options), options)
44
+ end
45
+
46
+ true
47
+ end
48
+
49
+ def delete_entry(key, options)
50
+ with_namespace(key, options) do |cache, k|
51
+ cache.delete(k)
52
+ end
53
+
54
+ true
55
+ end
56
+
57
+ private
58
+
59
+ def with_namespace(key, options)
60
+ options[:namespace] ||= 'rails_cache'
61
+
62
+ cache_name, key_name = namespaced_key(key, options).split(':', 2)
63
+
64
+ yield(@client.cache(cache_name), escape_key(key_name))
65
+ end
66
+
67
+ def escape_key(key)
68
+ ekey = ::Base64.encode64(key)
69
+
70
+ if ekey.size > 250
71
+ ekey = "#{key[0, 213]}:md5:#{Digest::MD5.hexdigest(key)}"
72
+ end
73
+
74
+ ekey
75
+ end
76
+
77
+ def deserialize_entry(raw_value)
78
+ if raw_value
79
+ raw_value = ::Base64.decode64(raw_value) rescue raw_value
80
+ entry = Marshal.load(raw_value) rescue raw_value
81
+ entry.is_a?(ActiveSupport::Cache::Entry) ? entry : ActiveSupport::Cache::Entry.new(entry)
82
+ else
83
+ nil
84
+ end
85
+ end
86
+
87
+ def serialize_entry(entry, options)
88
+ value = nil
89
+
90
+ if options[:raw]
91
+ if entry.respond_to?(:value)
92
+ value = entry.value.to_s
93
+ else
94
+ value = entry.to_s
95
+ end
96
+ else
97
+ value = ::Base64.encode64 Marshal.dump(entry)
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,3 @@
1
+ module IronCache
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'rest'
2
+ require_relative "iron_cache_rails/version"
3
+
4
+ # session store
5
+ if defined? ActionDispatch
6
+ require_relative 'action_dispatch/session/iron_cache'
7
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iron_cache_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Travis Reeder
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: iron_cache
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '1'
30
+ - !ruby/object:Gem::Dependency
31
+ name: test-unit
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: uber_config
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Rails cache store and session store using IronCache by www.iron.io
95
+ email:
96
+ - treeder@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - Gemfile.lock
104
+ - README.md
105
+ - Rakefile
106
+ - iron_cache_rails.gemspec
107
+ - lib/action_dispatch/session/iron_cache.rb
108
+ - lib/active_support/cache/iron_cache_store.rb
109
+ - lib/iron_cache_rails.rb
110
+ - lib/iron_cache_rails/version.rb
111
+ homepage: https://github.com/iron-io/iron_cache_rails
112
+ licenses: []
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '1.9'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: 1.3.6
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 1.8.24
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: Rails cache store and session store using IronCache by www.iron.io
135
+ test_files: []