Skip to content

Commit

Permalink
Merge pull request #1810 from MushroomObserver/nimmo-cache-try2
Browse files Browse the repository at this point in the history
Cache matrix_box in indexes of Observations & RssLogs
  • Loading branch information
nimmolo committed Jan 18, 2024
2 parents ac07586 + 28edc4c commit f470bbb
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ gem("requestjs-rails")
gem("turbo-rails")
# redis for combining actioncable broadcasts with turbo_stream
# gem("redis", "~> 4.0")
# dalli to run the memcached server
gem("dalli", "~> 3.2")
# Compile SCSS for stylesheets
gem("sassc-rails")
# add locale to cache key
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ GEM
cuprite (0.15)
capybara (~> 3.0)
ferrum (~> 0.14.0)
dalli (3.2.6)
database_cleaner-active_record (2.1.0)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
Expand Down Expand Up @@ -356,6 +357,7 @@ DEPENDENCIES
cache_with_locale
capybara (~> 3.37, >= 3.37.1)
cuprite
dalli (~> 3.2)
database_cleaner-active_record
date (>= 3.2.1)
debug (>= 1.0.0)
Expand Down
5 changes: 4 additions & 1 deletion app/helpers/matrix_box_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ def matrix_box(**args, &block)
def matrix_box_image(image = nil, **)
return unless image

user = User.current ? "logged_in" : "no_user"
tag.div(class: "thumbnail-container") do
interactive_image(image, **)
cache([image, user]) do
concat(interactive_image(image, **))
end
end
end

Expand Down
5 changes: 4 additions & 1 deletion app/models/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@
#
################################################################################
#
class Image < AbstractModel
class Image < AbstractModel # rubocop:disable Metrics/ClassLength
require "fileutils"
require "net/http"

Expand Down Expand Up @@ -882,6 +882,9 @@ def change_vote(user, value, anon: false)
# of the other callbacks, either, since this doesn't result in emails,
# contribution changes, or rss log entries.
save_without_our_callbacks if save_changes
# update +updated_at+ for any associated observations, in order to update
# the cached interactive_image in the matrix_box (contrast with the above)
observations&.touch_all

value
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/controllers/observations/identify/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ flash_error(@error) if @error && @objects.empty?
</div>

<%= paginate_block(@pages) do %>
<%= matrix_table(objects: @objects, identify: true) %>
<%= matrix_table(objects: @objects, identify: true, cached: true) %>
<% end %>

<!--AJAX SAVING VOTE MESSAGE-->
Expand Down
2 changes: 1 addition & 1 deletion app/views/controllers/observations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ flash_error(@error) if @error && @objects.empty?
<% end %>
<%= paginate_block(@pages) do %>
<%= matrix_table(objects: @objects) %>
<%= matrix_table(objects: @objects, cached: true) %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/controllers/rss_logs/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ flash_error(@error) if @error && @objects.empty?
%>
<%= paginate_block(@pages) do %>
<%= matrix_table(objects: @objects) %>
<%= matrix_table(objects: @objects, cached: true) %>
<% end %>
3 changes: 3 additions & 0 deletions app/views/controllers/shared/_matrix_box.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ if presenter
else
image_args = {}
end
# interactive_image votes UI is per-user, so cache must be per-user
# "Helper" comment below is necessary because the helper calls `cache`
%>
<%= matrix_box(columns: columns, id: object_id) do
tag.div(class: "panel panel-default") do
[
tag.div(class: "panel-sizing") do
[
# Helper Dependency Updated: Jan 14, 2024 at 3am
matrix_box_image(image, **image_args),
matrix_box_details(presenter, object, object_id, identify),
].safe_join
Expand Down
9 changes: 6 additions & 3 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,14 @@ class Application < Rails::Application
# Unfortunately this is also added to email templates!
# config.action_view.annotate_rendered_view_with_filenames = true

# Not sure we're even using Rails caching yet, but this gets us current
config.active_support.cache_format_version = 7.1

# Strict loading - just log, don't error out the page!
config.active_record.action_on_strict_loading_violation = :log

# Just starting to use Rails caching on 7.1, so we're current
config.active_support.cache_format_version = 7.1

# Set up memcached as the cache store everywhere
config.cache_store = :mem_cache_store
end
end

Expand Down
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
config.action_controller.perform_caching = true
config.action_controller.enable_fragment_cache_logging = true

config.cache_store = :memory_store
config.cache_store = :memory_store # :mem_cache_store via application.rb
config.public_file_server.headers = {
"Cache-Control" => "public, max-age=#{2.days.to_i}"
}
Expand Down

0 comments on commit f470bbb

Please sign in to comment.