h2ocube_rails_cache 0.0.9 → 0.0.10

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: 2731a7dce9f62d11312415d58be8ea9a3cf8222b
4
- data.tar.gz: 6dc223280dc7d0e37b312459641105825cde2fe9
3
+ metadata.gz: d8035a1f77233f7ff3329e9c709e58053fb7b5e5
4
+ data.tar.gz: 2278ba3f53b73bdfe300a77a3b937007949f3140
5
5
  SHA512:
6
- metadata.gz: beceb3d7ed1b330ae518f4cebddea7b4d000a742677ff84e2c6165a786c8ffb911b3fe786f62df64232edae8174c8bb9b6d7c177c85184269fe796b670b8281f
7
- data.tar.gz: 1c19a97d1175b585308ec00a9a87f6e65523a54f11bd4d711e3a36dc41fcb5bd98770c81bb58411edbef63709ec85bb18b0dd2b47cd2b0605810159c9589dc65
6
+ metadata.gz: 6ecad18d2d880acccbd9a3dfebb5d01df2e5a7c4d246a01775227157e254697b3a3317a532964cfdd6f4d1a6bb74a4a860996033ed35655f3357014619004617
7
+ data.tar.gz: 6cccea34813f6e1ffbc7e34bba2943a2857f364041665398c43451ee1d2f46e53545c2cfb0c1ad23a3af7a4aaa7d3cabbcd864440d19dc6e631beeeb06254f49
data/README.md CHANGED
@@ -30,6 +30,10 @@ Disable default session_store in config/initializers/session_store.rb
30
30
  * `clear`
31
31
  * `info`
32
32
 
33
+ ## Support Options
34
+
35
+ * `expires_in` # Rails.cache.write 'key', 'value', expires_in: 1.minute
36
+
33
37
  ## Task changed
34
38
 
35
39
  rake tmp:sessions:clear # will clear redis session data too
@@ -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.9'
7
+ gem.version = '0.0.10'
8
8
  gem.authors = ['Ben']
9
9
  gem.email = ['ben@h2ocube.com']
10
10
  gem.description = 'Just an redis cache.'
@@ -7,86 +7,97 @@ module ActiveSupport
7
7
  def initialize(options = nil, &blk)
8
8
  options ||= {}
9
9
  super(options)
10
- @data = Redis::Namespace.new("#{Rails.application.class.to_s.split("::").first}:#{Rails.env}:Cache", redis: Redis::Store.new)
10
+ @data = Redis::Namespace.new("#{Rails.application.class.to_s.split('::').first}:#{Rails.env}:Cache", redis: Redis::Store.new)
11
11
  end
12
12
 
13
- def keys key = '*'
13
+ def keys(key = '*')
14
14
  key = expanded_key key
15
15
  @data.keys key
16
16
  end
17
17
 
18
- def fetch key, options = nil, &blk
18
+ def fetch(key, options = nil, &block)
19
19
  key = expanded_key key
20
20
 
21
- return read(key, options) if exist?(key)
22
-
23
- write key, blk, options if block_given?
21
+ instrument :fetch, key, options do
22
+ exist?(key) ? read(key, options) : write(key, block, options)
23
+ end
24
24
  end
25
25
 
26
- def read key, options = nil
26
+ def read(key, options = nil)
27
27
  key = expanded_key key
28
28
  return nil if key.start_with?('http')
29
- if exist? key
30
- load_entry(@data.get key)
31
- else
32
- nil
29
+ instrument :read, key, options do
30
+ exist?(key) ? load_entry(@data.get(key)) : nil
33
31
  end
34
32
  end
35
33
 
36
- def read_raw key, options = nil
34
+ def read_raw(key, _options = nil)
37
35
  key = expanded_key key
38
36
  @data.get key
39
37
  end
40
38
 
41
-
42
- def write key, entry, options = nil
39
+ def write(key, entry, options = nil)
43
40
  key = expanded_key key
44
41
  return false if key.start_with?('http')
45
- entry = dump_entry entry
46
- if entry.nil?
47
- Rails.logger.warn "CacheWarn: '#{key}' is not cacheable!"
48
- nil
49
- else
50
- @data.set key, entry, options
51
- load_entry entry
42
+
43
+ instrument :write, key, options do
44
+ entry = dump_entry entry
45
+ if entry.nil?
46
+ Rails.logger.warn "CacheWarn: '#{key}' is not cacheable!"
47
+ nil
48
+ else
49
+ @data.set key, entry, options
50
+ load_entry entry
51
+ end
52
52
  end
