h2ocube_rails_cache 0.2.1 → 0.3.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.
- checksums.yaml +4 -4
- data/README.md +2 -1
- data/h2ocube_rails_cache.gemspec +1 -1
- data/lib/active_support/cache/h2ocube_rails_cache.rb +36 -35
- data/test/cache_test.rb +10 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2cfe66da39df5c16bf3aef04e9e33b11ad5e1518588916515965f67973ccd4f
|
4
|
+
data.tar.gz: 24d05e2c7afcbc6ca316bbb51028b172f9874de56c1cdccf068c3adce310e489
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 462463ed9d937cffa39fb84b04de25c7674ae5d3b7acab81bb7fc7c8f5e0471f3b652d51b24bbfd3c927628a6af4d4346651fe389bdc5e0e10ae975e45a5896a
|
7
|
+
data.tar.gz: aaf774d66b1203f2db36422c56b626b14c9f770645f0171f31cbf10d36fe8e77205dfd2d41b8a5bdc7d6043997cac5e7019c8dcfc733030fb80b3a112610d902
|
data/README.md
CHANGED
@@ -32,11 +32,12 @@ And then execute:
|
|
32
32
|
|
33
33
|
## Write Options
|
34
34
|
|
35
|
+
* `expires_in` such as 5.minutes, when value is nil, will not be expired
|
35
36
|
* `updated_at` will write timestamp with key_updated_at
|
36
37
|
|
37
38
|
## Fetch Options
|
38
39
|
|
39
|
-
* `expires_in` such as 5.minutes
|
40
|
+
* `expires_in` such as 5.minutes, when value is nil, will not be expired
|
40
41
|
* `force` true / false or Proc that return true / false
|
41
42
|
* `updated_at` will write timestamp with key_updated_at
|
42
43
|
|
data/h2ocube_rails_cache.gemspec
CHANGED
@@ -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.
|
7
|
+
gem.version = '0.3.0'
|
8
8
|
gem.authors = ['Ben']
|
9
9
|
gem.email = ['ben@h2ocube.com']
|
10
10
|
gem.description = 'Just an redis cache.'
|
@@ -8,19 +8,18 @@ module ActiveSupport
|
|
8
8
|
def initialize(options = {})
|
9
9
|
options ||= {}
|
10
10
|
@config = options
|
11
|
+
@namespace = options[:namespace]
|
11
12
|
@data = Redis.new(options)
|
12
13
|
super(options)
|
13
14
|
end
|
14
15
|
|
15
16
|
def keys(key = '*')
|
16
|
-
|
17
|
-
key = normalize_key key, config
|
17
|
+
key = normalize_key key
|
18
18
|
@data.keys key
|
19
19
|
end
|
20
20
|
|
21
21
|
def fetch(key, options = {}, &block)
|
22
|
-
|
23
|
-
key = normalize_key(key, options)
|
22
|
+
key = normalize_key key
|
24
23
|
|
25
24
|
if @data.exists(key)
|
26
25
|
if options.key?(:force)
|
@@ -43,8 +42,7 @@ module ActiveSupport
|
|
43
42
|
end
|
44
43
|
|
45
44
|
def fetch_raw(key, options = {}, &block)
|
46
|
-
|
47
|
-
key = normalize_key key, options
|
45
|
+
key = normalize_key key
|
48
46
|
instrument :fetch, key, options do
|
49
47
|
exist?(key) ? read(key) : write(key, block, options)
|
50
48
|
end
|
@@ -52,7 +50,7 @@ module ActiveSupport
|
|
52
50
|
|
53
51
|
def read(key, options = {})
|
54
52
|
options.reverse_merge! config
|
55
|
-
key = normalize_key key
|
53
|
+
key = normalize_key key
|
56
54
|
return nil if key.start_with?('http')
|
57
55
|
instrument :read, key, options do
|
58
56
|
exist?(key) ? load_entry(@data.get(key)) : nil
|
@@ -61,7 +59,7 @@ module ActiveSupport
|
|
61
59
|
|
62
60
|
def read_raw(key, options = {})
|
63
61
|
options.reverse_merge! config
|
64
|
-
key = normalize_key key
|
62
|
+
key = normalize_key key
|
65
63
|
@data.get key
|
66
64
|
end
|
67
65
|
|
@@ -77,9 +75,9 @@ module ActiveSupport
|
|
77
75
|
results
|
78
76
|
end
|
79
77
|
|
80
|
-
def write(key, entry,
|
81
|
-
options.reverse_merge
|
82
|
-
key = normalize_key
|
78
|
+
def write(key, entry, opts = {})
|
79
|
+
options = opts.reverse_merge config
|
80
|
+
key = normalize_key key
|
83
81
|
|
84
82
|
return false if key.start_with?('http')
|
85
83
|
|
@@ -89,9 +87,13 @@ module ActiveSupport
|
|
89
87
|
Rails.logger.warn "CacheWarn: '#{key}' is not cacheable!"
|
90
88
|
nil
|
91
89
|
else
|
92
|
-
expires_in
|
93
|
-
|
94
|
-
|
90
|
+
if opts.key?(:expires_in) && opts[:expires_in].nil?
|
91
|
+
@data.set key, entry
|
92
|
+
else
|
93
|
+
expires_in = options[:expires_in].to_i
|
94
|
+
@data.setex key, expires_in, entry
|
95
|
+
@data.setex "#{key}_updated_at", expires_in, Time.now.to_i if options[:updated_at]
|
96
|
+
end
|
95
97
|
load_entry entry
|
96
98
|
end
|
97
99
|
end
|
@@ -99,7 +101,7 @@ module ActiveSupport
|
|
99
101
|
|
100
102
|
def delete(key, options = {})
|
101
103
|
options.reverse_merge! config
|
102
|
-
key = normalize_key key
|
104
|
+
key = normalize_key key
|
103
105
|
|
104
106
|
instrument :delete, key, options do
|
105
107
|
@data.keys(key).each { |k| @data.del k }
|
@@ -107,9 +109,8 @@ module ActiveSupport
|
|
107
109
|
end
|
108
110
|
end
|
109
111
|
|
110
|
-
def exist?(key
|
111
|
-
|
112
|
-
key = normalize_key key, options
|
112
|
+
def exist?(key)
|
113
|
+
key = normalize_key key
|
113
114
|
@data.exists key
|
114
115
|
end
|
115
116
|
|
@@ -126,7 +127,7 @@ module ActiveSupport
|
|
126
127
|
|
127
128
|
def expire(key, expires_in)
|
128
129
|
options.reverse_merge! config
|
129
|
-
key = normalize_key key
|
130
|
+
key = normalize_key key
|
130
131
|
|
131
132
|
instrument :expire, key, expires_in: expires_in.to_i do
|
132
133
|
@data.expire key, expires_in.to_i
|
@@ -135,7 +136,7 @@ module ActiveSupport
|
|
135
136
|
|
136
137
|
def increment(key, amount = 1, options = {})
|
137
138
|
options.reverse_merge! config
|
138
|
-
key = normalize_key key
|
139
|
+
key = normalize_key key
|
139
140
|
|
140
141
|
instrument :increment, key, amount: amount do
|
141
142
|
if amount == 1
|
@@ -148,7 +149,7 @@ module ActiveSupport
|
|
148
149
|
|
149
150
|
def decrement(key, amount = 1, options = {})
|
150
151
|
options.reverse_merge! config
|
151
|
-
key = normalize_key key
|
152
|
+
key = normalize_key key
|
152
153
|
|
153
154
|
instrument :decrement, key, amount: amount do
|
154
155
|
if amount == 1
|
@@ -165,24 +166,24 @@ module ActiveSupport
|
|
165
166
|
|
166
167
|
private
|
167
168
|
|
168
|
-
def normalize_key(key
|
169
|
+
def normalize_key(key)
|
169
170
|
key = expanded_key(key)
|
170
|
-
|
171
|
-
prefix = namespace.is_a?(Proc) ? namespace.call : namespace
|
172
|
-
key = "#{prefix}:#{key}" if prefix && !key.start_with?(prefix)
|
171
|
+
key = "#{namespace}:#{key}" if !key.start_with?(namespace)
|
173
172
|
key
|
174
173
|
end
|
175
174
|
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
175
|
+
def instrument(operation, key, options = nil)
|
176
|
+
log { "Cache #{operation}: #{key}#{" (#{options.inspect})" unless options.blank?}" }
|
177
|
+
|
178
|
+
payload = { key: key }
|
179
|
+
payload.merge!(options) if options.is_a?(Hash)
|
180
|
+
ActiveSupport::Notifications.instrument("cache_#{operation}.active_support", payload) { yield(payload) }
|
181
|
+
end
|
182
|
+
|
183
|
+
def log
|
184
|
+
return unless logger && logger.debug? && !silence?
|
185
|
+
logger.debug(yield)
|
186
|
+
end
|
186
187
|
|
187
188
|
def dump_entry(entry)
|
188
189
|
entry = entry.call if entry.class.to_s == 'Proc'
|
data/test/cache_test.rb
CHANGED
@@ -40,6 +40,14 @@ describe 'h2ocube_rails_cache' do
|
|
40
40
|
Rails.cache.exist?('expire').must_be_same_as false
|
41
41
|
end
|
42
42
|
|
43
|
+
it '#write expire_in nil' do
|
44
|
+
Rails.cache.delete 'expire'
|
45
|
+
Rails.cache.write 'expire', true, expires_in: nil
|
46
|
+
Rails.cache.exist?('expire').must_be_same_as true
|
47
|
+
sleep 2
|
48
|
+
Rails.cache.exist?('expire').must_be_same_as true
|
49
|
+
end
|
50
|
+
|
43
51
|
it '#expire' do
|
44
52
|
Rails.cache.write 'expire', true
|
45
53
|
Rails.cache.expire 'expire', 1.second
|
@@ -119,7 +127,7 @@ describe 'h2ocube_rails_cache' do
|
|
119
127
|
'true'
|
120
128
|
end.must_equal 'true'
|
121
129
|
|
122
|
-
Rails.cache.fetch 'fetch force', force: -> (
|
130
|
+
Rails.cache.fetch 'fetch force', force: -> (_key, _options) { true } do
|
123
131
|
'true again'
|
124
132
|
end.must_equal 'true again'
|
125
133
|
|
@@ -127,7 +135,7 @@ describe 'h2ocube_rails_cache' do
|
|
127
135
|
'false'
|
128
136
|
end.must_equal 'true again'
|
129
137
|
|
130
|
-
Rails.cache.fetch 'fetch force', force: -> (
|
138
|
+
Rails.cache.fetch 'fetch force', force: -> (_key, _options) { false } do
|
131
139
|
'false again'
|
132
140
|
end.must_equal 'true again'
|
133
141
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: h2ocube_rails_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|