瀏覽代碼

Rails: Use fetch_multi for caching query (#6577)

* Use fetch_multi for caching query

* update gems while we're here

* do a little cleanup

* this comment isn't actually useful
Michael Spiz 4 年之前
父節點
當前提交
a021359466

+ 1 - 1
frameworks/Ruby/rails/Gemfile

@@ -5,7 +5,7 @@ source 'https://rubygems.org' do
   gem 'mysql2', '0.5.3', group: :mysql
   gem 'oj', '~> 3.11.2'
   gem 'pg', '1.2.3', group: :postgresql
-  gem 'puma', '~> 5.2.1'
+  gem 'puma', '~> 5.3.0'
   gem 'rails', '~> 6.1.3'
   gem 'redis', '~> 4.0'
   gem 'tzinfo-data', '1.2021.1'

+ 58 - 58
frameworks/Ruby/rails/Gemfile.lock

@@ -1,60 +1,60 @@
 GEM
   remote: https://rubygems.org/
   specs:
-    actioncable (6.1.3.1)
-      actionpack (= 6.1.3.1)
-      activesupport (= 6.1.3.1)
+    actioncable (6.1.3.2)
+      actionpack (= 6.1.3.2)
+      activesupport (= 6.1.3.2)
       nio4r (~> 2.0)
       websocket-driver (>= 0.6.1)
-    actionmailbox (6.1.3.1)
-      actionpack (= 6.1.3.1)
-      activejob (= 6.1.3.1)
-      activerecord (= 6.1.3.1)
-      activestorage (= 6.1.3.1)
-      activesupport (= 6.1.3.1)
+    actionmailbox (6.1.3.2)
+      actionpack (= 6.1.3.2)
+      activejob (= 6.1.3.2)
+      activerecord (= 6.1.3.2)
+      activestorage (= 6.1.3.2)
+      activesupport (= 6.1.3.2)
       mail (>= 2.7.1)
-    actionmailer (6.1.3.1)
-      actionpack (= 6.1.3.1)
-      actionview (= 6.1.3.1)
-      activejob (= 6.1.3.1)
-      activesupport (= 6.1.3.1)
+    actionmailer (6.1.3.2)
+      actionpack (= 6.1.3.2)
+      actionview (= 6.1.3.2)
+      activejob (= 6.1.3.2)
+      activesupport (= 6.1.3.2)
       mail (~> 2.5, >= 2.5.4)
       rails-dom-testing (~> 2.0)
-    actionpack (6.1.3.1)
-      actionview (= 6.1.3.1)
-      activesupport (= 6.1.3.1)
+    actionpack (6.1.3.2)
+      actionview (= 6.1.3.2)
+      activesupport (= 6.1.3.2)
       rack (~> 2.0, >= 2.0.9)
       rack-test (>= 0.6.3)
       rails-dom-testing (~> 2.0)
       rails-html-sanitizer (~> 1.0, >= 1.2.0)
-    actiontext (6.1.3.1)
-      actionpack (= 6.1.3.1)
-      activerecord (= 6.1.3.1)
-      activestorage (= 6.1.3.1)
-      activesupport (= 6.1.3.1)
+    actiontext (6.1.3.2)
+      actionpack (= 6.1.3.2)
+      activerecord (= 6.1.3.2)
+      activestorage (= 6.1.3.2)
+      activesupport (= 6.1.3.2)
       nokogiri (>= 1.8.5)
-    actionview (6.1.3.1)
-      activesupport (= 6.1.3.1)
+    actionview (6.1.3.2)
+      activesupport (= 6.1.3.2)
       builder (~> 3.1)
       erubi (~> 1.4)
       rails-dom-testing (~> 2.0)
       rails-html-sanitizer (~> 1.1, >= 1.2.0)
-    activejob (6.1.3.1)
-      activesupport (= 6.1.3.1)
+    activejob (6.1.3.2)
+      activesupport (= 6.1.3.2)
       globalid (>= 0.3.6)
-    activemodel (6.1.3.1)
-      activesupport (= 6.1.3.1)
-    activerecord (6.1.3.1)
-      activemodel (= 6.1.3.1)
-      activesupport (= 6.1.3.1)
-    activestorage (6.1.3.1)
-      actionpack (= 6.1.3.1)
-      activejob (= 6.1.3.1)
-      activerecord (= 6.1.3.1)
-      activesupport (= 6.1.3.1)
+    activemodel (6.1.3.2)
+      activesupport (= 6.1.3.2)
+    activerecord (6.1.3.2)
+      activemodel (= 6.1.3.2)
+      activesupport (= 6.1.3.2)
+    activestorage (6.1.3.2)
+      actionpack (= 6.1.3.2)
+      activejob (= 6.1.3.2)
+      activerecord (= 6.1.3.2)
+      activesupport (= 6.1.3.2)
       marcel (~> 1.0.0)
       mini_mime (~> 1.0.2)
-    activesupport (6.1.3.1)
+    activesupport (6.1.3.2)
       concurrent-ruby (~> 1.0, >= 1.0.2)
       i18n (>= 1.6, < 2)
       minitest (>= 5.1)
@@ -80,7 +80,7 @@ GEM
     marcel (1.0.1)
     method_source (1.0.0)
     mini_mime (1.0.3)
-    mini_portile2 (2.5.0)
+    mini_portile2 (2.5.1)
     minitest (5.14.4)
     mysql2 (0.5.3)
     nio4r (2.5.7)
@@ -91,42 +91,42 @@ GEM
       racc (~> 1.4)
     nokogiri (1.11.3-x86_64-linux)
       racc (~> 1.4)
-    oj (3.11.4)
+    oj (3.11.5)
     pg (1.2.3)
-    puma (5.2.2)
+    puma (5.3.0)
       nio4r (~> 2.0)
     racc (1.5.2)
     rack (2.2.3)
     rack-test (1.1.0)
       rack (>= 1.0, < 3)
-    rails (6.1.3.1)
-      actioncable (= 6.1.3.1)
-      actionmailbox (= 6.1.3.1)
-      actionmailer (= 6.1.3.1)
-      actionpack (= 6.1.3.1)
-      actiontext (= 6.1.3.1)
-      actionview (= 6.1.3.1)
-      activejob (= 6.1.3.1)
-      activemodel (= 6.1.3.1)
-      activerecord (= 6.1.3.1)
-      activestorage (= 6.1.3.1)
-      activesupport (= 6.1.3.1)
+    rails (6.1.3.2)
+      actioncable (= 6.1.3.2)
+      actionmailbox (= 6.1.3.2)
+      actionmailer (= 6.1.3.2)
+      actionpack (= 6.1.3.2)
+      actiontext (= 6.1.3.2)
+      actionview (= 6.1.3.2)
+      activejob (= 6.1.3.2)
+      activemodel (= 6.1.3.2)
+      activerecord (= 6.1.3.2)
+      activestorage (= 6.1.3.2)
+      activesupport (= 6.1.3.2)
       bundler (>= 1.15.0)
-      railties (= 6.1.3.1)
+      railties (= 6.1.3.2)
       sprockets-rails (>= 2.0.0)
     rails-dom-testing (2.0.3)
       activesupport (>= 4.2.0)
       nokogiri (>= 1.6)
     rails-html-sanitizer (1.3.0)
       loofah (~> 2.3)
-    railties (6.1.3.1)
-      actionpack (= 6.1.3.1)
-      activesupport (= 6.1.3.1)
+    railties (6.1.3.2)
+      actionpack (= 6.1.3.2)
+      activesupport (= 6.1.3.2)
       method_source
       rake (>= 0.8.7)
       thor (~> 1.0)
     rake (13.0.3)
-    rb-fsevent (0.10.4)
+    rb-fsevent (0.11.0)
     rb-inotify (0.10.1)
       ffi (~> 1.0)
     redis (4.2.5)
@@ -157,7 +157,7 @@ DEPENDENCIES
   mysql2 (= 0.5.3)!
   oj (~> 3.11.2)!
   pg (= 1.2.3)!
-  puma (~> 5.2.1)!
+  puma (~> 5.3.0)!
   rails (~> 6.1.3)!
   redis (~> 4.0)!
   tzinfo-data (= 1.2021.1)!

+ 19 - 16
frameworks/Ruby/rails/app/controllers/hello_world_controller.rb

@@ -1,7 +1,10 @@
 # frozen_string_literal: true
 
 class HelloWorldController < ApplicationController
-  QUERY_RANGE = (1..10_000).to_a
+  QUERY_RANGE = 1..10_000    # range of IDs in the Fortune DB
+  ALL_IDS = QUERY_RANGE.to_a # enumeration of all the IDs in fortune DB
+  MIN_QUERIES = 1            # min number of records that can be retrieved
+  MAX_QUERIES = 500          # max number of records that can be retrieved
 
   def plaintext
     render plain: 'Hello, World!'
@@ -12,11 +15,11 @@ class HelloWorldController < ApplicationController
   end
 
   def db
-    render json: World.find(Random.rand(1..10_000))
+    render json: World.find(random_id)
   end
 
   def query
-    results = QUERY_RANGE.sample(query_count).map do |id|
+    results = ALL_IDS.sample(query_count).map do |id|
       World.find(id)
     end
 
@@ -24,13 +27,11 @@ class HelloWorldController < ApplicationController
   end
 
   def cached_query
-    results = QUERY_RANGE.sample(query_count).map do |id|
-      Rails.cache.fetch(id) do
-        World.find(id).as_json
-      end
+    items = Rails.cache.fetch_multi(*ALL_IDS.sample(query_count)) do |id|
+      World.find(id).as_json
     end
 
-    render json: results
+    render json: items.values
   end
 
   def fortune
@@ -40,13 +41,11 @@ class HelloWorldController < ApplicationController
   end
 
   def update
-    worlds = query_count.times.map { Random.rand(1..10_000) }.map do |id|
-      # get a random row from the database, which we know has 10000
-      # rows with ids 1 - 10000
+    worlds = query_count.times.map { random_id }.map do |id|
       world = World.find(id)
-      random = Random.rand(1..10_000)
-      random = Random.rand(1..10_000) until random != world.randomNumber
-      world.update_columns(randomNumber: random)
+      new_value = random_id
+      new_value = random_id until new_value != world.randomNumber
+      world.update_columns(randomNumber: new_value)
       world
     end
 
@@ -57,9 +56,13 @@ class HelloWorldController < ApplicationController
 
   def query_count
     queries = params[:queries].to_i
-    return 1 if queries < 1
-    return 500 if queries > 500
+    return MIN_QUERIES if queries < MIN_QUERIES
+    return MAX_QUERIES if queries > MAX_QUERIES
 
     queries
   end
+
+  def random_id
+    Random.rand(QUERY_RANGE)
+  end
 end

+ 11 - 11
frameworks/Ruby/rails/app/views/hello_world/fortune.html.erb

@@ -1,12 +1,12 @@
 <table>
-<tr>
-<th>id</th>
-<th>message</th>
-</tr>
-<% @fortunes.each do |fortune| %>
-<tr>
-<td><%= fortune.id %></td>
-<td><%= fortune.message %></td>
-</tr>
-<% end %>
-</table>
+  <tr>
+    <th>id</th>
+    <th>message</th>
+  </tr>
+  <% @fortunes.each do |fortune| %>
+    <tr>
+      <td><%= fortune.id %></td>
+      <td><%= fortune.message %></td>
+    </tr>
+  <% end %>
+</table>

+ 6 - 6
frameworks/Ruby/rails/app/views/layouts/application.html.erb

@@ -1,9 +1,9 @@
 <!DOCTYPE html>
 <html>
-<head>
-<title>Fortunes</title>
-</head>
-<body>
-<%= yield %>
-</body>
+  <head>
+    <title>Fortunes</title>
+  </head>
+  <body>
+    <%= yield %>
+  </body>
 </html>