ar_cache 1.1.0 → 1.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '09eeda930c7263476f3b09e53747f9ec87ac070e65f18031856cdb05ad57a083'
4
- data.tar.gz: 425c321debdcc05d81d4c0815abecb0d9e19fa417bb251265ba6dab420dd03e1
3
+ metadata.gz: a01b3342ccad3bf06ce4438f714f00ffc467efc32f58809ab940163ebaf440d7
4
+ data.tar.gz: 453c9232cbc03dee8572b1133972f01d6dd1c2501df0c9279a32a5e4243ebeb0
5
5
  SHA512:
6
- metadata.gz: 827f0ecdf9e583870a454c16cfaa44c95b9d7b0ba4f3942097daf1c76fed218adfdafcd5733b68fa6476297c8b6d35c728e1a1b6f66db615d9fde834183ea4e8
7
- data.tar.gz: 273bef3daea68391d681045c384488bf9266a1f75cd5ea7057b6aaf70928ebe97934cb65daadf3f6a3442177d948923116867be503f479c6ac8ab06ac23bb286
6
+ metadata.gz: 7d6a3bb90d4ac0eb3384650fd17fbed43be74da2ca0a57b55c9d19b9c68722b2d23785fd42e00cbdec8a4563cc10c6c44ac7609a8b4a09d9b4a813f47c3f2ae1
7
+ data.tar.gz: 2105bf0e6e89c3d438f30b43bef1dd64b66b511968fec5c393068043ddb52ee833762f0508a4a84d25940ddac36c686c8b5fd4e6ef73d75802de878645ac265b
data/CHANGELOG.md CHANGED
@@ -2,12 +2,20 @@
2
2
 
3
3
  ## main
4
4
 
5
- ## 1.0.0 (2021-03-02)
5
+ ## 1.2.0 (2021-03-12)
6
6
 
