overleaf/services/spelling/app/coffee/LearnedWordsManager.coffee

41 lines
986 B
CoffeeScript
Raw Normal View History

2014-08-15 07:13:35 -04:00
db = require("./DB")
2015-03-03 08:57:16 -05:00
LRU = require("lru-cache")
cacheOpts =
max: 5000
maxAge: 1000 * 60 * 60
cache = LRU(cacheOpts)
logger = require 'logger-sharelatex'
metrics = require('metrics-sharelatex')
2014-08-15 07:13:35 -04:00
module.exports = LearnedWordsManager =
learnWord: (user_token, word, callback = (error)->) ->
2015-03-03 08:57:16 -05:00
cache.del(user_token)
2014-08-15 07:13:35 -04:00
db.spellingPreferences.update {
token: user_token
}, {
$push: learnedWords: word
}, {
upsert: true
}, callback
getLearnedWords: (user_token, callback = (error, words)->) ->
2015-03-03 08:57:16 -05:00
cachedWords = cache.get(user_token)
if cachedWords
logger.info user_token:user_token, "cache hit"
metrics.inc "cache-hit", 0.1
return callback(null, cachedWords)
metrics.inc "cache-miss", 0.1
logger.info user_token:user_token, "cache miss"
2014-08-15 07:13:35 -04:00
db.spellingPreferences.findOne token: user_token, (error, preferences) ->
return callback error if error?
2015-03-03 08:57:16 -05:00
words = preferences?.learnedWords || []
cache.set(user_token, words)
callback null, words
2014-08-15 07:13:35 -04:00