diff options
author | David Yip <yipdw@member.fsf.org> | 2017-10-23 19:31:59 -0500 |
---|---|---|
committer | David Yip <yipdw@member.fsf.org> | 2017-10-23 19:31:59 -0500 |
commit | 8410d33b49d66683f5765b6c6ee5a4a1af5b098f (patch) | |
tree | 23dee7accceb49e187641e274bfed53f66468bd3 /app/models | |
parent | af8f06413ee3bab91f1fb89b5828ed9a44e1a6bd (diff) |
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.
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/glitch/keyword_mute.rb | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/app/models/glitch/keyword_mute.rb b/app/models/glitch/keyword_mute.rb index a7ab3650e..4c3e69de4 100644 --- a/app/models/glitch/keyword_mute.rb +++ b/app/models/glitch/keyword_mute.rb @@ -34,23 +34,20 @@ class Glitch::KeywordMute < ApplicationRecord def initialize(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 def keywords - Glitch::KeywordMute. - where(account_id: account_id). - select(:keyword, :id, :whole_word) + Glitch::KeywordMute.where(account_id: account_id).select(:keyword, :id, :whole_word) end - def regex_for_account - re_text = [].tap do |arr| + def regex_text_for_account + [].tap do |arr| keywords.find_each do |kw| arr << (kw.whole_word ? boundary_regex_for_keyword(kw.keyword) : Regexp.escape(kw.keyword)) end end.join('|') - - /#{re_text}/i unless re_text.empty? end def boundary_regex_for_keyword(keyword) |