support_table_cache 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +3 -0
- data/VERSION +1 -1
- data/lib/support_table_cache.rb +39 -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: f7874d2e1669bb98537be4230846cc44862ebce2df16201d319926607a478c4b
|
4
|
+
data.tar.gz: 6f9c7bd2737b23689f487bbee803a0ce7aa592a3bde8357dab98bcd8fafa5612
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f322815aac9b0074d75a148c9d6b05ddb2ecb044fdae5d80b80bfca1bc409b70820b754fbecc83ddfe368da7f01872fde0b2eec724c328d1077759b2587aec70
|
7
|
+
data.tar.gz: 14733e659226be252601d4705e92248ac8377b21b0e301b6cb4b621f56731aad173c2ad01febb64a51f7051690de31b83ecc36fe69cfa4545806aebe94b38c52
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
5
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
+
## 1.0.1
|
8
|
+
|
9
|
+
### Added
|
10
|
+
- Preserve scope on relations terminated with a `find_by`.
|
11
|
+
|
7
12
|
## 1.0.0
|
8
13
|
|
9
14
|
### Added
|
data/README.md
CHANGED
@@ -36,6 +36,9 @@ To use the gem, you need to include it in you models and then specify which attr
|
|
36
36
|
# Uses cache on a composite key
|
37
37
|
MyModel.find_by(group: "first", name: "One")
|
38
38
|
|
39
|
+
# Uses cache on a composite key with scoping
|
40
|
+
MyModel.where(group: "first").find_by(name: "One")
|
41
|
+
|
39
42
|
# Does not use cache since value is not defined as a cacheable key
|
40
43
|
MyModel.find_by(value: 1)
|
41
44
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1
|
data/lib/support_table_cache.rb
CHANGED
@@ -10,6 +10,10 @@ module SupportTableCache
|
|
10
10
|
class_attribute :support_table_cache_by_attributes, instance_accessor: false
|
11
11
|
class_attribute :support_table_cache_ttl, instance_accessor: false
|
12
12
|
|
13
|
+
unless ActiveRecord::Relation.include?(RelationOverride)
|
14
|
+
ActiveRecord::Relation.prepend(RelationOverride)
|
15
|
+
end
|
16
|
+
|
13
17
|
class << self
|
14
18
|
prepend FindByOverride unless include?(FindByOverride)
|
15
19
|
private :support_table_cache_by_attributes=
|
@@ -105,7 +109,12 @@ module SupportTableCache
|
|
105
109
|
|
106
110
|
cache_key = nil
|
107
111
|
attributes = args.first if args.size == 1 && args.first.is_a?(Hash)
|
108
|
-
|
112
|
+
|
113
|
+
if respond_to?(:scope_attributes) && scope_attributes.present?
|
114
|
+
attributes = scope_attributes.merge(attributes || {})
|
115
|
+
end
|
116
|
+
|
117
|
+
if attributes.present?
|
109
118
|
support_table_cache_by_attributes.each do |attribute_names, case_sensitive|
|
110
119
|
cache_key = SupportTableCache.cache_key(self, attributes, attribute_names, case_sensitive)
|
111
120
|
break if cache_key
|
@@ -120,6 +129,35 @@ module SupportTableCache
|
|
120
129
|
end
|
121
130
|
end
|
122
131
|
|
132
|
+
module RelationOverride
|
133
|
+
# Override for the find_by method that looks in the cache first.
|
134
|
+
def find_by(*args)
|
135
|
+
return super unless klass.include?(SupportTableCache)
|
136
|
+
return super if SupportTableCache.cache.nil? || SupportTableCache.disabled?
|
137
|
+
|
138
|
+
cache_key = nil
|
139
|
+
attributes = args.first if args.size == 1 && args.first.is_a?(Hash)
|
140
|
+
|
141
|
+
# Apply any attributes from the current relation chain
|
142
|
+
if scope_attributes.present?
|
143
|
+
attributes = scope_attributes.merge(attributes || {})
|
144
|
+
end
|
145
|
+
|
146
|
+
if attributes.present?
|
147
|
+
support_table_cache_by_attributes.each do |attribute_names, case_sensitive|
|
148
|
+
cache_key = SupportTableCache.cache_key(klass, attributes, attribute_names, case_sensitive)
|
149
|
+
break if cache_key
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
if cache_key
|
154
|
+
SupportTableCache.cache.fetch(cache_key, expires_in: support_table_cache_ttl) { super }
|
155
|
+
else
|
156
|
+
super
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
123
161
|
# Remove the cache entry for this record.
|
124
162
|
# @return [void]
|
125
163
|
def uncache
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: support_table_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Durand
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-05-
|
11
|
+
date: 2022-05-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|