Skip to content

Commit 986f8fe

Browse files
authored
Merge pull request #193 from rails/find-by-sql
Use find_by_sql for SQL lookups
2 parents b8a4548 + 012c3c7 commit 986f8fe

File tree

2 files changed

+53
-61
lines changed

2 files changed

+53
-61
lines changed

app/models/solid_cache/entry.rb

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ def write_multi(payloads)
2020
end
2121

2222
def read(key)
23-
result = select_all_no_query_cache(get_sql, key_hash_for(key)).first
23+
result = select_all_no_query_cache([key]).first
2424
result[1] if result&.first == key
2525
end
2626

2727
def read_multi(keys)
28-
key_hashes = keys.map { |key| key_hash_for(key) }
29-
results = select_all_no_query_cache(get_all_sql(key_hashes), key_hashes).to_h
28+
results = select_all_no_query_cache(keys).to_h
3029
results.except!(results.keys - keys)
3130
end
3231

@@ -107,36 +106,21 @@ def upsert_update_only
107106
[ :key, :value, :byte_size ]
108107
end
109108

110-
def get_sql
111-
@get_sql ||= build_sql(where(key_hash: 1).select(:key, :value))
112-
end
113-
114-
def get_all_sql(key_hashes)
115-
@get_all_sql ||= {}
116-
@get_all_sql[key_hashes.count] ||= build_sql(where(key_hash: key_hashes).select(:key, :value))
117-
end
118-
119-
def build_sql(relation)
120-
collector = Arel::Collectors::Composite.new(
121-
Arel::Collectors::SQLString.new,
122-
Arel::Collectors::Bind.new,
123-
)
124-
125-
connection.visitor.compile(relation.arel.ast, collector)[0]
126-
end
127-
128-
def select_all_no_query_cache(query, values)
109+
def select_all_no_query_cache(keys)
129110
uncached do
130-
if connection.prepared_statements?
131-
result = connection.select_all(sanitize_sql(query), "#{name} Load", Array(values), preparable: true)
132-
else
133-
result = connection.select_all(sanitize_sql([ query, *values ]), "#{name} Load", Array(values), preparable: false)
134-
end
135-
136-
result.cast_values(SolidCache::Entry.attribute_types)
111+
find_by_sql([select_sql(keys), *key_hashes_for(keys)]).pluck(:key, :value)
137112
end
138113
end
139114

115+
def select_sql(keys)
116+
@get_sql ||= {}
117+
@get_sql[keys.count] ||= \
118+
where(key_hash: [ "1111", "2222" ])
119+
.select(:key, :value)
120+
.to_sql
121+
.gsub("1111, 2222", (["?"] * keys.count).join(", "))
122+
end
123+
140124
def delete_no_query_cache(attribute, values)
141125
uncached do
142126
relation = where(attribute => values)
@@ -160,6 +144,10 @@ def key_hash_for(key)
160144
Digest::SHA256.digest(key.to_s).unpack("q>").first
161145
end
162146

147+
def key_hashes_for(keys)
148+
keys.map { |key| key_hash_for(key) }
149+
end
150+
163151
def byte_size_for(payload)
164152
payload[:key].to_s.bytesize + payload[:value].to_s.bytesize + ESTIMATED_ROW_OVERHEAD
165153
end

