Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep programming languages in memory #4376

Merged
merged 3 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/models/exercise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.cached_find(programming_language_id) || super
end

def exercise?
true
end
Expand Down
16 changes: 16 additions & 0 deletions app/models/programming_language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,25 @@ 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 = {} # rubocop:disable Style/ClassVars

def self.cached_find(id)
return nil if id.nil?

return nil unless id.is_a?(Integer)

@@in_memory_instances[id] ||= find(id)
end

def remove_from_cache
@@in_memory_instances.delete(id)
end

def fill_fields
self.editor_name ||= name
self.renderer_name ||= name
Expand Down