53
53
  end
54
54
 
55
- def delete key, options = nil
55
+ def delete(key, options = nil)
56
56
  key = expanded_key key
57
- @data.keys(key).each{ |k| @data.del k }
58
- true
57
+
58
+ instrument :delete, key, options do
59
+ @data.keys(key).each { |k| @data.del k }
60
+ true
61
+ end
59
62
  end
60
63
 
61
- def exist? key, options = nil
64
+ def exist?(key, _options = nil)
62
65
  key = expanded_key key
63
66
  @data.exists key
64
67
  end
65
68
 
66
69
  def clear
67
- @data.keys('*').each{ |k| @data.del k }
68
- true
70
+ instrument :clear, nil, nil do
71
+ @data.keys('*').each { |k| @data.del k }
72
+ true
73
+ end
69
74
  end
70
75
 
71
76
  def info
72
77
  @data.info
73
78
  end
74
79
 
75
- def increment key, amount = 1, options = nil
80
+ def increment(key, amount = 1, _options = nil)
76
81
  key = expanded_key key
77
- if amount == 1
78
- @data.incr key
79
- else
80
- @data.incrby key, amount
82
+
83
+ instrument :increment, key, amount do
84
+ if amount == 1
85
+ @data.incr key
86
+ else
87
+ @data.incrby key, amount
88
+ end
81
89
  end
82
90
  end
83
91
 
84
- def decrement key, amount = 1, options = nil
92
+ def decrement(key, amount = 1, _options = nil)
85
93
  key = expanded_key key
86
- if amount == 1
87
- @data.decr key
88
- else
89
- @data.decrby key, amount
94
+
95
+ instrument :decrement, key, amount do
96
+ if amount == 1
97
+ @data.decr key
98
+ else
99
+ @data.decrby key, amount
100
+ end
90
101
  end
91
102
  end
92
103
 
@@ -96,7 +107,18 @@ module ActiveSupport
96
107
 
97
108
  private
98
109
 
99
- def dump_entry entry
110
+ def instrument(operation, key, options = nil)
111
+ payload = { key: key }
112
+ payload.merge!(options) if options.is_a?(Hash)
113
+ ActiveSupport::Notifications.instrument("cache_#{operation}.active_support", payload) { yield(payload) }
114
+ end
115
+
116
+ def log(operation, key, options = nil)
117
+ return unless logger && logger.debug? && !silence?
118
+ logger.debug(" \e[95mCACHE #{operation}\e[0m #{key}#{options.blank? ? "" : " (#{options.inspect})"}")
119
+ end
120
+
121
+ def dump_entry(entry)
100
122
  entry = entry.call if entry.class.to_s == 'Proc'
101
123
 
102
124
  case entry.class.to_s
@@ -112,7 +134,7 @@ module ActiveSupport
112
134
  end
113
135
  end
114
136
 
115
- def load_entry entry
137
+ def load_entry(entry)
116
138
  begin
117
139
  Marshal.load entry
118
140
  rescue
@@ -22,6 +22,14 @@ module H2ocubeRailsCache
22
22
  app.config.session_store :h2ocube_rails_cache_session
23
23
  end
24
24
 
25
+ config.after_initialize do
26
+ Rails.cache.logger = Rails.logger
27
+
28
+ ActiveSupport::Notifications.subscribe(/cache_[^.]+.active_support/) do |name, start, finish, id, payload|
29
+ Rails.cache.logger.debug " \e[95mCACHE #{name} (#{((finish - start) * 1000).round 2}ms)\e[0m #{payload[:key]} (#{payload[:options].inspect})"
30
+ end if Rails.cache.logger.debug? && !Rails.cache.silence?
31
+ end
32
+
25
33
  rake_tasks do
26
34
  load 'tasks/tmp.rake'
27
35
  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.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-29 00:00:00.000000000 Z
11
+ date: 2016-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis-namespace
@@ -111,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  version: '0'
112
112
  requirements: []
113
113
  rubyforge_project:
114
- rubygems_version: 2.2.1
114
+ rubygems_version: 2.6.4
115
115
  signing_key:
116
116
  specification_version: 4
117
117
  summary: Just an redis cache.