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

Add caching to profanity filter route #35709

Merged
merged 3 commits into from
Jul 9, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/src/code-studio/components/libraries/util.js
@@ -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
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
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
JillianK marked this conversation as resolved.
Show resolved Hide resolved
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