incrdecr_cached_counts 0.0.4 → 0.0.5

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: 033e0da0b3b6191884b1ed3547029daf979e9af7
4
- data.tar.gz: b3e5b4e46ac655f2ab54b6b59ce57544b0b11177
3
+ metadata.gz: d751a2be8fc971ab8346677cf6af2417bd24c484
4
+ data.tar.gz: b05d9811217b500bc3e28fd15d0719837833c9cc
5
5
  SHA512:
6
- metadata.gz: 7d0a1cf6c691aa0f41d86ab72743575d5eb906943eba00295681e033a69d39c2f5bc5d9ab0c0f1865bb387a82d6016ffe2f442844876f8c7bb3375ac11fb665e
7
- data.tar.gz: 578b20bd19e73cd0df63d4fbbb99618f7bf7c343b85b049a9560be46ae8e493c746dc835710977440572fe792f6df72a1750fae61dd28fde207e8eaa4268ab65
6
+ metadata.gz: 1f42924de3ffae5cebb7c496a95a486a283126852c3d0c0a9239b158204aa38ba0191d8569b26266466f50f8ae200344ae4ab4e59db0c33b7b598bf80f1176a8
7
+ data.tar.gz: cf742a3a18c544245972b41e7187ab5d1f82374bc8d664387cbdf196e12447978f9e0aebb273849a73c0b0e011bb90936f55b57ad0773254ee75add302e8dd93
data/.gitignore CHANGED
@@ -9,3 +9,6 @@ test/dummy/db/*.sqlite3-journal
9
9
  test/dummy/log/*.log
10
10
  test/dummy/tmp/
11
11
  test/dummy/.sass-cache
12
+ tags
13
+ .yardoc/*
14
+ doc/*
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --no-private
data/README.rdoc CHANGED
@@ -35,12 +35,12 @@ Basic usage:
35
35
  class Following < ActiveRecord::Base
36
36
  include CachedCounts
37
37
 
38
- belongs_to :followee, class_name: 'User', # ...
38
+ belongs_to :followee, class_name: 'User' #, ...
39
39
  end
40
40
 
41
- Note that _CachedCounts_ must be included in the counted class, but no class methods need be called there.
41
+ Note that +CachedCounts+ must be included in the counted class, but no class methods need be called there.
42
42
 
43
- For full options, see docs for _CachedCounts.caches_count_of_ and _CachedCounts.caches_count_where_.
43
+ For full options, see docs for +CachedCounts.caches_count_of+ and +CachedCounts.caches_count_where+.
44
44
 
45
45
  = Licence
46
46
 
data/Rakefile CHANGED
@@ -4,12 +4,6 @@ rescue LoadError
4
4
  puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
5
  end
6
6
 
7
- require 'rdoc/task'
8
- RDoc::Task.new do |rdoc|
9
- rdoc.main = "README.rdoc"
10
- rdoc.rdoc_files.include("README.rdoc", "lib/**/*.rb")
11
- end
12
-
13
7
  require 'rspec/core/rake_task'
14
8
  RSpec::Core::RakeTask.new('spec')
15
9
 
data/lib/cached_counts.rb CHANGED
@@ -14,33 +14,37 @@ module CachedCounts
14
14
  # Automatically adds after_commit hooks which increment/decrement the value
15
15
  # in memcached when needed. Queries the db on cache miss.
16
16
  #
17
- # Valid options:
18
- # :scope
19
- # Name of the scope to count. Defaults to the attribute_name
20
- # (the required argument to `caches_count_where`).
17
+ # @param [String] attribute_name
21
18
  #
22
- # :alias
23
- # Alias(es) for the count attribute.
24
- # e.g. `caches_count_where :confirmed, alias: 'sitemap'`
19
+ # @param [Hash] options
20
+ #
21
+ # @option options [String] :scope
22
+ # Name of the scope to count. Defaults to the +attribute_name+
23
+ # (the required argument to +caches_count_where+).
24
+ #
25
+ # @option options [String, Array<String>] :alias
26
+ # Alias(es) for the count attribute.
27
+ # e.g.
28
+ # caches_count_where :confirmed, alias: 'sitemap'
25
29
  # > User.sitemap_count
26
30
  #
27
- # :expires_in
28
- # Expiry for the cached value.
31
+ # @option options [Integer] :expires_in
32
+ # Expiry for the cached value.
29
33
  #
30
- # :if
31
- # proc passed through to the after_commit hooks;
32
- # decides whether an object counts towards the association total.
34
+ # @option options [Proc] :if
35
+ # proc passed through to the after_commit hooks;
36
+ # decides whether an object counts towards the association total.
33
37
  #
34
- # :version
35
- # Cache version - bump if you change the definition of a count.
38
+ # @option options [Integer, #to_s] :version
39
+ # Cache version - bump if you change the definition of a count.
36
40
  #
37
- # :race_condition_fallback
38
- # Fallback to the result of this proc if the cache is empty, while
39
- # loading the actual value from the db. Works similarly to
40
- # race_condition_ttl but for empty caches rather than expired values.
41
- # Meant to prevent a thundering-herd scenario, if for example a
42
- # memcached instance goes away. Can be nil; defaults to using a value
43
- # grabbed from the cache or DB at startup.
41
+ # @option options [Proc] :race_condition_fallback
42
+ # Fallback to the result of this proc if the cache is empty, while
43
+ # loading the actual value from the db. Works similarly to
44
+ # +race_condition_ttl+ but for empty caches rather than expired values.
45
+ # Meant to prevent a thundering-herd scenario, if for example a
46
+ # memcached instance goes away. Can be nil; defaults to using a value
47
+ # grabbed from the cache or DB at startup.
44
48
  #
45
49
  def caches_count_where(attribute_name, options = {})
46
50
  # Delay actual run to work around circular dependencies
@@ -60,28 +64,32 @@ module CachedCounts
60
64
  # increment/decrement the value in memcached when needed. Queries the db
61
65
  # on cache miss.
62
66
  #
63
- # Valid options:
64
- # :association
65
- # Name of the association to count. Defaults to the attribute_name
66
- # (the required argument to `caches_count_of`).
67
+ # @param [String] attribute_name
68
+ #
69
+ # @param [Hash] options
67
70
  #
68
- # :alias
69
- # Alias(es) for the count attribute. Useful with join tables.
70
- # e.g. `caches_count_of :user_departments, alias: 'users'`
71
+ # @option options [Symbol] :association
72
+ # Name of the association to count. Defaults to the +attribute_name+
73
+ # (the required argument to +caches_count_of+).
74
+ #
75
+ # @option options [String, Array<String>] :alias
76
+ # Alias(es) for the count attribute. Useful with join tables.
77
+ # e.g.
78
+ # caches_count_of :user_departments, alias: 'users'
71
79
  # > Department.first.users_count
72
80
  #
73
- # :expires_in
74
- # Expiry for the cached value.
81
+ # @option options [Integer] :expires_in
82
+ # Expiry for the cached value.
75
83
  #
76
- # :if
77
- # proc passed through to the after_commit hooks on the counted class;
78
- # decides whether an object counts towards the association total.
84
+ # @option options [Proc] :if
85
+ # proc passed through to the after_commit hooks on the counted class;
86
+ # decides whether an object counts towards the association total.
79
87
  #
80
- # :scope
81
- # proc used like an ActiveRecord scope on the counted class on cache misses.
88
+ # @option options [Proc] :scope
89
+ # proc used like an ActiveRecord scope on the counted class on cache misses.
82
90
  #
83
- # :version
84
- # Cache version - bump if you change the definition of a count.
91
+ # @option options [Integer, #to_s] :version
92
+ # Cache version - bump if you change the definition of a count.
85
93
  #
86
94
  def caches_count_of(attribute_name, options = {})
87
95
  # Delay actual run to work around circular dependencies
@@ -91,10 +99,12 @@ module CachedCounts
91
99
  end
92
100
  end
93
101
 
102
+ # @private
94
103
  def scope_count_key(attribute_name, version = 1)
95
104
  "#{name}:#{attribute_name}_count:#{version}"
96
105
  end
97
106
 
107
+ # @private
98
108
  def association_count_key(counter_id, attribute_name, version = 1)
99
109
  "#{name}:#{counter_id}:#{attribute_name}_count:#{version}" unless counter_id.nil?
100
110
  end
@@ -156,7 +166,7 @@ module CachedCounts
156
166
  fallback = 0
157
167
  end
158
168
 
159
- Rails.cache.write key, fallback, expires_in: options.fetch(:expires_in, 1.week)
169
+ Rails.cache.write key, fallback, expires_in: options.fetch(:expires_in, 1.week), raw: true
160
170
  end
161
171
 
162
172
  -> { fallback }
@@ -231,6 +241,7 @@ module CachedCounts
231
241
  # Ensure that other reads find something in the cache, but
232
242
  # continue calculating here because the default is likely inaccurate.
233
243
  fallback_value = instance_exec &race_condition_fallback
244
+ logger.warn "Setting #{fallback_value} as race_condition_fallback for #{send(key_method)}"
234
245
  Rails.cache.write(
235
246
  send(key_method),
236
247
  fallback_value.to_i,
@@ -385,6 +396,17 @@ module CachedCounts
385
396
  -> { send(association_name).spawn }
386
397
  end
387
398
  end
399
+
400
+ def logger
401
+ @logger ||= begin
402
+ if Rails.logger.nil?
403
+ require 'logger'
404
+ Logger.new($stderr)
405
+ else
406
+ Rails.logger
407
+ end
408
+ end
409
+ end
388
410
  end
389
411
  end
390
412
 
@@ -1,3 +1,3 @@
1
1
  module CachedCounts
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: incrdecr_cached_counts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Judd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-09 00:00:00.000000000 Z
11
+ date: 2016-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -133,6 +133,7 @@ files:
133
133
  - ".circleci-matrix.yml"
134
134
  - ".gitignore"
135
135
  - ".travis.yml"
136
+ - ".yardopts"
136
137
  - Gemfile
137
138
  - MIT-LICENSE
138
139
  - README.rdoc