cache_key_for 0.1.9 → 0.1.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: 0019a0a8e3f7265702688e55c5b340a129fcbde0
4
- data.tar.gz: 5e2715145b399c3e161011e434d648b52d7594ff
3
+ metadata.gz: a75d030a404b2048b534fb95b9a34bff92bec378
4
+ data.tar.gz: 54d450d3ced6c78baadd1fc9a9c31e4d53bc9ce0
5
5
  SHA512:
6
- metadata.gz: 97d765ebee9045c4f5df6ec07990d32cd7b4bf5e85b843833058656cf07b2bd6d20fb41b55ad37491deb1c15117eb9c5a2aada1b5bf6eb0ba590eb5d2c0dd218
7
- data.tar.gz: 94e3e4fd81d0e0cd516a7a0d054a4d595a5cd0f77698a766b6981c5b08c90acbcf453dd5657742ff70cd70c3b374d6beda5f2ea884dcf409303570797aaf87c2
6
+ metadata.gz: 5a7e08df937897ff5aac575f39296b52e7496fed1fecf0b90f810487bd9953513e875e25f92ea338d116540bb90a7eb36723210c2f2c98a9e15ac7e8b2bcf01f
7
+ data.tar.gz: 86c4a91d17463bac1c8dc9e809d16e62204618278eb2b23abf0c7433573817e77aafc0e07d50e95e98d06e74e3b80b3b55f2d89a6e75ee81955af5e51be8351f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cache_key_for (0.1.9)
4
+ cache_key_for (0.1.10)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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
- if scoped_collection.respond_to?(:maximum) # ActiveRecord
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
- max_updated_at = scoped_collection.maximum(scoped_collection.table_name + '.updated_at').to_f
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
- max_updated_at = scoped_collection.to_a.map { |i| i.updated_at ? i.updated_at.utc.to_f : 0 }.max
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.class == Array
56
- max_updated_at = scoped_collection.to_a.map { |i| i.updated_at ? i.updated_at.utc.to_f : 0 }.max
57
- elsif scoped_collection.respond_to?(:max) # Mongoid
58
- max_updated_at = scoped_collection.max(:updated_at).to_f
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
- request_params = if request.params
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.to_s) }
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.to_s) }
72
- end.map do |k, v|
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
- end
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}-#{request_params}")
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
- # puts "generated cache key digest base: #{ids_string}-#{max_updated_at}-#{count}-#{request.subdomains.join('.')}-#{request.path}-#{request_params}"
82
- # puts "generated cache key: #{I18n.locale}/#{collection_prefix}/#{digest}/#{cache_owner_cache_key}/#{suffix}"
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
@@ -1,3 +1,3 @@
1
1
  module CacheKeyFor
2
- VERSION = '0.1.9'.freeze
2
+ VERSION = '0.1.10'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_key_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Kalita