Skip to content

Commit

Permalink
Merge pull request #35709 from code-dot-org/profanity-caching
Browse files Browse the repository at this point in the history
Add caching to profanity filter route
  • Loading branch information
JillianK committed Jul 9, 2020
2 parents eca751b + 89f8b98 commit 94d250f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions apps/src/code-studio/components/libraries/util.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import $ from 'jquery';

export const findProfanity = (text, language) => {
export const findProfanity = (text, language, shouldCache) => {
return $.ajax({
url: '/profanity/find',
method: 'POST',
contentType: 'application/json;charset=UTF-8',
data: JSON.stringify({text, language})
data: JSON.stringify({text, language, should_cache: shouldCache})
});
};
2 changes: 1 addition & 1 deletion apps/src/lib/util/speech.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function textToSpeech(

var request = new XMLHttpRequest();
const resultPromise = new Promise(async function(resolve, reject) {
let profaneWords = await findProfanity(text, languageCode);
let profaneWords = await findProfanity(text, languageCode, true);
if (profaneWords && profaneWords.length > 0) {
if (!Array.isArray(profaneWords)) {
profaneWords = [profaneWords];
Expand Down
14 changes: 13 additions & 1 deletion dashboard/app/controllers/profanity_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ class ProfanityController < ApplicationController
# POST /profanity/find
# @param [String] params[:text] String to test
# @param [String] params[:language] Language of the text to test
# @param [Boolean] params[:should_cache] Whether or not we should use the cache on this request
# @returns [Array<String>|nil] Profane words within the given string
def find
render json: ProfanityFilter.find_potential_profanities(params[:text] || "", params[:language] || request.locale)
profanity_result = []
unless params[:text].nil?
if params[:should_cache]
cache_key = "profanity/#{params[:language] || request.locale}/#{params[:text]}"
profanity_result = Rails.cache.fetch(cache_key) do
ProfanityFilter.find_potential_profanities(params[:text], params[:language] || request.locale)
end
else
profanity_result = ProfanityFilter.find_potential_profanities(params[:text], params[:language] || request.locale)
end
end
render json: profanity_result
end
end

0 comments on commit 94d250f

Please sign in to comment.