h2ocube_rails_cache 0.0.10 → 0.0.11

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: d8035a1f77233f7ff3329e9c709e58053fb7b5e5
4
- data.tar.gz: 2278ba3f53b73bdfe300a77a3b937007949f3140
3
+ metadata.gz: fb26049cd9b2c220429a627cd0e72106c283215a
4
+ data.tar.gz: a3886f5a62833fc2334bd14027c6de2f40fb5d21
5
5
  SHA512:
6
- metadata.gz: 6ecad18d2d880acccbd9a3dfebb5d01df2e5a7c4d246a01775227157e254697b3a3317a532964cfdd6f4d1a6bb74a4a860996033ed35655f3357014619004617
7
- data.tar.gz: 6cccea34813f6e1ffbc7e34bba2943a2857f364041665398c43451ee1d2f46e53545c2cfb0c1ad23a3af7a4aaa7d3cabbcd864440d19dc6e631beeeb06254f49
6
+ metadata.gz: d9dc5b840bbeae7865f2206dc0f2650717782f41ebd568b29177f132b238a58703241d0d23b9a7665f754f900cddf272212c297da8985aa499edf95f16c74341
7
+ data.tar.gz: e90bd05c7877aa8a9f96c58cd567482a82ce0a5d869e080c267ed239f231c549f142eee5925f2603496ecf0a162d49f8f9206be1d9c7bd96264d48d47c1eba6c
data/.travis.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0.0
3
+ - 2.3.1
4
4
  services:
5
5
  - redis-server
6
6
  script: bundle exec rake test
data/README.md CHANGED
@@ -22,7 +22,7 @@ Disable default session_store in config/initializers/session_store.rb
22
22
  * `keys key = '*'`
23
23
  * `read key, options = nil`
24
24
  * `write key, entry, options = nil`
25
- * `fetch key, options = nil, &blk`
25
+ * `fetch key, options = nil, &block`
26
26
  * `delete key, options = nil`
27
27
  * `exist? key, options = nil`
28
28
  * `increment key, amount = 1, options = nil`
@@ -30,9 +30,15 @@ Disable default session_store in config/initializers/session_store.rb
30
30
  * `clear`
31
31
  * `info`
32
32
 
33
- ## Support Options
33
+ ## Write Options
34
34
 
35
- * `expires_in` # Rails.cache.write 'key', 'value', expires_in: 1.minute
35
+ * `created_at` will write timestamp with key_created_at
36
+
37
+ ## Fetch Options
38
+
39
+ * `expires_in` such as 5.minutes
40
+ * `if` true / false or Proc that return true / false
41
+ * `created_at` will write timestamp with key_created_at
36
42
 
37
43
  ## Task changed
38
44
 
data/Rakefile CHANGED
@@ -1,11 +1,10 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new(:test) do |t|
5
5
  t.libs << 'lib'
6
6
  t.libs << 'test'
7
7
  t.pattern = 'test/**/*_test.rb'
8
- t.verbose = false
9
8
  end
10
9
 
11
- task :default => :test
10
+ task default: :test
@@ -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.10'
7
+ gem.version = '0.0.11'
8
8
  gem.authors = ['Ben']
9
9
  gem.email = ['ben@h2ocube.com']
10
10
  gem.description = 'Just an redis cache.'
@@ -13,11 +13,11 @@ Gem::Specification.new do |gem|
13
13
  gem.license = 'MIT'
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
16
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ['lib']
19
19
 
20
20
  gem.add_dependency 'redis-namespace'
21
21
 
22
- %w(rails minitest-rails).each{ |g| gem.add_development_dependency g }
22
+ %w(rails minitest-rails).each { |g| gem.add_development_dependency g }
23
23
  end
@@ -15,15 +15,32 @@ module ActiveSupport
15
15
  @data.keys key
16
16
  end
17
17
 
18
- def fetch(key, options = nil, &block)
18
+ def fetch(key, options = {}, &block)
19
19
  key = expanded_key key
20
20
 
21
+ if options.key?(:force)
22
+ result = options[:force].is_a?(Proc) ? options[:force].call(key, options) : options[:force]
23
+ if result
24
+ fetch_raw key, options do
25
+ yield
26
+ end
27
+ else
28
+ write key, yield, options
29
+ end
30
+ else
31
+ fetch_raw(key, options) do
32
+ yield
33
+ end
34
+ end
35
+ end
36
+
37
+ def fetch_raw(key, options = {}, &block)
21
38
  instrument :fetch, key, options do
22
39
  exist?(key) ? read(key, options) : write(key, block, options)
23
40
  end
24
41
  end
25
42
 
26
- def read(key, options = nil)
43
+ def read(key, options = {})
27
44
  key = expanded_key key
28
45
  return nil if key.start_with?('http')
29
46
  instrument :read, key, options do
@@ -31,12 +48,12 @@ module ActiveSupport
31
48
  end
32
49
  end
33
50
 
34
- def read_raw(key, _options = nil)
51
+ def read_raw(key, _options = {})
35
52
  key = expanded_key key