7
- - Initial version.
7
+ [Commit [#c830907](https://github.com/OuYangJinTing/ar_cache/commit/c830907595b7d1d46a2f29204ee6051ecc3ff30c)]:
8
+
9
+ - Remove methods: `ArCache::MockTable#enabled?`, `ArCache::MockTable#select_enabled?`, `ArCache::Table.enabled?`, `ArCache::Table.select_enabled?`, `ActiveRecord::Relation#skip_ar_cache`.
10
+ - Rename methods: `ArCache#skip_cache? => ArCache#skip?`, `ArCache#skip_cache => ArCache#skip`, `ArCache#pre_expire? => ArCache#expire?`, `ArCache#pre_expire => ArCache#expire`.
11
+ - Now, `ArCache#skip` method only skip read cache, but still try delete cache.
8
12
 
9
13
  ## 1.1.0 (2021-03-11)
10
14
 
11
- - Fully automatic delete cache when call delete_all/update_all method.
12
- - Optimize has_one(through:) cache implementation.
13
- - ActiveRecord::Relation#reload and ActiveRecord::Associations::Association#reload should skip read cache if associated target is already loaded.
15
+ - [Commit [#92965d2](https://github.com/OuYangJinTing/ar_cache/commit/92965d26e130da9a13bd52ea31f3f668851f6f12)] Fully automatic delete cache when call delete_all/update_all method.
16
+ - [Commit [#231cfd3](https://github.com/OuYangJinTing/ar_cache/commit/231cfd35c2c197bf41628f4f914ba39fb8debd81)] Optimize has_one(through:) cache implementation.
17
+ - [Commit [#ce5444c](https://github.com/OuYangJinTing/ar_cache/commit/ce5444c8c4ec0a61bec5e07d694295d3cc5decf8)] ActiveRecord::Relation#reload and ActiveRecord::Associations::Association#reload should skip read cache if associated target is already loaded.
18
+
19
+ ## 1.0.0 (2021-03-02)
20
+
21
+ - Initial version.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ar_cache (1.1.0)
4
+ ar_cache (1.2.0)
5
5
  activerecord (>= 6.1, < 7)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -55,17 +55,31 @@ rake db:migrate
55
55
 
56
56
  Skip cache:
57
57
 
58
+ - `ArCache#skip`, eg:
59
+
60
+ ```ruby
61
+ # All queries in the block will not use the cache.
62
+ ArCache.skip { User.find(1) }
63
+ ```
64
+
58
65
  - `ActiveRecord::Persistence#reload`, eg:
59
66
 
60
67
  ```ruby
61
68
  User.find(1).reload
62
69
  ```
63
70
 
64
- - `ActiveRecord::Relation#skip_ar_cache`, eg:
71
+ - `ActiveRecord::Relation#reload`, eg:
72
+
73
+ ```ruby
74
+ # When reload is called after the associated target has been loaded, the cache will be skipped.
75
+ User.where(id: 1).load.reload
76
+ ```
77
+
78
+ - `ActiveRecord::Associations::Association#reload`, eg:
65
79
 
66
80
  ```ruby
67
- User.skip_ar_cache.find(1)
68
- User.where(id: [1, 2]).skip_ar_cache.load
81
+ # When reload is called after the associated target has been loaded, the cache will be skipped.
82
+ user.association(:account).load_target.reload
69
83
  ```
70
84
 
71
85
  Delete cache:
@@ -91,7 +105,7 @@ If all the following conditions are met, ArCache will try to read the cache:
91
105
  - No call `#select` or select value is table column.
92
106
  - No call `#order` or order value is table column and only one.
93
107
  - No call `#limit` or value of the unique index isn't array.
94
- - No call `#joins`, `#left_joins`, `#skip_query_cache!`, `#skip_ar_cache`, `#explain`, `#from`, `#group`, `#offset`, `#lock`
108
+ - No call `#joins`, `#left_joins`, `#skip_query_cache!`, `#explain`, `#from`, `#group`, `#offset`, `#lock`
95
109
  - ...
96
110
 
97
111
  **Cacheable example:**
data/lib/ar_cache.rb CHANGED
@@ -23,26 +23,26 @@ module ArCache
23
23
  class << self
24
24
  delegate :configure, to: Configuration
25
25
 
26
- def skip_cache?
27
- Thread.current[:ar_cache_skip_cache]
26
+ def skip?
27
+ Thread.current[:ar_cache_skip]
28
28
  end
29
29
 
30
- def skip_cache
31
- Thread.current[:ar_cache_skip_cache] = true
30
+ def skip
31
+ Thread.current[:ar_cache_skip] = true
32
32
  yield
33
33
  ensure
34
- Thread.current[:ar_cache_skip_cache] = false
34
+ Thread.current[:ar_cache_skip] = false
35
35
  end
36
36
 
37
- def pre_expire?
38
- Thread.current[:ar_cache_pre_expire]
37
+ def expire?
38
+ Thread.current[:ar_cache_expire]
39
39
  end
40
40
 
41
- def pre_expire
42
- Thread.current[:ar_cache_pre_expire] = true
41
+ def expire
42
+ Thread.current[:ar_cache_expire] = true
43
43
  yield
44
44
  ensure
45
- Thread.current[:ar_cache_pre_expire] = false
45
+ Thread.current[:ar_cache_expire] = false
46
46
  end
47
47
  end
48
48
  end
@@ -5,7 +5,7 @@ module ArCache
5
5
  module Associations
6
6
  module Association
7
7
  def reload(...)
8
- loaded? ? ArCache.skip_cache { super } : super
8
+ loaded? ? ArCache.skip { super } : super
9
9
  end
10
10
  end
11
11
  end
@@ -4,7 +4,8 @@ module ArCache
4
4
  module ActiveRecord
5
5
  module Associations
6
6
  module HasOneThroughAssociation
7
- private def find_target
7
+ private def find_target # rubocop:disable Metrics/CyclomaticComplexity
8
+ return super if ArCache.skip?
8
9
  return super if reflection.klass.ar_cache_table.disabled?
9
10
  return super if reflection.through_reflection.klass.ar_cache_table.disabled?
10
11
 
@@ -5,10 +5,12 @@ module ArCache
5
5
  module Associations
6
6
  module SingularAssociation
7
7
  private def skip_statement_cache?(...)
8
+ return super if ArCache.skip?
9
+
8
10
  # Polymorphic associations do not support computing the class, so can't judge ArCache status.
9
11
  # But SingularAssociation query usually can hit the unique index, so here return true directly.
10
12
  return true if is_a?(::ActiveRecord::Associations::BelongsToPolymorphicAssociation)
11
- return true if reflection.klass.ar_cache_table.enabled?
13
+ return true unless reflection.klass.ar_cache_table.disabled?
12
14
 
13
15
  super
14
16
  end
@@ -46,24 +46,24 @@ module ArCache
46
46
  end
47
47
 
48
48
  private def update_ar_cache_by_arel(arel)
49
- return if ArCache.pre_expire?
49
+ return if ArCache.expire?
50
50
 
51
51
  arel_table = arel.ast.relation.is_a?(Arel::Table) ? arel.ast.relation : arel.ast.relation.left
52
52
  klass = arel_table.instance_variable_get(:@klass)
53
- current_transaction.update_ar_cache_table(klass.ar_cache_table) if klass.ar_cache_table.enabled?
53
+ current_transaction.update_ar_cache_table(klass.ar_cache_table) unless klass.ar_cache_table.disabled?
54
54
  end
55
55
 
56
56
  private def update_ar_cache_by_sql(sql)
57
57
  sql = sql.downcase
58
58
 
59
59
  ArCache::Table.all.each do |table|
60
- current_transaction.update_ar_cache_table(table) if table.enabled? && sql.include?(table.name)
60
+ current_transaction.update_ar_cache_table(table) if !table.disabled? && sql.include?(table.name)
61
61
  end
62
62
  end
63
63
 
64
64
  private def update_ar_cache_by_table(table_name)
65
65
  ArCache::Table.all.each do |table|
66
- current_transaction.update_ar_cache_table(table) if table.enabled? && table_name.casecmp?(table.name)
66
+ current_transaction.update_ar_cache_table(table) if !table.disabled? && table_name.casecmp?(table.name)
67
67
  end
68
68
  end
69
69
  end
@@ -4,16 +4,14 @@ module ArCache
4
4
  module ActiveRecord
5
5
  module Core
6
6
  module ClassMethods
7
- delegate :skip_ar_cache, to: :all
8
-
9
7
  # The #find use statement cache execute querying first, so need force skip.
10
8
  def find(...)
11
- ar_cache_table.enabled? ? all.find(...) : super
9
+ ArCache.skip? || ar_cache_table.disabled? ? super : all.find(...)
12
10
  end
13
11
 
14
12
  # The #find_by use statement cache execute querying first, so need force skip.
15
13
  def find_by(...)
16
- ar_cache_table.enabled? ? all.find_by(...) : super
14
+ ArCache.skip? || ar_cache_table.disabled? ? super : all.find_by(...)
17
15
  end
18
16
  end
19
17
  end
@@ -5,14 +5,14 @@ module ArCache
5
5
  module Persistence
6
6
  module ClassMethods
7
7
  def _update_record(_, constraints)
8
- ArCache.pre_expire do
8
+ ArCache.expire do
9
9
  delete_ar_cache_key(constraints[@primary_key])
10
10
  super
11
11
  end
12
12
  end
13
13
 
14
14
  def _delete_record(constraints)
15
- ArCache.pre_expire do
15
+ ArCache.expire do
16
16
  delete_ar_cache_key(constraints[@primary_key])
17
17
  super
18
18
  end
@@ -26,7 +26,7 @@ module ArCache
26
26
  end
27
27
 
28
28
  def reload(...)
29
- ArCache.skip_cache { super }
29
+ ArCache.skip { super }
30
30
  end
31
31
  end
32
32
  end
@@ -4,25 +4,19 @@ module ArCache
4
4
  module ActiveRecord
5
5
  module Relation
6
6
  def reload
7
- @skip_ar_cache = true if loaded?
8
- super
9
- end
10
-
11
- def skip_ar_cache
12
- tap { @skip_ar_cache = true }
7
+ loaded? ? ArCache.skip { super } : super
13
8
  end
14
9
 
15
10
  def explain
16
- @skip_ar_cache = true
17
- super
11
+ ArCache.skip { super }
18
12
  end
19
13
 
20
14
  def update_all(...)
21
- ArCache.pre_expire { delete_ar_cache_keys ? super : 0 }
15
+ ArCache.expire { delete_ar_cache_keys ? super : 0 }
22
16
  end
23
17
 
24
18
  def delete_all
25
- ArCache.pre_expire { delete_ar_cache_keys ? super : 0 }
19
+ ArCache.expire { delete_ar_cache_keys ? super : 0 }
26
20
  end
27
21
 
28
22
  private def delete_ar_cache_keys
@@ -43,7 +37,7 @@ module ArCache
43
37
  end
44
38
 
45
39
  private def exec_queries(&block)
46
- @skip_ar_cache ? super : ArCache::Query.new(self).exec_queries(&block).freeze
40
+ ArCache.skip? ? super : ArCache::Query.new(self).exec_queries(&block).freeze
47
41
  end
48
42
  end
49
43
  end
@@ -7,18 +7,10 @@ module ArCache
7
7
  true
8
8
  end
9
9
 
10
- def enabled?
11
- false
12
- end
13
-
14
10
  def select_disabled?
15
11
  true
16
12
  end
17
13
 
18
- def select_enabled?
19
- false
20
- end
21
-
22
14
  def version
23
15
  -1
24
16
  end
@@ -12,7 +12,7 @@ module ArCache
12
12
 
13
13
  def exec_queries(&block) # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
14
14
  return [] if relation.where_clause.contradiction?
15
- return relation.skip_ar_cache.send(:exec_queries, &block) unless exec_queries_cacheable?
15
+ return ArCache.skip { relation.send(:exec_queries, &block) } unless exec_queries_cacheable?
16
16
 
17
17
  records = table.read(where_clause, @select_values, &block)
18
18
 
@@ -4,10 +4,8 @@ module ArCache
4
4
  class Record < ActiveRecord::Base # rubocop:disable Rails/ApplicationRecord
5
5
  self.table_name = 'ar_cache_records'
6
6
 
7
- default_scope { skip_ar_cache }
8
-
9
7
  def self.get(table_name)
10
- find_by(table_name: table_name)
8
+ ArCache.skip { find_by(table_name: table_name) }
11
9
  end
12
10
 
13
11
  def self.version(table)
@@ -43,21 +43,13 @@ module ArCache
43
43
  end
44
44
 
45
45
  def disabled?
46
- @disabled || ArCache.skip_cache?
47
- end
48
-
49
- def enabled?
50
- !disabled?
46
+ @disabled
51
47
  end
52
48
 
53
49
  def select_disabled?
54
50
  @select_disabled
55
51
  end
56
52
 
57
- def select_enabled?
58
- !@select_disabled
59
- end
60
-
61
53
  def version
62
54
  version = ArCache::Store.read(cache_key_prefix)
63
55
  unless version
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ArCache
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - OuYangJinTing
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-10 00:00:00.000000000 Z
11
+ date: 2021-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord