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 +4 -4
- data/.gitignore +3 -0
- data/.yardopts +1 -0
- data/README.rdoc +3 -3
- data/Rakefile +0 -6
- data/lib/cached_counts.rb +60 -38
- data/lib/cached_counts/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d751a2be8fc971ab8346677cf6af2417bd24c484
|
4
|
+
data.tar.gz: b05d9811217b500bc3e28fd15d0719837833c9cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f42924de3ffae5cebb7c496a95a486a283126852c3d0c0a9239b158204aa38ba0191d8569b26266466f50f8ae200344ae4ab4e59db0c33b7b598bf80f1176a8
|
7
|
+
data.tar.gz: cf742a3a18c544245972b41e7187ab5d1f82374bc8d664387cbdf196e12447978f9e0aebb273849a73c0b0e011bb90936f55b57ad0773254ee75add302e8dd93
|
data/.gitignore
CHANGED
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
|
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
|
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
|
-
#
|
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
|
-
#
|
23
|
-
#
|
24
|
-
#
|
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
|
-
#
|
28
|
-
#
|
31
|
+
# @option options [Integer] :expires_in
|
32
|
+
# Expiry for the cached value.
|
29
33
|
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
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
|
-
#
|
35
|
-
#
|
38
|
+
# @option options [Integer, #to_s] :version
|
39
|
+
# Cache version - bump if you change the definition of a count.
|
36
40
|
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
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
|
-
#
|
64
|
-
#
|
65
|
-
#
|
66
|
-
# (the required argument to `caches_count_of`).
|
67
|
+
# @param [String] attribute_name
|
68
|
+
#
|
69
|
+
# @param [Hash] options
|
67
70
|
#
|
68
|
-
#
|
69
|
-
#
|
70
|
-
#
|
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
|
-
#
|
74
|
-
#
|
81
|
+
# @option options [Integer] :expires_in
|
82
|
+
# Expiry for the cached value.
|
75
83
|
#
|
76
|
-
#
|
77
|
-
#
|
78
|
-
#
|
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
|
-
#
|
81
|
-
#
|
88
|
+
# @option options [Proc] :scope
|
89
|
+
# proc used like an ActiveRecord scope on the counted class on cache misses.
|
82
90
|
#
|
83
|
-
#
|
84
|
-
#
|
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
|
|
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
|
+
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-
|
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
|