gemfiles/rails_main.gemfile.lock

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GIT
22
remote: https://github.com/rails/rails.git
3-
revision: ea0f0a2c964659aa2f9ead9ba999f53a6cc39366
3+
revision: d7f93473682d9b64ab268a6736d5af2853e97ce2
44
branch: main
55
specs:
66
actionpack (8.0.0.alpha)
@@ -36,7 +36,9 @@ GIT
3636
connection_pool (>= 2.2.5)
3737
drb
3838
i18n (>= 1.6, < 2)
39+
logger (>= 1.4.2)
3940
minitest (>= 5.1)
41+
securerandom (>= 0.3)
4042
tzinfo (~> 2.0, >= 2.0.5)
4143
railties (8.0.0.alpha)
4244
actionpack (= 8.0.0.alpha)
@@ -65,48 +67,49 @@ GEM
6567
ast (2.4.2)
6668
base64 (0.2.0)
6769
bigdecimal (3.1.8)
68-
builder (3.2.4)
69-
concurrent-ruby (1.3.1)
70+
builder (3.3.0)
71+
concurrent-ruby (1.3.4)
7072
connection_pool (2.4.1)
7173
crass (1.0.6)
7274
debug (1.9.2)
7375
irb (~> 1.10)
7476
reline (>= 0.3.8)
7577
drb (2.2.1)
76-
erubi (1.12.0)
78+
erubi (1.13.0)
7779
globalid (1.2.1)
7880
activesupport (>= 6.1)
7981
i18n (1.14.5)
8082
concurrent-ruby (~> 1.0)
8183
io-console (0.7.2)
82-
irb (1.13.1)
84+
irb (1.14.0)
8385
rdoc (>= 4.0.0)
8486
reline (>= 0.4.2)
8587
json (2.7.2)
8688
language_server-protocol (3.17.0.3)
89+
logger (1.6.0)
8790
loofah (2.22.0)
8891
crass (~> 1.0.2)
8992
nokogiri (>= 1.12.0)
90-
minitest (5.23.1)
91-
mocha (2.3.0)
93+
minitest (5.24.1)
94+
mocha (2.4.5)
9295
ruby2_keywords (>= 0.0.5)
9396
msgpack (1.7.2)
9497
mysql2 (0.5.6)
95-
nokogiri (1.16.5-arm64-darwin)
98+
nokogiri (1.16.7-arm64-darwin)
9699
racc (~> 1.4)
97-
nokogiri (1.16.5-x86_64-darwin)
100+
nokogiri (1.16.7-x86_64-darwin)
98101
racc (~> 1.4)
99-
nokogiri (1.16.5-x86_64-linux)
102+
nokogiri (1.16.7-x86_64-linux)
100103
racc (~> 1.4)
101-
parallel (1.24.0)
102-
parser (3.3.2.0)
104+
parallel (1.26.2)
105+
parser (3.3.4.2)
103106
ast (~> 2.4.1)
104107
racc
105-
pg (1.5.6)
108+
pg (1.5.7)
106109
psych (5.1.2)
107110
stringio
108-
racc (1.8.0)
109-
rack (3.0.11)
111+
racc (1.8.1)
112+
rack (3.1.7)
110113
rack-session (2.0.0)
111114
rack (>= 3.0.0)
112115
rack-test (2.1.0)
@@ -126,51 +129,52 @@ GEM
126129
rdoc (6.7.0)
127130
psych (>= 4.0.0)
128131
regexp_parser (2.9.2)
129-
reline (0.5.8)
132+
reline (0.5.9)
130133
io-console (~> 0.5)
131-
rexml (3.2.8)
132-
strscan (>= 3.0.9)
133-
rubocop (1.64.0)
134+
rexml (3.3.5)
135+
strscan
136+
rubocop (1.65.1)
134137
json (~> 2.3)
135138
language_server-protocol (>= 3.17.0)
136139
parallel (~> 1.10)
137140
parser (>= 3.3.0.2)
138141
rainbow (>= 2.2.2, < 4.0)
139-
regexp_parser (>= 1.8, < 3.0)
142+
regexp_parser (>= 2.4, < 3.0)
140143
rexml (>= 3.2.5, < 4.0)
141144
rubocop-ast (>= 1.31.1, < 2.0)
142145
ruby-progressbar (~> 1.7)
143146
unicode-display_width (>= 2.4.0, < 3.0)
144-
rubocop-ast (1.31.3)
147+
rubocop-ast (1.32.0)
145148
parser (>= 3.3.1.0)
146149
rubocop-md (1.2.2)
147150
rubocop (>= 1.0)
148-
rubocop-minitest (0.35.0)
151+
rubocop-minitest (0.35.1)
149152
rubocop (>= 1.61, < 2.0)
150153
rubocop-ast (>= 1.31.1, < 2.0)
151154
rubocop-packaging (0.5.2)
152155
rubocop (>= 1.33, < 2.0)
153-
rubocop-performance (1.21.0)
156+
rubocop-performance (1.21.1)
154157
rubocop (>= 1.48.1, < 2.0)
155158
rubocop-ast (>= 1.31.1, < 2.0)
156-
rubocop-rails (2.25.0)
159+
rubocop-rails (2.25.1)
157160
activesupport (>= 4.2.0)
158161
rack (>= 1.1)
159162
rubocop (>= 1.33.0, < 2.0)
160163
rubocop-ast (>= 1.31.1, < 2.0)
161164
ruby-progressbar (1.13.0)
162165
ruby2_keywords (0.0.5)
166+
securerandom (0.3.1)
163167
sprockets (4.2.1)
164168
concurrent-ruby (~> 1.0)
165169
rack (>= 2.2.4, < 4)
166-
sprockets-rails (3.4.2)
167-
actionpack (>= 5.2)
168-
activesupport (>= 5.2)
170+
sprockets-rails (3.5.2)
171+
actionpack (>= 6.1)
172+
activesupport (>= 6.1)
169173
sprockets (>= 3.0.0)
170-
sqlite3 (2.0.2-arm64-darwin)
171-
sqlite3 (2.0.2-x86_64-darwin)
172-
sqlite3 (2.0.2-x86_64-linux-gnu)
173-
stringio (3.1.0)
174+
sqlite3 (2.0.3-arm64-darwin)
175+
sqlite3 (2.0.3-x86_64-darwin)
176+
sqlite3 (2.0.3-x86_64-linux-gnu)
177+
stringio (3.1.1)
174178
strscan (3.1.0)
175179
thor (1.3.1)
176180
timeout (0.4.1)
@@ -179,7 +183,7 @@ GEM
179183
unicode-display_width (2.5.0)
180184
useragent (0.16.10)
181185
webrick (1.8.1)
182-
zeitwerk (2.6.15)
186+
zeitwerk (2.6.17)
183187

184188
PLATFORMS
185189
arm64-darwin-21

0 commit comments

Comments
 (0)