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 +4 -4
- data/CHANGELOG.md +13 -5
- data/Gemfile.lock +1 -1
- data/README.md +18 -4
- data/lib/ar_cache.rb +10 -10
- data/lib/ar_cache/active_record/associations/association.rb +1 -1
- data/lib/ar_cache/active_record/associations/has_one_through_association.rb +2 -1
- data/lib/ar_cache/active_record/associations/singular_association.rb +3 -1
- data/lib/ar_cache/active_record/connection_adapters/abstract/database_statements.rb +4 -4
- data/lib/ar_cache/active_record/core.rb +2 -4
- data/lib/ar_cache/active_record/persistence.rb +3 -3
- data/lib/ar_cache/active_record/relation.rb +5 -11
- data/lib/ar_cache/mock_table.rb +0 -8
- data/lib/ar_cache/query.rb +1 -1
- data/lib/ar_cache/record.rb +1 -3
- data/lib/ar_cache/table.rb +1 -9
- data/lib/ar_cache/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a01b3342ccad3bf06ce4438f714f00ffc467efc32f58809ab940163ebaf440d7
|
4
|
+
data.tar.gz: 453c9232cbc03dee8572b1133972f01d6dd1c2501df0c9279a32a5e4243ebeb0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
5
|
+
## 1.2.0 (2021-03-12)
|
6
6
|
|
7
|
-
|
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
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#
|
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
|
-
|
68
|
-
|
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!`, `#
|
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
|
27
|
-
Thread.current[:
|
26
|
+
def skip?
|
27
|
+
Thread.current[:ar_cache_skip]
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
31
|
-
Thread.current[:
|
30
|
+
def skip
|
31
|
+
Thread.current[:ar_cache_skip] = true
|
32
32
|
yield
|
33
33
|
ensure
|
34
|
-
Thread.current[:
|
34
|
+
Thread.current[:ar_cache_skip] = false
|
35
35
|
end
|
36
36
|
|
37
|
-
def
|
38
|
-
Thread.current[:
|
37
|
+
def expire?
|
38
|
+
Thread.current[:ar_cache_expire]
|
39
39
|
end
|
40
40
|
|
41
|
-
def
|
42
|
-
Thread.current[:
|
41
|
+
def expire
|
42
|
+
Thread.current[:ar_cache_expire] = true
|
43
43
|
yield
|
44
44
|
ensure
|
45
|
-
Thread.current[:
|
45
|
+
Thread.current[:ar_cache_expire] = false
|
46
46
|
end
|
47
47
|
end
|
48
48
|
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
|
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.
|
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)
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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
|
-
|
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
|
-
|
17
|
-
super
|
11
|
+
ArCache.skip { super }
|
18
12
|
end
|
19
13
|
|
20
14
|
def update_all(...)
|
21
|
-
ArCache.
|
15
|
+
ArCache.expire { delete_ar_cache_keys ? super : 0 }
|
22
16
|
end
|
23
17
|
|
24
18
|
def delete_all
|
25
|
-
ArCache.
|
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
|
-
|
40
|
+
ArCache.skip? ? super : ArCache::Query.new(self).exec_queries(&block).freeze
|
47
41
|
end
|
48
42
|
end
|
49
43
|
end
|
data/lib/ar_cache/mock_table.rb
CHANGED
data/lib/ar_cache/query.rb
CHANGED
@@ -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.
|
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
|
|
data/lib/ar_cache/record.rb
CHANGED
@@ -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)
|
data/lib/ar_cache/table.rb
CHANGED
@@ -43,21 +43,13 @@ module ArCache
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def disabled?
|
46
|
-
@disabled
|
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
|
data/lib/ar_cache/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2021-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|