h2ocube_rails_cache 0.0.13 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e6fdaa111c24c74f76968d52aa0c38baec02601f
4
- data.tar.gz: 78296c48b10451b8b962d1cc778aca9dca6b4a38
3
+ metadata.gz: 6bcffee68231cef62c1326e5e2138304810eadbd
4
+ data.tar.gz: 4fba5b4127457e7cae04744fae8389e419f16cbb
5
5
  SHA512:
6
- metadata.gz: 88be1bbae3b0e746009e637841014d2464fdbfc578c8278e88a1e19928ba0145028b8ae95a759fe7da85c5bf9559e77179bb325c4c7613fbe215a2da881d80b8
7
- data.tar.gz: d7295800b31495b54e493e68be8ea36048b1ec838f129e5f9d5635ef27cb777ff6af7473c5ec8ce04b8440128145e3ead2a1cdffb62c59b01e6983d020862bd7
6
+ metadata.gz: 0155ccd5909e64f348b26198a848ec4e9eac5c4953976f3d074a5900d6da5f84afbcd9d3ca1e72aa55ff80305250b26d69995459d0f8c841af149d9bf480bf0c
7
+ data.tar.gz: 2ececa04a39fba44fbc698339e96e3888261e0294ea0923dde2bfbe9cfc03f1042852f92cb211f6fc0c8d4c64a0398f197cd018fdca656e6ab57855403b6d344
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
- source 'https://rubygems.org'
1
+ # source 'https://rubygems.org'
2
+ source 'https://gems.ruby-china.org'
2
3
 
3
4
  # Specify your gem's dependencies in h2ocube_rails_assets.gemspec
4
5
  gemspec
data/README.md CHANGED
@@ -3,20 +3,18 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/h2ocube_rails_cache.png)](http://badge.fury.io/rb/h2ocube_rails_cache)
4
4
  [![Build Status](https://travis-ci.org/h2ocube/h2ocube_rails_cache.png?branch=master)](https://travis-ci.org/h2ocube/h2ocube_rails_cache)
5
5
 
6
- Just an redis cache.
6
+ Just an redis cache. Default expires_in `60.minutes`.
7
7
 
8
8
  ## Installation
9
9
 
10
10
  Add this line to your application's Gemfile:
11
11
 
12
- gem 'h2ocube_rails_cache', group: :production
12
+ gem 'h2ocube_rails_cache'
13
13
 
14
14
  And then execute:
15
15
 
16
16
  $ bundle
17
17
 
18
- Disable default session_store in config/initializers/session_store.rb
19
-
20
18
  ## Rails.cache support methods
21
19
 
22
20
  * `keys key = '*'`
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = 'h2ocube_rails_cache'
7
- gem.version = '0.0.13'
7
+ gem.version = '0.1.0'
8
8
  gem.authors = ['Ben']
9
9
  gem.email = ['ben@h2ocube.com']
10
10
  gem.description = 'Just an redis cache.'
@@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ['lib']
19
19
 
20
- gem.add_dependency 'redis-namespace'
20
+ gem.add_dependency 'redis'
21
21
 
22
22
  %w(rails minitest-rails).each { |g| gem.add_development_dependency g }
23
23
  end
@@ -1,24 +1,28 @@
1
1
  require 'redis'
2
- require 'redis/namespace'
3
2
 
4
3
  module ActiveSupport
5
4
  module Cache
6
5
  class H2ocubeRailsCache < Store
7
- def initialize(options = nil, &blk)
6
+ attr_reader :config, :namespace, :data
7
+
8
+ def initialize(options = {})
8
9
  options ||= {}
10
+ @config = options
11
+ @data = Redis.new(options)
9
12
  super(options)
10
- @data = Redis::Namespace.new("#{Rails.application.class.to_s.split('::').first}:#{Rails.env}:Cache", redis: Redis::Store.new)
11
13
  end
12
14
 
13
15
  def keys(key = '*')
14
- key = expanded_key key
16
+ options.reverse_merge! config
17
+ key = normalize_key key, config
15
18
  @data.keys key
16
19
  end
17
20
 
18
21
  def fetch(key, options = {}, &block)
19
- key = expanded_key key
22
+ options.reverse_merge! config
23
+ key = normalize_key(key, options)
20
24
 
21
- if exist?(key)
25
+ if @data.exists(key)
22
26
  if options.key?(:force)
23
27
  force = options[:force].is_a?(Proc) ? options[:force].call(key, options) : options[:force]
24
28
  if force
@@ -39,21 +43,25 @@ module ActiveSupport
39
43
  end
40
44
 
41
45
  def fetch_raw(key, options = {}, &block)
46
+ options.reverse_merge! config
47
+ key = normalize_key key, options
42
48
  instrument :fetch, key, options do
43
- exist?(key) ? read(key, options) : write(key, block, options)
49
+ exist?(key) ? read(key) : write(key, block, options)
44
50
  end
45
51
  end
46
52
 
47
53
  def read(key, options = {})
48
- key = expanded_key key
54
+ options.reverse_merge! config
55
+ key = normalize_key key, options
49
56
  return nil if key.start_with?('http')
50
57
  instrument :read, key, options do
51
58
  exist?(key) ? load_entry(@data.get(key)) : nil
52
59
  end
53
60
  end
54
61
 
55
- def read_raw(key, _options = {})
56
- key = expanded_key key
62
+ def read_raw(key, options = {})
63
+ options.reverse_merge! config
64
+ key = normalize_key key, options
57
65
  @data.get key
58
66
  end
59
67
 
@@ -70,7 +78,9 @@ module ActiveSupport
70
78
  end
71
79
 
72
80
  def write(key, entry, options = {})
73
- key = expanded_key key
81
+ options.reverse_merge! config
82
+ key = normalize_key(key, options)
83
+
74
84
  return false if key.start_with?('http')
75
85
 
76
86
  instrument :write, key, options do
@@ -79,15 +89,17 @@ module ActiveSupport
79
89
  Rails.logger.warn "CacheWarn: '#{key}' is not cacheable!"
80
90
  nil
81
91
  else
82
- @data.set key, entry, options
83
- @data.set "#{key}_updated_at", Time.now.to_i if options[:updated_at]
92
+ expires_in = options[:expires_in].to_i
93
+ @data.setex key, expires_in, entry
94
+ @data.setex "#{key}_updated_at", expires_in, Time.now.to_i if options[:updated_at]
84
95
  load_entry entry
85
96
  end
86
97
  end
87
98
  end
88
99
 
89
100
  def delete(key, options = {})
90
- key = expanded_key key
101
+ options.reverse_merge! config
102
+ key = normalize_key key, options
91
103
 
92
104
  instrument :delete, key, options do
93
105
  @data.keys(key).each { |k| @data.del k }
@@ -95,8 +107,9 @@ module ActiveSupport
95
107
  end
96
108
  end
97
109
 
98
- def exist?(key, _options = {})
99
- key = expanded_key key
110
+ def exist?(key, options = {})
111
+ options.reverse_merge! config
112
+ key = normalize_key key, options
100
113
  @data.exists key
101
114
  end
102
115
 
@@ -111,10 +124,11 @@ module ActiveSupport
111
124
  @data.info
112
125
  end
113
126
 
114
- def increment(key, amount = 1, _options = {})
115
- key = expanded_key key
127
+ def increment(key, amount = 1, options = {})
128
+ options.reverse_merge! config
129
+ key = normalize_key key, options
116
130
 
117
- instrument :increment, key, amount do
131
+ instrument :increment, key, amount: amount do
118
132
  if amount == 1
119
133
  @data.incr key
120
134
  else
@@ -123,10 +137,11 @@ module ActiveSupport
123
137
  end
124
138
  end
125
139
 
126
- def decrement(key, amount = 1, _options = {})
127
- key = expanded_key key
140
+ def decrement(key, amount = 1, options = {})
141
+ options.reverse_merge! config
142
+ key = normalize_key key, options
128
143
 
129
- instrument :decrement, key, amount do
144
+ instrument :decrement, key, amount: amount do
130
145
  if amount == 1
131
146
  @data.decr key
132
147
  else
@@ -141,27 +156,35 @@ module ActiveSupport
141
156
 
142
157
  private
143
158
 
144
- def instrument(operation, key, options = {})
145
- payload = { key: key }
146
- payload.merge!(options) if options.is_a?(Hash)
147
- ActiveSupport::Notifications.instrument("cache_#{operation}.active_support", payload) { yield(payload) }
148
- end
149
-
150
- def log(operation, key, options = {})
151
- return unless logger && logger.debug? && !silence?
152
- logger.debug(" \e[95mCACHE #{operation}\e[0m #{key}#{options.blank? ? "" : " (#{options.inspect})"}")
153
- end
159
+ def normalize_key(key, options)
160
+ key = expanded_key(key)
161
+ namespace = options[:namespace] if options
162
+ prefix = namespace.is_a?(Proc) ? namespace.call : namespace
163
+ key = "#{prefix}:#{key}" if prefix && !key.start_with?(prefix)
164
+ key
165
+ end
166
+
167
+ # def instrument(operation, key, options = {})
168
+ # payload = { key: key }
169
+ # payload.merge!(options) if options.is_a?(Hash)
170
+ # ActiveSupport::Notifications.instrument("cache_#{operation}.active_support", payload) { yield(payload) }
171
+ # end
172
+ #
173
+ # def log(operation, key, options = {})
174
+ # return unless logger && logger.debug? && !silence?
175
+ # logger.debug(" \e[95mCACHE #{operation}\e[0m #{key}#{options.blank? ? "" : " (#{options.inspect})"}")
176
+ # end
154
177
 
155
178
  def dump_entry(entry)
156
179
  entry = entry.call if entry.class.to_s == 'Proc'
157
180
 
158
181
  case entry.class.to_s
159
- when 'String', 'Fixnum', 'Float'
182
+ when 'String', 'Integer', 'Float'
160
183
  entry
161
184
  else
162
185
  begin
163
186
  Marshal.dump entry
164
- rescue Exception => e
187
+ rescue => e
165
188
  Rails.logger.error "CacheError: #{e}"
166
189
  return nil
167
190
  end
@@ -170,7 +193,7 @@ module ActiveSupport
170
193
 
171
194
  def load_entry(entry)
172
195
  begin
173
- Marshal.load entry
196
+ Marshal.load(entry)
174
197
  rescue
175
198
  return entry.to_f if entry.respond_to?(:to_f) && entry.to_f.to_s == entry
176
199
  return entry.to_i if entry.respond_to?(:to_i) && entry.to_i.to_s == entry
@@ -1,28 +1,9 @@
1
- # coding: utf-8
2
1
  require 'active_support/cache/h2ocube_rails_cache'
3
- require 'rack/session/h2ocube_rails_cache_session'
4
- require 'action_dispatch/middleware/session/h2ocube_rails_cache_session'
5
-
6
- class Redis
7
- class Store < self
8
- def set(key, value, options = nil)
9
- if options && options[:expires_in]
10
- setex(key, options[:expires_in].to_i, value)
11
- else
12
- super(key, value)
13
- end
14
- end
15
- end
16
- end
17
2
 
18
3
  module H2ocubeRailsCache
19
4
  class Railtie < Rails::Railtie
20
- config.before_configuration do |app|
21
- app.config.cache_store = :h2ocube_rails_cache
22
- app.config.session_store :h2ocube_rails_cache_session
23
- end
24
-
25
- config.after_initialize do
5
+ config.after_initialize do |app|
6
+ Rails.cache = ActiveSupport::Cache.lookup_store :h2ocube_rails_cache, namespace: "#{Rails.application.class.to_s.split("::").first}:#{Rails.env}#{ENV['TEST_ENV_NUMBER']}", expires_in: 60.minutes
26
7
  Rails.cache.logger = Rails.logger
27
8
 
28
9
  ActiveSupport::Notifications.subscribe(/cache_[^.]+.active_support/) do |name, start, finish, id, payload|
data/lib/tasks/tmp.rake CHANGED
@@ -1,13 +1,6 @@
1
1
  namespace :tmp do
2
- namespace :sessions do
3
- task :clear => :environment do
4
- FileUtils.rm(Dir['tmp/sessions/[^.]*'])
5
- Rails.application.config.session_store.clear
6
- end
7
- end
8
-
9
2
  namespace :cache do
10
- task :clear => :environment do
3
+ task clear: :environment do
11
4
  FileUtils.rm_rf(Dir['tmp/cache/[^.]*'])
12
5
  Rails.cache.clear
13
6
  end
data/test/cache_test.rb CHANGED
@@ -2,15 +2,11 @@ require 'test_helper'
2
2
 
3
3
  describe 'h2ocube_rails_cache' do
4
4
  before do
5
- @redis = Redis.new
6
- @cache_key = "#{Rails.application.class.to_s.split("::").first}:#{Rails.env}:Cache"
7
- @cache = Redis::Namespace.new(@cache_key)
8
5
  Rails.cache.clear
9
6
  end
10
7
 
11
8
  it 'should work' do
12
9
  Rails.cache.class.to_s.must_equal 'ActiveSupport::Cache::H2ocubeRailsCache'
13
- Rails.application.config.session_store.to_s.must_equal 'ActionDispatch::Session::H2ocubeRailsCacheSession'
14
10
  end
15
11
 
16
12
  it '.keys' do
@@ -20,8 +16,6 @@ describe 'h2ocube_rails_cache' do
20
16
  it '.clear' do
21
17
  Rails.cache.clear.must_be_same_as true
22
18
  Rails.cache.keys('*').must_be_empty
23
-
24
- @redis.keys(@cache_key).must_be_empty
25
19
  end
26
20
 
27
21
  it '.write, .exist?, .read and .delete' do
@@ -31,8 +25,6 @@ describe 'h2ocube_rails_cache' do
31
25
 
32
26
  Rails.cache.read('a').must_equal true
33
27
 
34
- Marshal.load(@redis.get("#{@cache_key}:a")).must_equal true
35
-
36
28
  Rails.cache.delete('a').must_be_same_as true
37
29
 
38
30
  Rails.cache.exist?('a').must_be_same_as false
@@ -42,7 +34,7 @@ describe 'h2ocube_rails_cache' do
42
34
 
43
35
  it 'expire' do
44
36
  Rails.cache.delete 'expire'
45
- Rails.cache.write 'expire', 1, expires_in: 1
37
+ Rails.cache.write 'expire', true, expires_in: 1.second
46
38
  Rails.cache.exist?('expire').must_be_same_as true
47
39
  sleep 2
48
40
  Rails.cache.exist?('expire').must_be_same_as false
@@ -50,11 +42,11 @@ describe 'h2ocube_rails_cache' do
50
42
 
51
43
  it 'key class' do
52
44
  Rails.cache.write ['a', 0], 'a0'
53
- Rails.cache.keys[0].must_equal 'a/0'
45
+ Rails.cache.keys[0].must_equal "#{Rails.cache.config[:namespace]}:a/0"
54
46
  Rails.cache.clear
55
47
 
56
- Rails.cache.write({a: 0}, 'a0')
57
- Rails.cache.keys[0].must_equal 'a=0'
48
+ Rails.cache.write({ a: 0 }, 'a0')
49
+ Rails.cache.keys[0].must_equal "#{Rails.cache.config[:namespace]}:a=0"
58
50
  Rails.cache.clear
59
51
  end
60
52
 
@@ -62,13 +54,13 @@ describe 'h2ocube_rails_cache' do
62
54
  Rails.cache.write 'String', 'String'
63
55
  Rails.cache.read_raw('String').must_be_kind_of String
64
56
 
65
- Rails.cache.write 'Fixnum', 1
66
- Rails.cache.read('Fixnum').must_be_kind_of Fixnum
57
+ Rails.cache.write 'Integer', 1
58
+ Rails.cache.read('Integer').must_be_kind_of Integer
67
59
 
68
60
  Rails.cache.write 'Float', 1.1
69
61
  Rails.cache.read('Float').must_be_kind_of Float
70
62
 
71
- Rails.cache.write 'Proc', Proc.new{ 1 }
63
+ Rails.cache.write 'Proc', proc { 1 }
72
64
  Rails.cache.read('Proc').must_equal 1
73
65
  end
74
66
 
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../config/application', __dir__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
data/test/test_helper.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  ENV['RAILS_ENV'] = 'test'
2
2
 
3
- require File.expand_path('../dummy/config/environment.rb', __FILE__)
3
+ require File.expand_path('../dummy/config/environment.rb', __FILE__)
4
4
  require 'rails/test_help'
5
5
  require 'minitest/rails'
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: h2ocube_rails_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-26 00:00:00.000000000 Z
11
+ date: 2017-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: redis-namespace
14
+ name: redis
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -66,10 +66,8 @@ files:
66
66
  - README.md
67
67
  - Rakefile
68
68
  - h2ocube_rails_cache.gemspec
69
- - lib/action_dispatch/middleware/session/h2ocube_rails_cache_session.rb
70
69
  - lib/active_support/cache/h2ocube_rails_cache.rb
71
70
  - lib/h2ocube_rails_cache.rb
72
- - lib/rack/session/h2ocube_rails_cache_session.rb
73
71
  - lib/tasks/tmp.rake
74
72
  - test/cache_test.rb
75
73
  - test/dummy/Rakefile
@@ -77,6 +75,7 @@ files:
77
75
  - test/dummy/app/helpers/application_helper.rb
78
76
  - test/dummy/app/views/application/home.html.erb
79
77
  - test/dummy/app/views/layouts/application.html.erb
78
+ - test/dummy/bin/rails
80
79
  - test/dummy/config.ru
81
80
  - test/dummy/config/application.rb
82
81
  - test/dummy/config/boot.rb
@@ -89,7 +88,6 @@ files:
89
88
  - test/dummy/config/initializers/session_store.rb
90
89
  - test/dummy/config/locales/en.yml
91
90
  - test/dummy/config/routes.rb
92
- - test/dummy/script/rails
93
91
  - test/test_helper.rb
94
92
  homepage: https://github.com/h2ocube/h2ocube_rails_cache
95
93
  licenses:
@@ -111,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
109
  version: '0'
112
110
  requirements: []
113
111
  rubyforge_project:
114
- rubygems_version: 2.6.4
112
+ rubygems_version: 2.6.11
115
113
  signing_key:
116
114
  specification_version: 4
117
115
  summary: Just an redis cache.
@@ -122,6 +120,7 @@ test_files:
122
120
  - test/dummy/app/helpers/application_helper.rb
123
121
  - test/dummy/app/views/application/home.html.erb
124
122
  - test/dummy/app/views/layouts/application.html.erb
123
+ - test/dummy/bin/rails
125
124
  - test/dummy/config.ru
126
125
  - test/dummy/config/application.rb
127
126
  - test/dummy/config/boot.rb
@@ -134,5 +133,4 @@ test_files:
134
133
  - test/dummy/config/initializers/session_store.rb
135
134
  - test/dummy/config/locales/en.yml
136
135
  - test/dummy/config/routes.rb
137
- - test/dummy/script/rails
138
136
  - test/test_helper.rb
@@ -1,22 +0,0 @@
1
- require 'redis'
2
- require 'redis/namespace'
3
- require 'action_dispatch/middleware/session/abstract_store'
4
-
5
- module ActionDispatch
6
- module Session
7
- class H2ocubeRailsCacheSession < Rack::Session::H2ocubeRailsCacheSession
8
- include Compatibility
9
- include StaleSessionCheck
10
- def initialize(app, options = {})
11
- options = options.dup
12
- super
13
- end
14
-
15
- def self.clear
16
- r = Redis::Namespace.new("#{Rails.application.class.to_s.split("::").first}:#{Rails.env}:Session", redis: Redis::Store.new)
17
- r.keys('*').each{ |k| r.del k }
18
- true
19
- end
20
- end
21
- end
22
- end
@@ -1,69 +0,0 @@
1
- require 'redis'
2
- require 'redis/namespace'
3
- require 'rack/session/abstract/id'
4
-
5
- module Rack
6
- module Session
7
- class H2ocubeRailsCacheSession < Abstract::ID
8
- attr_reader :mutex, :pool
9
-
10
- DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge \
11
- expire_after: 30.days
12
-
13
- def initialize(app, options = nil)
14
- super
15
-
16
- @mutex = Mutex.new
17
- @pool = Redis::Namespace.new("#{Rails.application.class.to_s.split("::").first}:#{Rails.env}:Session", redis: Redis::Store.new)
18
- end
19
-
20
- def generate_sid
21
- loop do
22
- sid = super
23
- break sid unless @pool.get(sid)
24
- end
25
- end
26
-
27
- def get_session(env, sid)
28
- with_lock(env, [nil, {}]) do
29
- unless sid and session = @pool.get(sid)
30
- sid, session = generate_sid, Hash.new
31
- unless /^OK/ =~ @pool.set(sid, Marshal.dump(session), @default_options)
32
- raise "Session collision on '#{sid.inspect}'"
33
- end
34
- else
35
- session = Marshal.load(session)
36
- end
37
- [sid, session]
38
- end
39
- end
40
-
41
- def set_session(env, session_id, new_session, options)
42
- with_lock(env, false) do
43
- @pool.set session_id, Marshal.dump(new_session), options
44
- session_id
45
- end
46
- end
47
-
48
- def destroy_session(env, session_id, options)
49
- with_lock(env) do
50
- @pool.del(session_id)
51
- generate_sid unless options[:drop]
52
- end
53
- end
54
-
55
- def with_lock(env, default=nil)
56
- @mutex.lock if env['rack.multithread']
57
- yield
58
- rescue Errno::ECONNREFUSED
59
- if $VERBOSE
60
- warn "#{self} is unable to find Redis server."
61
- warn $!.inspect
62
- end
63
- default
64
- ensure
65
- @mutex.unlock if @mutex.locked?
66
- end
67
- end
68
- end
69
- end
@@ -1,6 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
-
4
- APP_PATH = File.expand_path('../../config/application', __FILE__)
5
- require File.expand_path('../../config/boot', __FILE__)
6
- require 'rails/commands'