Only cache the regex text, not the regex itself.
It is possible to cache a Regexp object, but I'm not sure what happens if e.g. that object remains in cache across two different Ruby versions. Caching a string seems to raise fewer questions.
This commit is contained in:
parent
af8f06413e
commit
8410d33b49
|
@ -34,23 +34,20 @@ class Glitch::KeywordMute < ApplicationRecord
|
||||||
|
|
||||||
def initialize(account_id)
|
def initialize(account_id)
|
||||||
@account_id = account_id
|
@account_id = account_id
|
||||||
@regex = Rails.cache.fetch("keyword_mutes:regex:#{account_id}") { regex_for_account }
|
regex_text = Rails.cache.fetch("keyword_mutes:regex:#{account_id}") { regex_text_for_account }
|
||||||
|
@regex = /#{regex_text}/i unless regex_text.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
def keywords
|
def keywords
|
||||||
Glitch::KeywordMute.
|
Glitch::KeywordMute.where(account_id: account_id).select(:keyword, :id, :whole_word)
|
||||||
where(account_id: account_id).
|
|
||||||
select(:keyword, :id, :whole_word)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def regex_for_account
|
def regex_text_for_account
|
||||||
re_text = [].tap do |arr|
|
[].tap do |arr|
|
||||||
keywords.find_each do |kw|
|
keywords.find_each do |kw|
|
||||||
arr << (kw.whole_word ? boundary_regex_for_keyword(kw.keyword) : Regexp.escape(kw.keyword))
|
arr << (kw.whole_word ? boundary_regex_for_keyword(kw.keyword) : Regexp.escape(kw.keyword))
|
||||||
end
|
end
|
||||||
end.join('|')
|
end.join('|')
|
||||||
|
|
||||||
/#{re_text}/i unless re_text.empty?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def boundary_regex_for_keyword(keyword)
|
def boundary_regex_for_keyword(keyword)
|
||||||
|
|
Reference in New Issue