DEV: Minor improvements to WordWatcher (#16735)

Follow-up to fd1dc91eed
This commit is contained in:
Alan Guo Xiang Tan 2022-05-24 10:23:54 +08:00 committed by GitHub
parent 1ee973e6e2
commit 6edf101d5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 11 deletions

View File

@ -7,17 +7,14 @@ class WordWatcher
@raw = raw @raw = raw
end end
@cache_enabled = true
def self.disable_cache def self.disable_cache
@disabled = true @cache_enabled = false
end end
def self.enable_cache def self.cache_enabled?
@disabled = false @cache_enabled
end
# Don't cache in tests mode
def self.cache_disabled?
@disabled
end end
def self.words_for_action(action) def self.words_for_action(action)
@ -37,12 +34,12 @@ class WordWatcher
end end
def self.get_cached_words(action) def self.get_cached_words(action)
if cache_disabled? if cache_enabled?
words_for_action(action).presence
else
Discourse.cache.fetch(word_matcher_regexp_key(action), expires_in: 1.day) do Discourse.cache.fetch(word_matcher_regexp_key(action), expires_in: 1.day) do
words_for_action(action).presence words_for_action(action).presence
end end
else
words_for_action(action).presence
end end
end end