From deffa0dfd24f1fb90b03e10432ef575954c78615 Mon Sep 17 00:00:00 2001 From: jorg-vr Date: Wed, 1 Feb 2023 11:01:27 +0100 Subject: [PATCH 1/3] Keep programming languages in memory --- app/models/exercise.rb | 4 ++++ app/models/programming_language.rb | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/app/models/exercise.rb b/app/models/exercise.rb index e07d2cde8a..5bc993effe 100644 --- a/app/models/exercise.rb +++ b/app/models/exercise.rb @@ -43,6 +43,10 @@ class Exercise < Activity # Only used manually from the console scope :unstarted_by, ->(users) { where.not(id: Submission.where(user_id: users).select(:exercise_id)) } + def programming_language + ProgrammingLanguage.find(programming_language_id) || super + end + def exercise? true end diff --git a/app/models/programming_language.rb b/app/models/programming_language.rb index 3057ee7c34..e42bedb125 100644 --- a/app/models/programming_language.rb +++ b/app/models/programming_language.rb @@ -16,9 +16,29 @@ class ProgrammingLanguage < ApplicationRecord DEFAULT_ICON = 'file-document-edit-outline'.freeze before_save :fill_fields + after_save :remove_from_cache has_many :exercises, dependent: :restrict_with_error + # There are only a few programming languages, so we can keep them in memory + @@in_memory_instances = {} + + def self.find(*ids) + # We don't have cache keys for this stuff yet + return super unless ids.length == 1 + + id = ids.first + return nil if id.nil? + + return super unless id.is_a?(Integer) + + @@in_memory_instances[id] ||= super + end + + def remove_from_cache + @@in_memory_instances.delete(id) + end + def fill_fields self.editor_name ||= name self.renderer_name ||= name From 459dd8f09222c14ef902bcaced3b6c4d05934c8d Mon Sep 17 00:00:00 2001 From: jorg-vr Date: Wed, 1 Feb 2023 11:18:39 +0100 Subject: [PATCH 2/3] Ignore linting --- app/models/programming_language.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/programming_language.rb b/app/models/programming_language.rb index e42bedb125..a457a2f557 100644 --- a/app/models/programming_language.rb +++ b/app/models/programming_language.rb @@ -21,7 +21,7 @@ class ProgrammingLanguage < ApplicationRecord has_many :exercises, dependent: :restrict_with_error # There are only a few programming languages, so we can keep them in memory - @@in_memory_instances = {} + @@in_memory_instances = {} # rubocop:disable Style/ClassVars def self.find(*ids) # We don't have cache keys for this stuff yet From 22789b124e605c0030edf7587488466438539664 Mon Sep 17 00:00:00 2001 From: jorg-vr Date: Wed, 1 Feb 2023 11:46:10 +0100 Subject: [PATCH 3/3] Don't overwrite find --- app/models/exercise.rb | 2 +- app/models/programming_language.rb | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/app/models/exercise.rb b/app/models/exercise.rb index 5bc993effe..a5ad8acb1c 100644 --- a/app/models/exercise.rb +++ b/app/models/exercise.rb @@ -44,7 +44,7 @@ class Exercise < Activity scope :unstarted_by, ->(users) { where.not(id: Submission.where(user_id: users).select(:exercise_id)) } def programming_language - ProgrammingLanguage.find(programming_language_id) || super + ProgrammingLanguage.cached_find(programming_language_id) || super end def exercise? diff --git a/app/models/programming_language.rb b/app/models/programming_language.rb index a457a2f557..de8be4017c 100644 --- a/app/models/programming_language.rb +++ b/app/models/programming_language.rb @@ -23,16 +23,12 @@ class ProgrammingLanguage < ApplicationRecord # There are only a few programming languages, so we can keep them in memory @@in_memory_instances = {} # rubocop:disable Style/ClassVars - def self.find(*ids) - # We don't have cache keys for this stuff yet - return super unless ids.length == 1 - - id = ids.first + def self.cached_find(id) return nil if id.nil? - return super unless id.is_a?(Integer) + return nil unless id.is_a?(Integer) - @@in_memory_instances[id] ||= super + @@in_memory_instances[id] ||= find(id) end def remove_from_cache