36
53
  @data.get key
37
54
  end
38
55
 
39
- def write(key, entry, options = nil)
56
+ def write(key, entry, options = {})
40
57
  key = expanded_key key
41
58
  return false if key.start_with?('http')
42
59
 
@@ -47,12 +64,13 @@ module ActiveSupport
47
64
  nil
48
65
  else
49
66
  @data.set key, entry, options
67
+ @data.set "#{key}_updated_at", Time.now.to_i if options[:updated_at]
50
68
  load_entry entry
51
69
  end
52
70
  end
53
71
  end
54
72
 
55
- def delete(key, options = nil)
73
+ def delete(key, options = {})
56
74
  key = expanded_key key
57
75
 
58
76
  instrument :delete, key, options do
@@ -61,7 +79,7 @@ module ActiveSupport
61
79
  end
62
80
  end
63
81
 
64
- def exist?(key, _options = nil)
82
+ def exist?(key, _options = {})
65
83
  key = expanded_key key
66
84
  @data.exists key
67
85
  end
@@ -77,7 +95,7 @@ module ActiveSupport
77
95
  @data.info
78
96
  end
79
97
 
80
- def increment(key, amount = 1, _options = nil)
98
+ def increment(key, amount = 1, _options = {})
81
99
  key = expanded_key key
82
100
 
83
101
  instrument :increment, key, amount do
@@ -89,7 +107,7 @@ module ActiveSupport
89
107
  end
90
108
  end
91
109
 
92
- def decrement(key, amount = 1, _options = nil)
110
+ def decrement(key, amount = 1, _options = {})
93
111
  key = expanded_key key
94
112
 
95
113
  instrument :decrement, key, amount do
@@ -107,13 +125,13 @@ module ActiveSupport
107
125
 
108
126
  private
109
127
 
110
- def instrument(operation, key, options = nil)
128
+ def instrument(operation, key, options = {})
111
129
  payload = { key: key }
112
130
  payload.merge!(options) if options.is_a?(Hash)
113
131
  ActiveSupport::Notifications.instrument("cache_#{operation}.active_support", payload) { yield(payload) }
114
132
  end
115
133
 
116
- def log(operation, key, options = nil)
134
+ def log(operation, key, options = {})
117
135
  return unless logger && logger.debug? && !silence?
118
136
  logger.debug(" \e[95mCACHE #{operation}\e[0m #{key}#{options.blank? ? "" : " (#{options.inspect})"}")
119
137
  end
@@ -6,8 +6,8 @@ require 'action_dispatch/middleware/session/h2ocube_rails_cache_session'
6
6
  class Redis
7
7
  class Store < self
8
8
  def set(key, value, options = nil)
9
- if options && ttl = options[:expire_after] || options[:expires_in] || options[:expire_in] || nil
10
- setex(key, ttl.to_i, value)
9
+ if options && options[:expires_in]
10
+ setex(key, options[:expires_in].to_i, value)
11
11
  else
12
12
  super(key, value)
13
13
  end
data/test/cache_test.rb CHANGED
@@ -111,11 +111,48 @@ describe 'h2ocube_rails_cache' do
111
111
 
112
112
  Rails.cache.exist?('fetch error').must_be_same_as false
113
113
  end
114
+
115
+ it 'fetch with force' do
116
+ Rails.cache.write 'fetch force', 'content'
117
+
118
+ Rails.cache.fetch 'fetch force', force: true do
119
+ 'true'
120
+ end.must_equal 'content'
121
+
122
+ Rails.cache.fetch 'fetch force', force: -> (key, options) { true } do
123
+ 'true'
124
+ end.must_equal 'content'
125
+
126
+ Rails.cache.fetch 'fetch force', force: false do
127
+ 'false'
128
+ end.must_equal 'false'
129
+
130
+ Rails.cache.fetch 'fetch force', force: -> (key, options) { false } do
131
+ 'false again'
132
+ end.must_equal 'false again'
133
+ end
134
+
135
+ it 'fetch with updated_at' do
136
+ Rails.cache.fetch 'fetch updated_at', updated_at: true do
137
+ 'content'
138
+ end.must_equal 'content'
139
+
140
+ Rails.cache.exist?('fetch updated_at_updated_at').must_be_same_as true
141
+
142
+ sleep 1
143
+
144
+ now = Time.now.to_i
145
+ Rails.cache.fetch 'fetch updated_at', updated_at: true, force: -> (key, options) { now < Rails.cache.read("#{key}_updated_at") } do
146
+ 'new content'
147
+ end.must_equal 'new content'
148
+
149
+ Rails.cache.read('fetch updated_at_updated_at').must_equal now
150
+ end
114
151
  end
115
152
 
116
153
  describe ApplicationController do
117
154
  it 'get home' do
118
- get :home
155
+ get '/'
119
156
  assert_response :success
120
157
  end
121
158
  end
@@ -1,3 +1,3 @@
1
1
  Dummy::Application.routes.draw do
2
- root to: 'application#home'
2
+ get '/' => 'application#home'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: h2ocube_rails_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben