cache_key_for 0.1.9 → 0.1.10
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/Gemfile.lock +1 -1
- data/README.md +1 -0
- data/lib/cache_key_for/cache_key_for_helper.rb +25 -16
- data/lib/cache_key_for/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a75d030a404b2048b534fb95b9a34bff92bec378
|
4
|
+
data.tar.gz: 54d450d3ced6c78baadd1fc9a9c31e4d53bc9ce0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a7e08df937897ff5aac575f39296b52e7496fed1fecf0b90f810487bd9953513e875e25f92ea338d116540bb90a7eb36723210c2f2c98a9e15ac7e8b2bcf01f
|
7
|
+
data.tar.gz: 86c4a91d17463bac1c8dc9e809d16e62204618278eb2b23abf0c7433573817e77aafc0e07d50e95e98d06e74e3b80b3b55f2d89a6e75ee81955af5e51be8351f
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -21,6 +21,7 @@ Features:
|
|
21
21
|
* deletion from and addition to collections sorted in ascending order (via embedded `count` in the key)
|
22
22
|
* accepts `cache_owner_cache_key` for personalized cache, eg. current_company.cache_key, current_user.cache_key etc.
|
23
23
|
* filters params with proper non-utf8 data handling for key generation
|
24
|
+
* supports paginated and not paginated collections or arrays of objects
|
24
25
|
* recognizes pagination via params (performs well for less than 100 objects per page)
|
25
26
|
* allows to set default page and per_page or sort order or any param in `default_params` to avoid multiple different default caches
|
26
27
|
* includes all params, not only GET's `query` params, which enables submitting of complex forms via POST,
|
@@ -6,6 +6,7 @@
|
|
6
6
|
# * deletion from and addition to collections sorted in ascending order (via embedded `count` in the key)
|
7
7
|
# * accepts `cache_owner_cache_key` for personalized cache, eg. current_company.cache_key, current_user.cache_key etc.
|
8
8
|
# * filters params with proper non-utf8 data handling for key generation
|
9
|
+
# * supports paginated and not paginated collections or arrays of objects
|
9
10
|
# * recognizes pagination via params (performs well for less than 100 objects per page)
|
10
11
|
# * allows to set default page and per_page or sort order or any param in `default_params` to avoid multiple different default caches
|
11
12
|
# * includes all params, not only GET's `query` params, which enables submitting of complex forms via POST,
|
@@ -43,43 +44,51 @@
|
|
43
44
|
# ```
|
44
45
|
module CacheKeyForHelper
|
45
46
|
def cache_key_for(scoped_collection, collection_prefix, cache_owner_cache_key = '', suffix = '', whitelist_params = [], default_params = {})
|
46
|
-
|
47
|
+
# 1) paginated scope - `maximum/max` database query on page(2) does not work
|
48
|
+
# 2) Array doesn't respond to `total_pages`
|
49
|
+
max_updated_at = if scoped_collection.respond_to?(:total_pages) || scoped_collection.class == Array
|
50
|
+
scoped_collection.to_a.map { |i| i.updated_at ? i.updated_at.utc.to_f : 0 }.max
|
51
|
+
elsif scoped_collection.respond_to?(:maximum) # not paginated ActiveRecord::Relation
|
47
52
|
begin
|
48
|
-
|
53
|
+
scoped_collection.maximum(scoped_collection.table_name + '.updated_at').to_f
|
49
54
|
# can't use join table as query root if query includes polimorphic associations
|
50
55
|
rescue ActiveRecord::EagerLoadPolymorphicError
|
51
56
|
Rails.logger.debug "[CacheKeyForHelper] Fallback to array (ActiveRecord::EagerLoadPolymorphicError)"
|
52
57
|
scoped_collection = scoped_collection.to_a
|
53
|
-
|
58
|
+
scoped_collection.to_a.map { |i| i.updated_at ? i.updated_at.utc.to_f : 0 }.max
|
54
59
|
end
|
55
|
-
elsif scoped_collection.
|
56
|
-
|
57
|
-
|
58
|
-
|
60
|
+
elsif scoped_collection.respond_to?(:max) # not paginated Mongoid::Criteria
|
61
|
+
scoped_collection.max(:updated_at).to_f
|
62
|
+
end
|
63
|
+
count = if scoped_collection.respond_to?(:total_count) # kaminari
|
64
|
+
scoped_collection.total_count
|
65
|
+
elsif scoped_collection.respond_to?(:total_entries) # will_paginate
|
66
|
+
scoped_collection.total_entries
|
67
|
+
else # Array or not paginated scope
|
68
|
+
scoped_collection.count
|
59
69
|
end
|
60
|
-
count = scoped_collection.count
|
61
70
|
if scoped_collection.respond_to?(:ids)
|
62
71
|
ids_string = scoped_collection.ids
|
63
72
|
else
|
64
73
|
ids_string = scoped_collection.to_a.map(&:id).join('-')
|
65
74
|
end
|
66
75
|
blacklist_params = ['utm_source', 'utm_medium', 'utm_term', 'utm_content', 'utm_campaign']
|
67
|
-
|
76
|
+
flat_request_params = if request.params
|
68
77
|
if whitelist_params.empty?
|
69
|
-
default_params.merge(request.params).reject { |k, _v| blacklist_params.map(&:to_s).include?(k
|
78
|
+
default_params.stringify_keys.merge(request.params).reject { |k, _v| blacklist_params.map(&:to_s).include?(k) }
|
70
79
|
else
|
71
|
-
default_params.merge(request.params).select { |k, _v| whitelist_params.map(&:to_s).include?(k
|
72
|
-
end.map
|
80
|
+
default_params.stringify_keys.merge(request.params).select { |k, _v| whitelist_params.map(&:to_s).include?(k) }
|
81
|
+
end.map { |k, v|
|
73
82
|
# don't care about data type in the `v`, convert all to string
|
74
83
|
[k.to_s.dup.force_encoding('UTF-8'), v.to_s.dup.force_encoding('UTF-8')]
|
75
|
-
|
84
|
+
}.to_h
|
76
85
|
else
|
77
86
|
nil
|
78
87
|
end
|
79
|
-
digest = Digest::SHA1.hexdigest("#{ids_string}-#{max_updated_at}-#{count}-#{request.subdomains.join('.')}-#{request.path}-#{
|
88
|
+
digest = Digest::SHA1.hexdigest("#{ids_string}-#{max_updated_at}-#{count}-#{request.subdomains.join('.')}-#{request.path}-#{flat_request_params}")
|
80
89
|
# puts "Caller: #{caller.first}"
|
81
|
-
|
82
|
-
|
90
|
+
puts "generated cache key digest base: #{ids_string}-#{max_updated_at}-#{count}-#{request.subdomains.join('.')}-#{request.path}-#{flat_request_params}"
|
91
|
+
puts "generated cache key: #{I18n.locale}/#{collection_prefix}/#{digest}/#{cache_owner_cache_key}/#{suffix}"
|
83
92
|
"#{I18n.locale}/#{collection_prefix}/#{digest}/#{cache_owner_cache_key}/#{suffix}"
|
84
93
|
end
|
85
94
|
end
|